潍坊市论坛

首页 » 分类 » 定义 » Java第九章作业补充解析
TUhjnbcbe - 2021/7/5 13:26:00

9.1sumRect

publicclasssumRect{publicstaticvoidmain(String[]args){Rectanglerectangle1=newRectangle(4,40);Rectanglerectangle2=newRectangle(3.5,35.9);System.out.println("宽:"+rectangle1.width+"高:"+rectangle1.height+"面积:"+rectangle1.getArea()+"周长:"+rectangle1.getPerimeter());System.out.println("宽:"+rectangle2.width+"高:"+rectangle2.height+"面积:"+rectangle2.getArea()+"周长:"+rectangle2.getPerimeter());}}classRectangle{doublewidth,height;Rectangle(){}Rectangle(doublenewWidth,doublenewHeight){width=newWidth;height=newHeight;}doublegetArea(){returnwidth*height;}doublegetPerimeter(){return2*(width+height);}}

9.2Stock

publicclassstock{publicstaticvoidmain(String[]args){Stock1sto=newStock1("ORCL","OracleCorporation");sto.setPrePrice(34.5);sto.setCurPrice(34.35);System.out.printf("%.2f",sto.getChangePercent());}}classStock1{Stringsymbol,name;privatedoubleprePrice,curPrice;Stock1(){}Stock1(StringnewSymbol,StringnewName){symbol=newSymbol;name=newName;}voidsetPrePrice(doubleprePrice){this.prePrice=prePrice;}voidsetCurPrice(doublecurPrice){this.curPrice=curPrice;}doublegetChangePercent(){return(curPrice-prePrice)/prePrice*;}}

9.6sTime

importjava.util.Random;publicclasssTime{publicstaticvoidmain(String[]args){Randomr=newRandom(000);int[]arr=newint[0];for(inti=0;i0;i++){arr=r.nextInt(000);}StopWatchs=newStopWatch();longnewStartTime=System.currentTimeMillis();s.start(newStartTime);for(inti=0;i0;i++){selectionSort(arr);}longnewEndTime=System.currentTimeMillis();s.stop(newEndTime);System.out.println("排序共计"+(s.getElapsedTime()/0.0)+"毫秒");}publicstaticvoidselectionSort(int[]arr){for(inti=0;iarr.length-1;i++){intmin=i;for(intj=i+1;jarr.length;j++){if(arr[j]arr[min]){min=j;}}if(min!=i){inttemp=arr;arr=arr[min];arr[min]=temp;}}}}classStopWatch{privatelongstartTime;privatelongendTime;StopWatch(){}publiclonggetStart(){returnstartTime;}publiclonggetEnd(){returnendTime;}publicvoidstart(longnewStartTime){startTime=newStartTime;}publicvoidstop(longnewEndTime){endTime=newEndTime;}publiclonggetElapsedTime(){longelapsedTime=endTime-startTime;returnelapsedTime;}}

9.8fan

publicclassfan{publicstaticvoidmain(String[]args){Fan1fan=newFan1();fan.turnOn();fan.ToString();}}classFan1{privatefinalshortSLOW=1;privatefinalshortMEDIUM=2;privatefinalshortFAST=3;privateintspeed;privatebooleanon;privatedoubleradius;privateStringcolor;Fan1(){speed=SLOW;on=false;radius=5;color="blue";}publicvoidturnOn(){on=true;}publicvoidturnOff(){on=false;}publicvoidsetRadius(doublenewRadius){radius=newRadius;}publicdoublegetRadius(){returnradius;}publicvoidsetColor(StringnewColor){color=newColor;}publicStringgetColor(){returncolor;}publicvoidsetSpeed(intnewSpeed){speed=newSpeed;}publicintgetSpeed(){returnspeed;}publicvoidToString(){if(on)System.out.println(speed+""+color+""+radius);elseSystem.out.println("fanisoff");}}

#以上程序仅供参考

题目来自Java语言程序设计与数据结构(基础篇)

编译软件采用IntelliJIDEA.1

订书机工作室编辑

预览时标签不可点收录于话题#个上一篇下一篇
1