Java語言程序設(shè)計(第3版)沈澤剛主編第678章課后習(xí)題答案_第1頁
Java語言程序設(shè)計(第3版)沈澤剛主編第678章課后習(xí)題答案_第2頁
Java語言程序設(shè)計(第3版)沈澤剛主編第678章課后習(xí)題答案_第3頁
Java語言程序設(shè)計(第3版)沈澤剛主編第678章課后習(xí)題答案_第4頁
Java語言程序設(shè)計(第3版)沈澤剛主編第678章課后習(xí)題答案_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

Java語?程序設(shè)計(第3版)沈澤剛主編第6,7,8章課后習(xí)題答案Java語?程序設(shè)計(第3版)沈澤剛主編第6,7,8章課后習(xí)題答案第6章字符串importScannerimportScanner;publicclassStringDemo{publicstaticvoidmain(String[]args){ScannerinputnewScanner(System.in.out.print("System.out.println("s.length.out.println("第1s.charAt(0System.out.println("s.charAt(s.length()-1}}importScannerimportScanner;publicclassSubStringDemo{publicstaticvoidmain(String[]args){ScannerinputnewScanner(System.in.out.print("1);Strings1input.nextLineSystem.out.print("2);Strings2input.nextLineSystem.out.println("s2(s1.indexOf(s20是")s1"}}使?下??法簽名編寫?個?法,統(tǒng)計字符串中包含字母的個數(shù)。publicclasspublicclassCountLetters{publicstaticintcountLetters(Strings){intcount=0;for(inti=0;i<s.length();i++){if(Character.isLetter(s.charAt(i))){count++;}}returncount;}publicstaticvoidmain(String[]args){Strings="1adAA24e%$#@R";System.out.println("scountLetters(s"}}編寫?法將10進制整數(shù)轉(zhuǎn)換為2進制整數(shù)。importimportScanner;publicclassToBinary{publicstaticStringtoBinary(intvalue){StringBuilders=newStringBuilder("");while(value>0){s.append(value%2);value/=2;}s.reverse();returnnewString(s);}publicstaticvoidmain(String[]args){ScannerinputnewScanner(System.in.out.print("numinput.nextIntSystem.out.println("?進制數(shù)numtoBinary(num}}importArraysimportArrays;publicclassSort{publicstaticStringsort(Strings){char[]c=s.toCharArray();Arrays.sort(c);returnString.valueOf(c);}publicstaticvoidmain(String[]args){Strings="morning";System.out.println(sort(s));}}importScannerimportScanner;publicclassEncrypt{publicstaticStringencrypt(Strings){StringBuilderss=newStringBuilder(s);for(inti=0;i<ss.length();i++){charc=ss.charAt(i);if(s.charAt(i)=='Z'||s.charAt(i)=='z'){c=(char)(c-25);}else{c=(char)(c+1);}ss.setCharAt(i,c);}returnnewString(ss);}publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);System.out.print("Strings=input.nextLine();System.out.println("encrypt(s}}為上?題編寫?個解密程序,輸?密?,輸出明?。importimportScanner;publicclassEncrypt1{publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);System.out.print("請輸?已經(jīng)加密的字符串:");Strings=input.nextLine();StringBuilderss=newStringBuilder(s);for(inti=0;i<ss.length();i++){charc=ss.charAt(i);if(ss.charAt(i)=='A'||ss.charAt(i)=='a'){c=(char)(c+25);}else{c=(char)(c-1);}ss.setCharAt(i,c);}System.out.println("ss}}將字符串“nopains,nogains.“解析成含有4個單詞的字符串?dāng)?shù)組。publicclasspublicclassSplitDemo{publicstaticvoidmain(String[]args){Strings=newString("nopains,nogins.");String[]array=s.split("[,.]");for(inti=0;i<array.length;i++){System.out.print(array[i]+"");}}}第7章繼承與多態(tài)7.1根據(jù)Animal類及其?類的繼承關(guān)系,編寫?法實現(xiàn)這些類。classclassBirdextendsAnimal{publicintnumberOfWings;publicvoidfly(){System.out.println("Icanfly");}}classFishextendsAnimal{publicintnumberOfFins;publicvoidswim(){System.out.println("Icanswim");}}classDogextendsAnimal{publicintnumberOfLegs;publicvoidwalk(){System.out.println("Icanwalk");}}publicclassAnimal{publicdoubleweight;publicvoideat(){System.out.println("Icaneatanything");}publicstaticvoidmain(String[]args){Animalanimal=newAnimal();animal.eat();Birdbird=newBird();bird.fly();bird.eat();}}import Scanner;classCircle{doublecenterXdoublecenterYpublicdoubleradius;publicCircle(doubleradius){this.radius=radius;}publicvoidsetRadius(doubleradius){this.radius=radius;}publicdoublegetRadius(){returnradius;}publicdoublegetArea(){returnMath.PI*radius*radius;}publicdoublegetPerimeter(){return2*Math.PI*radius}}publicclassCylinderextendsCircle{doubleheight;publicCylinder(doubleradius,doubleheight){super(radius);this.height=height;}@OverridepublicdoublegetArea(){returngetPerimeter()*height;}publicdoublegetVolume(){returngetArea()*height;}publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);System.out.print("doubleradiusinput.nextDouble.out.print(");doubleheightinput.nextDoubleCylindercylinder=newCylinder(radius,height);System.out.println("cylinder.getArea.out.println("cylinder.getVolume}}定義?個汽車類Auto,定義?個Auto的?類Bus,編寫程序測試Bus類的使?。classclassBusextendsAuto{publicintpassenger;publicvoidgotOn(intpassenger){this.passenger+=passenger;System.out.println("車上上了"+passenger+"名乘客,"+"現(xiàn)在車上有"+this.passenger+"名乘客");}publicvoidgetOff(intpassenger){this.passenger-=passenger;System.out.println("車上下了"+passenger+"名乘客,"+"現(xiàn)在車上有"+this.passenger+"名乘客");}}publicclassAuto{publicdoublespeed;publicvoidstart(){System.out.println("汽車啟動了");}publicvoidspeedUp(doublespeed){this.speed=speed;System.out.println("汽車加速到"+speed);}publicvoidstop(){this.speed=0;System.out.println("汽車停?運?");}publicstaticvoidmain(String[]args){Busbus=newBus();bus.start();bus.speedUp(60.0);bus.gotOn(20);bus.getOff(10);bus.stop();}}定義?個名為Square類表?正?形,使其繼承Shape抽象類,覆蓋Shape中的抽象?法。abstractclassabstractclassShape{publicStringname;publicShape(){}publicShape(Stringname){=name;}publicabstractdoublegetPerimeter();publicabstractdoublegetArea();}publicclassSquareextendsShape{doubleside;publicSquare(doubleside){this.side=side;}@OverridepublicdoublegetPerimeter(){returnside*4;}@OverridepublicdoublegetArea(){returnside*side;}publicstaticvoidmain(Stringargs){SquaresquarenewSquare(2.0.name正?形;System.out.println(square.getPerimeter.out.println(square.getArea}}定義名為Cuboid的長?體類,繼承長?形類Rectangle,編寫程序,求長寬?分別為10,5,2的長?體的體積。classclassRectangle{publicdoublelength;publicdoublewidth;publicRectangle(doublelength,doublewidth){this.length=length;this.width=width;}publicdoublegetArea(){returnlength*width;}publicdoublegetPerimeter(){return(length+width)*2;}}publicclassCuboidextendsRectangle{publicdoubleheight;publicCuboid(doublelength,doublewidth,doubleheight){super(length,width);this.height=height;}publicdoublegetVolume(){returngetArea()*height;}publicstaticvoidmain(String[]args){Cuboidcuboid=newCuboid(10,5,2);System.out.println("cuboid.getVolume}}第8章Java常?核?類定義?個名為Square的類表?正?形,編寫?個程序測試clone(),equals(),toString()等?法的使?。publicclasspublicclassSquareimplementsCloneable{publicdoublelength;publicSquare(doublelength){this.length=length;}publicvoidsetLength(doublelength){this.length=length;}publicdoublegetLength(){returnlength;}publicbooleanequals(Squaresquare){returnsquare.length==this.length;}@OverridepublicStringtoString(){return"Square[length="+length+"]";}publicstaticvoidmain(String[]args)throwsCloneNotSupportedException{Squaresquare=newSquare(100);Squaresquare1=(Square)square.clone();System.out.println(square1.toString());System.out.println(square.equals(square1));}}1000個16數(shù)出現(xiàn)的概率,修改程序,?成1000個隨機數(shù),并統(tǒng)計概率,?較結(jié)果并給出結(jié)論。publicclasspublicclassRandomTest{publicstaticvoidmain(String[]args){int[]count1=newint[6];int[]count2=newint[6];for(inti=0;i<100;i++){intr=(int)(Math.random()*6)+1;switch(r){case1:count1[0]++;break;case2:count1[1]++;break;case3:count1[2]++;break;case4:count1[3]++;break;case5:count1[4]++;break;case6:count1[5]++;break;}}for(inti=0;i<1000;i++){intr=(int)(Math.random()*6)+1;switch(r){case1:count2[0]++;break;case2:count2[1]++;break;case3:count2[2]++;break;case4:count2[3]++;break;case5:count2[4]++;break;case6:count2[5]++;break;}}for(inti=0;i<count1.length;i++){System.out.print(count1[i]+"");}System.out.println();for(inti=0;i<count2.length;i++){System.out.print(count2[i]+"");}}}有?個三?形的兩條邊長分別為4.0和5.0,夾?為30°,編寫程序計算該三?形的?積。publicclasspublicclassAreaTest{publicstaticvoidmain(String[]args){System.out.println(0.5*4*5*Math.sin(Math.toRadians(30)));}}編寫程序,輸出6中數(shù)值型包裝類的最?值和最?值。publicclasspublicclassNumber

溫馨提示

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

評論

0/150

提交評論