Java程序設計-試驗3(學生版)_第1頁
Java程序設計-試驗3(學生版)_第2頁
Java程序設計-試驗3(學生版)_第3頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

..實用文檔.實用文檔.華北電力大學實驗報告實驗名稱 Java程序面向?qū)ο笤O計〔二〕課程名稱 Java程序設計專業(yè)班級學 號:

學生姓名成 績:指導教師:張學斌 實驗日期:一、實驗目的及要求掌握類的繼承。掌握類的隱藏和覆蓋。掌握抽象類和抽象方法二、試驗內(nèi)容實驗1中國人、北京人和美國人實驗要求編寫程序模擬中國人、美國人是人,北京人是人。出主類外,程序中還有4個類ChinaPeoplAmericanPeopl和BeijingPeope要求如下:People類有權(quán)限是protecte的double型成員變量height和weightcspeakHellopublicvoidaverageHeihblicvoidaverageWeihChinaPeopl類是PeoplpublicvoidchinaGonguChinapeople重寫父類的publicvoidspeakHelicvoidaverageHeihblicvoidaverageWeight()方法。AmericanPeople類是People的子類,新增publicvoidamericanBoxing(方)法。要求Americanpeopl重寫父類的publicvoidspeakHelicvoidaverageHeihblicvoidaverageWeight法。BeijingPeope是ChinapeoplpublicvoidbeijingOprChinaPeople重寫父類的publicvoidspeakHelicvoidaverageHeihblicvoidaverageWeight()方法。程序模板publicclassPeople{protecteddoubleweight,height;publicvoidspeakHello(){System.out.println("yayayaya");}publicvoidaverageHeight(){height=173;System.out.println("averageheight:"+height);}publicvoidaverageWeight(){weight=70;System.out.println("averageweight:"+weight);}}publicclassChinaPeopleextendsPeoplepublicvoidspeakHello(){System.out.printn}publicvoidaverageHeight(){height=168.78;System.out.println(":"+height+"厘米");}【代碼1】//重寫publicvoidaverageWeight()方法,輸出:"中國人的平均體重:65公斤"publicvoidchinaGongfu(){System.out.println("坐如鐘,站如松,睡如弓");}}publicclassAmericanPeopleextendsPeople{【代碼2】//重寫publicvoidspeakHello()方法,輸出"Howdoyoudo"【代碼3】//重寫publicvoidaverageHeight()方法,輸出"American'saverageheight:176cm"publicvoidaverageWeight(){weight=75;System.out.println("American'saverageweight:"+weight+"kg");}publicvoidamericanBoxing(){System.out.println("直拳、鉤拳、組合拳");}}publicclassBeijingPeopleextendsChinaPeople{【代碼4】//重寫publicvoidaverageHeight()方法,輸出:"北京人的平均身高:172.5厘米"【代碼5】//重寫publicvoidaverageWeight()方法,輸出:"北京人的平均體重:70公斤"publicvoidbeijingOpera(){System.out.println("花臉、青衣、花旦和老生");}}publicclassExample{publicstaticvoidmain(Stringargs[]){ChinaPeoplechinaPeople=newChinaPeople();AmericanPeopleamericanPeople=newAmericanPeople();BeijingPeoplebeijingPeople=newBeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();}}實驗2銀行利息計算實驗要求假設銀行Bank()已經(jīng)有了按整年year計算利息的一般方法,其中year只能取正整數(shù)。比方按整年計算的方法:doublecomputerInterest(){interest=year**savedMoney;returninterest;}ConstructionBank是Bank法,即自己聲明一個doubleyear變量,比方,當year5.2165216天的利息,但希望首先按銀行BankcomputerInterest()5216天5.216的整數(shù)局部賦給隱藏的year,并讓super年計算利息的方法。要求ConstructionBank和BankOfDalian類是Bank類的子類,ConstructionBank和BankOfDalian都使用super調(diào)用隱藏的成員變量和方法。程序模板publicclassBank{intsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest(){interest=year*interestRate*savedMoney;returninterest;}publicvoidsetInterestRate(doublerate){interestRate=rate;}}publicclassConstructionBankextendsBank{doubleyear;publicdoublecomputerInterest()super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=【代碼1】//super調(diào)用隱藏的computerInterest()方法doubledayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d元存在建設銀行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);returninterest;}}publicclassBankOfDalianextendsBank{doubleyear;publicdoublecomputerInterest()super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=【代碼2】//super調(diào)用隱藏的computerInterest()方法doubledayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d元存在大連銀行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);returninterest;}}publicclassSaveMoney{publicstaticvoidmain(Stringargs[]){intamount=8000;ConstructionBankbank1=newConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);doubleinterest1=bank1 puterInterest();BankOfDalianbank2=newBankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);doubleinterest2=bank2 puterInterest();System.out.printf("兩個銀行利息相差%f元\n",interest2-interest1);}}實驗擴展80008236天的利息實驗3 公司支出的總薪水實驗要求有一個abstractEmployeeEmployee的子類有MonthWorker和YearWorker對象按年領取薪水,MonthWorker按月領取薪水,WeekWorkerEmployee類有一個abstract方法:Publicabstractearnings();子類必須重寫父類的earnings()方法給出各自領取報酬的具體公式。有一個Company類,該類用Employee對象數(shù)組作為成員,Employee對象數(shù)組的單元可以是YearWorker對象的向上轉(zhuǎn)型對象、MonthWorker對象的向上轉(zhuǎn)型對象或WeekWorker對象的向上轉(zhuǎn)型對象。程序能輸出Company對象一年需要支付的薪水總額。程序模板abstractclassEmployee{publicabstractdoubleearnings();}classYearWorkerextendsEmployee{1】//重寫earnings()方法}classMonthWorkerextendsEmployee{【代碼2】//重寫earnings()方法}classWeekWorkerextendsEmployee{【代碼3】//重寫earnings()方法。}classCompany{Employee[]employee;doublesalaries=0;Company(Employee[]employee){this.employee=employee;}publicdoublesalariesPay(){salaries=0;【代碼4】//計算salaries。returnsalaries;}}publicclassCompanySalary{publicstaticvoidmain(Stringargs[]){Employee[]employee=newEmployee[29];公司有29名雇for(inti=0;i<employee.length;i++){ //雇員簡單地分成三類if(i%3==0)employee[i]=newelseif(i%3==1)employee[i]=newMonthWorker();elseif(i%3==2)employee[i]=newYearWorker();}Companycompany=newCompany(employee);元}}實驗指導對于【代碼2】,一年按12個月計算出雇員的一年的年薪,比方:publicdoubleearnings(){return12*2300;}盡管

溫馨提示

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

評論

0/150

提交評論