面向對象程序設計的基本操作 實驗報告_第1頁
面向對象程序設計的基本操作 實驗報告_第2頁
面向對象程序設計的基本操作 實驗報告_第3頁
面向對象程序設計的基本操作 實驗報告_第4頁
面向對象程序設計的基本操作 實驗報告_第5頁
已閱讀5頁,還剩6頁未讀 繼續免費閱讀

付費下載

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

PAGE學生姓名:林子木學號:學院:理學院班級:課程名稱面向對象課程設計實驗題目:面向對象程序設計的基本操作指導教師姓名及職稱副教授實驗師2014年10月17日目錄TOC\o"1-1"\h\z\u一、實驗目的 1二、實驗內容 1三、實現方法 1四、實驗結果 2五、源程序清單 3六、思考及總結 8-PAGE8-一、實驗目的1.掌握Java類的定義、對象的創建和使用方法。2.掌握Java類中域、方法修飾符的運用。3.掌握Java類的訪問控制符的使用。二、實驗內容定義一個表示公司員工的Employee類,類中包括姓名、年齡、工資、入職時間(包括年月日)4個private數據域,通過構造函數對所有數據域進行初始化,并通過定義一些public方法訪問和修改類中的private數據域,具體要求如下:1.編寫JavaApplication程序,在程序中創建至少2個不同的Employee對象,并將創建的Employee對象的所有信息輸出。2.編寫JavaApplet程序,通過文本框輸入個員工的姓名、年齡、工資、入職時間信息創建Employee對象(至少創建2個員工對象),并將創建的Employee對象的所有信息在界面上顯示輸出。3.在操作2的基礎上通過建立新的文本框輸入某個員工新的年齡和工資信息,以對原來員工的年齡和工資信息進行修改,并將修改后員工的所有信息在界面上重新顯示輸出。三、實現方法1.第1題:創建Employee對象,定義了域:name(職工姓名)、age(職工年齡)、salary(職工工資)、entry_time(入職時間),定義方法getInfo()返回職工信息。創建2個員工對象,輸出員工信息。2.第2題:在Applet中,定義Label、TextField、Button,并將輸入的數據作為實例化Employee對象的輸入參數,最后將返回的輸出內容在屏幕上顯示出來。3.第3題:在Applet中,定義Label、TextField、TextArea、Button,判別修改年齡和修改工資文本框有沒有輸入內容,若輸入了內容,則將輸入的內容代替原來年齡和工資的值,并作為實例化Employee對象的輸入參數,最后在屏幕上顯示出輸出。

