Java語言程序設(shè)計(基礎(chǔ)篇)原書第十一版 梁勇 第9章 課后題答案_第1頁
Java語言程序設(shè)計(基礎(chǔ)篇)原書第十一版 梁勇 第9章 課后題答案_第2頁
Java語言程序設(shè)計(基礎(chǔ)篇)原書第十一版 梁勇 第9章 課后題答案_第3頁
Java語言程序設(shè)計(基礎(chǔ)篇)原書第十一版 梁勇 第9章 課后題答案_第4頁
Java語言程序設(shè)計(基礎(chǔ)篇)原書第十一版 梁勇 第9章 課后題答案_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

9.1設(shè)計一個名為Rectangle的類表示矩形publicclassTest{publicstaticvoidmain(String[]args){Rectanglerectanglel=newRectangle(4,40);System.out.println("矩形的寬是"+rectanglel.width+",高是"+rectanglel.height+"z面積是"+rectanglel?getArea()+”,周長是n+rectanglel.getPerimeter());Rectanglerectangle2=newRectangle(3.5,35.9);System.out.print("矩形的寬是"+rectangle2.width+",高是"+rectangle2.height+",面積是,,+rectangle2.getArea()+,1,周長是,,+rectangle2.getPerimeter());}}classRectangle{doublewidth=l;doubleheight=l;Rectangle(){}RectanglefdoublenewWidth,doublenewHeight){width=newWidth;height=newHeight;}doublegetArea(){returnwidth*height;}doublegetPerimeter(){return(width+height)*2;〃C:\ProgramFiles\java\jdkl.8.0_101\bin\java.exe〃...矩形的寬是4.0,髙是40.0,面積是160.0,周長是88.0矩形的寬是3?5,高是35.9,面積是125.64999999999999,周長是78?8Processfinishedwithexitcode09.3使用Date類publicclassTest{publicstaticvoidmain(String[]args){Datedate=newDate();long1=10000;for(inti=1;i<9;i++){date.setTime(l);printDate(l,date);l*=10;}}publicstaticvoidprintDateflongI,Datedate){System.out.println(l+n:n+date.toString());C:\ProgramFiles'java'jdkl.8?0_101\bin\ja10000:ThuJan0108:00:10CST1970100000:ThuJan0108:01:40CST19701000000:ThuJan0108:16:40CST197010000000:ThuJan0110:46:40CST1970100000000:FriJan0211:46:40CST19701000000000:MonJan1221:46:40CST197010000000000:MonApr2701:46:40CST1970100000000000:SatMar0317:46:40CST1973Processfinishedwithexitcode09.4使用Random類publicclassTest{publicstaticvoidmain(String[]args){Randomrandom=newRandom(lOOO);for(inti=l;i<51;i++){System.out.print(random?nextlnt(100)+”\t“);if(i%10==0){System.out.printlnf);}}}}“C:\ProgrQmFiles\java\jdkl.8.0_101\bi87357624924941456450795972833675462234122718929342494235763205295873199187929155584719558877738Processfinishedwithexitcode09.6秒表publicclassTest{publicstaticvoidmain(String[]args){StopWatchstopWatch=newStopWatch();stopWatch.start();int[]a=newint[100000];for(inti=0;i<100000;i++){a[i]=(int)(Math.random()*200);}count(a);stopWatch.stop();System.out.printf"測試所用時間為:"+stopWatch.getElapsedTime()+"ms");}publicstaticvoidcount(int[]a){intmax;inttmp;for(inti=0;i<a」ength;i++){max=i;forfintj=i+l;j<a」ength;j++){if(a[max]<a[jj)max=j;}if(i!=max){tmp=a[i];a[i]=a[max];a[max]=tmp;}}}}classStopWatch{privatelongstartTime;privatelongendTime;StopWatch(){startTime=System.currentTimeMillis();}publicvoidstart(){startTime=System.currentTimeMillis();}"C:\ProgramFiles\java\jdk1?"C:\ProgramFiles\java\jdk1?8?0測試所用時間為;12406msProcessfinishedwithexitcode}publiclonggetElapsedTime(){returnendTime-startTime;9.7Accont類publicclassDemoA{publicstaticvoidmain(String[]args){Accountaccount=newAccountf);Scannerin=newScanner(System.in);System.out.printlnf'創(chuàng)建用戶,依次輸入id、balance^annuallnterestRate:H);intid=in.nextlnt();doublebalance=in.nextDouble();doubleannuallnterestRate=in.nextDoublef);account.ChuangJian(id,balance,annuallnterestRate);System.out.printf'請輸入取款額:”);intmoney=in.nextlnt();account.\A/ithDraw(money);System.out.printf"請輸入存款額:");money=in.nextlnt();account.deposit(money);account.Print();}}classAccount!privateintid;privatedoublebalance;privatedoubleannuallnterestRate;privateDatedateCreated;publicvoidPrint(){System.out.printlnf1余額為:H+balance);System.out.printlnf'月利息為:,,+getMonthlylnterset());System.out.printf'開戶日期為:");getDate();}publicvoidChuangJian(){id=0;balance=0;annuallnterestRate=O;}publicvoidChuangJian(intid,doublebalance,doubleannuallnterestRate){this.id=id;this.balance=balance;this.annuallnterestRate=annuallnterestRate;this.dateCreated=newDate();}publicvoidLook(){System.out.println(Hid:"+id);System.out.println(nbalance:H+balance);System.out.println「annuallnterestRate:"+annuallnterestRate);publicvoidChangefintid){System.out.printlnf'請輸入更改后的id^balance^annuallnterestRate'1);Seanne「in=newScanner(System.in);this.id=in.nextlnt();this,balancedn.nextDouble();this.annuallnterestRate=irunextDoublef);}publicDategetDate(){System.out.println(dateCreated);returndateCreated;}publicdoublegetMonthlylnterestRate(){returnannuallnterestRate/12;}publicdoublegetMonthlylnterset(){returngetMonthlylnterestRate()*balanee;}publicvoidwithDrawfintmoneyOut){balance-=moneyOut;}publicvoiddepos讓(intmoneyln){balance+=moneyln;}},ZC:\ProgramFiles\java\jdk1?8?0_101\bin\java?巳:???創(chuàng)建用戶,依次輸入id、balance、annualInterestRate:請輸入取款額:2500請輸入存款額:3000余額為;20500.0月利息為:76.875開戶日期為:TueOct2218:39:24CST2019Processfinishedwithexitcode09.8Fan類publicclassDemoB{publicstaticvoidmain(String[]args){Fanfanl=newFan();fanl.setSpeed(3);fanl.setRadius(lO);fanl.setColorf^ellow");fanl.setOn(true);System.out.println(,,fabl:"+fanl.toString());Fanfan2=newFan();fan2.setSpeed(2);fan2.setRadius(5);fan2.setColor(,,blueH);fan2.setOn(false);System.out.println(,,fab2:"+fan2.toString());}}classFan{intSLOW=1;intMEDIUM=2;intFAST=3;privateintspeed=SLOW;privatebooleanon=false;privatedoubleradius=5;Stringcolor=,,blue";publicFan(){}publicintgetSpeed(){returnspeed;}publicvoidsetSpeed(intspeed){this.speed=speed;}publicbooleanisOn(){returnon;}publicvoidsetOnfbooleanon){this.on二on;}publicdoublegetRadius(){returnradius;}publicvoidsetRadiusfdoubleradius){this.radius=radius;publicStringgetColor(){returncolor;publicvoidsetColor(Stringcolor){this.color=color;}publicStringtoString(){if(on){returngetSpeed()+,,\t,,+getColor()+,,\t,,+getRadius();}else{return"fanisoff,,+,,\t,,+getColor()+,,\t,,+getRadius();9.11代數(shù):2X2的線性方程publicclassDemoC{publicstaticvoidmain(String[]args){LinearEquationlinearEquationl=newLinearEquation(9,4,3,-5,-6,-21);if(linearEquationl.isSolvable()){System.out.printlnf'X是:”+linearEquationl?getX()+蔦\tY是:,,+linearEquationl.getY());}elseSystem.out.print(HTheequationhasnosolution");LinearEquationlinearEquation2=newLinearEquation(1,2,2,4,4,5);if(linearEquation2.isSolvable()){System.out.print("X是:,,+linearEquation2.getX()+,,/\tY是:N+linearEquation2.getY());}elseSystem.out.print(HTheequationhasnosolution");classLinearEquation{privatedoublea;privatedoubleb;pr

溫馨提示

  • 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

提交評論