四、實驗結果1.第1題運行結果:2.第2題運行結果:3.第3題運行結果:五、源程序清單第1題源程序:packageexperiment3;publicclassexperiment3_1{ publicstaticvoidmain(Stringargs[]){ Employeeemp1=newEmployee("徐永凱",21,2000,"2014年10月17日"); System.out.println(emp1.getInfo()); Employeeemp2=newEmployee("張靖",21,3000,"2014年10月17日"); System.out.println(emp2.getInfo()); }}classEmployee{ Stringname;//姓名 intage;//年齡 doublesalary;//工資 Stringentry_time;//入職時間 Employee(Stringname,intage,doublesal,Stringentry){ =name; this.age=age; this.salary=sal; this.entry_time=entry; } StringgetInfo(){ return"職工名稱:"+name+"\n" +"職工年齡:"+age+"\n" +"職工工資:"+salary+"\n" +"入職時間:"+entry_time+"\n"; }}第2題源程序:packageexperiment3;importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassexperiment3_2extendsAppletimplementsActionListener{ /** * */ privatestaticfinallongserialVersionUID=1L; Labelname1Prompt,age1Prompt,salary1Prompt,entry1Prompt; Labelname2Prompt,age2Prompt,salary2Prompt,entry2Prompt; TextFieldname1Input,age1Input,salary1Input,entry1Input; TextFieldname2Input,age2Input,salary2Input,entry2Input; Buttonbtn; Stringname1,name2;//姓名 intage1,age2;//年齡 doublesalary1,salary2;//工資 Stringentry_time1,entry_time2;//入職時間 Stringstr1,str2; publicvoidinit(){ name1Prompt=newLabel("第一位職工名稱:"); age1Prompt=newLabel("第一位職工年齡:"); salary1Prompt=newLabel("第一位職工工資:"); entry1Prompt=newLabel("第一位入職時間:"); name1Input=newTextField(10); age1Input=newTextField(10); salary1Input=newTextField(10); entry1Input=newTextField(10); name2Prompt=newLabel("第二位職工名稱:"); age2Prompt=newLabel("第二位職工年齡:"); salary2Prompt=newLabel("第二位職工工資:"); entry2Prompt=newLabel("第二位入職時間:"); name2Input=newTextField(10); age2Input=newTextField(10); salary2Input=newTextField(10); entry2Input=newTextField(10); btn=newButton("提交"); add(name1Prompt);add(name1Input); add(name2Prompt);add(name2Input); add(age1Prompt);add(age1Input); add(age2Prompt);add(age2Input); add(salary1Prompt);add(salary1Input); add(salary2Prompt);add(salary2Input); add(entry1Prompt);add(entry1Input); add(entry2Prompt);add(entry2Input); add(btn); btn.addActionListener(this); } publicvoidpaint(Graphicsg){ g.drawString(str1,20,160); g.drawString(str2,20,180); } publicvoidactionPerformed(ActionEvente){ name1=name1Input.getText(); age1=Integer.parseInt(age1Input.getText()); salary1=Double.parseDouble(salary1Input.getText()); entry_time1=entry1Input.getText(); name2=name2Input.getText(); age2=Integer.parseInt(age2Input.getText()); salary2=Double.parseDouble(salary2Input.getText()); entry_time2=entry2Input.getText(); Employeeemp1=newEmployee(name1,age1,salary1,entry_time1); str1=emp1.getInfo(); Employeeemp2=newEmployee(name2,age2,salary2,entry_time2); str2=emp2.getInfo(); repaint(); }}classEmployee{ Stringname;//姓名 intage;//年齡 doublesalary;//工資 Stringentry_time;//入職時間 Employee(Stringname,intage,doublesalary2,Stringentry){ =name; this.age=age; this.salary=salary2; this.entry_time=entry; } StringgetInfo(){ return"職工名稱:"+name+"\n" +"職工年齡:"+age+"\n" +"職工工資:"+salary+"\n" +"入職時間:"+entry_time+"\n"; }}第3題源程序:packageexperiment3;importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassexperiment3_3extendsAppletimplementsActionListener{ /** * */ privatestaticfinallongserialVersionUID=1L; Labelname1Prompt,age1Prompt,salary1Prompt,entry1Prompt,age1Prompt2,salary1Prompt2; Labelname2Prompt,age2Prompt,salary2Prompt,entry2Prompt,age2Prompt2,salary2Prompt2; TextFieldname1Input,age1Input,salary1Input,entry1Input,age1Input2,salary1Input2; TextFieldname2Input,age2Input,salary2Input,entry2Input,age2Input2,salary2Input2; Buttonbtn; Stringname1,name2;//姓名 intage1,age2;//年齡 doublesalary1,salary2;//工資 Stringentry_time1,entry_time2;//入職時間 Stringstr1,str2; publicvoidinit(){ name1Prompt=newLabel("第一位職工名稱:"); age1Prompt=newLabel("第一位職工年齡:"); salary1Prompt=newLabel("第一位職工工資:"); entry1Prompt=newLabel("第一位入職時間:"); age1Prompt2=newLabel("修改職工年齡:"); salary1Prompt2=newLabel("修改職工工資:"); name1Input=newTextField(10); age1Input=newTextField(10); salary1Input=newTextField(10); entry1Input=newTextField(10); age1Input2=newTextField(10); salary1Input2=newTextField(10); name2Prompt=newLabel("第二位職工名稱:"); age2Prompt=newLabel("第二位職工年齡:"); salary2Prompt=newLabel("第二位職工工資:"); entry2Prompt=newLabel("第二位入職時間:"); age2Prompt2=newLabel("修改職工年齡:"); salary2Prompt2=newLabel("修改職工工資:"); name2Input=newTextField(10); age2Input=newTextField(10); salary2Input=newTextField(10); entry2Input=newTextField(10); age2Input2=newTextField(10); salary2Input2=newTextField(10); btn=newButton("提交"); add(name1Prompt);add(name1Input); add(name2Prompt);add(name2Input); add(age1Prompt);add(age1Input); add(age2Prompt);add(age2Input); add(salary1Prompt);add(salary1Input); add(salary2Prompt);add(salary2Input); add(entry1Prompt);add(entry1Input); add(entry2Prompt);add(entry2Input); add(age1Prompt2);add(age1Input2); add(age2Prompt2);add(age2Input2); add(salary1Prompt2);add(salary1Input2); add(salary2Prompt2);add(salary2Input2); add(btn); btn.addActionListener(this); } publicvoidpaint(Graphicsg){ g.drawString(str1,20,210); g.drawString(str2,20,230); } publicvoidactionPerformed(ActionEvente){ name1=name1Input.getText(); age1=Integer.parseInt(age1Input.getText()); salary1=Double.parseDouble(salary1Input.getText()); entry_time1=entry1Input.getText(); name2=name2Input.getText(); age2=Integer.parseInt(age2Input.getText()); salary2=Double.parseDouble(salary2Input.getText()); entry_time2=entry2Input.getText(); if(age1Input2.getText()!=""){ age1=Integer.parseInt(age1Input2.getText()); } if(salary1Input2.getText()!=""){ salary1=Double.parseDouble(sal

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論