Java2实用教程(第三版)课后习题参考答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

,1Java1.Java1Java:Edit.java2Java:Javajavac.exe*.class3Java:JavaJavajava.exeJavaJava2.publicmainmainpublicpublic3.JavapublicJavaJavapublicpublic:.javapublic,public.javapublic.java:.java4.HTMLcodebasecodebaseHTMLC:\5000HTMLcodebase:appletcode=width=200height=300codebase=C:\5000/applet:Javaimportjava.applet.*;importjava.awt.*;publicclassBoyextendsApplet{publicvoidpaint(Graphicsg){g.setColor(Color.red);g.drawString(Java,5,30);g.setColor(Color.blue);g.drawString(,10,50);}}Boy.javaBoy.classC:\5000HTML:appletcode=Boy.classwdith=200height=300codebase=C:\5000/appletBoy.htmlC:\5000C:\1000Boy.html21.2.5Javaclassbreakpublicreturnstaticextends3.Javaboolean()char()floatdouble()byteshortintlong()4.:intx=8;byteb=127;b=x;:b=x,b=(byte)x;5.publicclassE{publicstaticvoidmain(Stringargs[]){long[]a={1,2,3,4};long[]b={100,200,300,400,500};b=a;System.out.println(b:+b.length);System.out.println(b[0]=+b[0]);}}:b:4b[0]=16.publicclassE{publicstaticvoidmain(Stringargs[]){for(inti=20302;i20322;i++){System.out.println((char)i);}}}:7.System.out.println()System.out.println()+:System.out.println(+x)System.out.println(:+123++122)System.out.print()System.out.println()publicclassOutputData{publicstaticvoidmain(Stringargs[]){intx=234,y=432;System.out.println(x++(2*x));System.out.print();System.out.println();System.out.println(x+y=+(x+y));System.out.println(+x+y+=234432);}}:234468x+y=666234432=2344328.Java:publicclassa{publicstaticvoidmain(Stringargs[]){System.out.println();}}31.publicclassE{publicstaticvoidmain(Stringargs[]){charx='',y='e',z='';if(x'A'){y='';z='';}elsey='';z='';System.out.println(+x+y+z);}}:2.publicclassE3{publicstaticvoidmain(Stringargs[]){charc='\0';for(inti=1;i=4;i++){switch(i){case1:c='b';System.out.print(c);case2:c='e';System.out.print(c);break;case3:c='p';System.out.print(c);default:System.out.print(!);}}}}:beep3.1+2+,+10:classFact{publicstaticvoidmain(Stringargs[]){intfact,sum=0;for(inti=1;i=10;i++){fact=1;for(intj=1;j=i;j++)fact*=j;sum+=fact;}System.out.println(110:+sum);}}4.100:classPrimes{publicstaticvoidmain(Stringargs[]){intw=1;for(inti=2;i=100;i++){for(intj=2;ji;j++){w=i%j;if(w==0)break;}if(w!=0)System.out.println(i+);}}}5.dowhilefor1+1/2+1/3+1/4+,20:forclassSum{publicstaticvoidmain(Stringargs[]){intfact;doublesum=0;for(inti=1;i=20;i++){fact=1;for(intj=1;j=i;j++)fact*=j;sum+=1.0/fact;}System.out.println(sum);}}dowhileclassSum{publicstaticvoidmain(Stringargs[]){inti=1;intfact;doublesum=0;do{fact=1;intj=0;while(++j=i)fact*=j;sum+=1.0/fact;}while(++i=20);System.out.println(sum);}}6.1000:classWanshu{publicstaticvoidmain(Stringargs[]){intx,i,sum;for(x=1;x=1000;x++){sum=0;for(i=1;ix;i++){if(x%i==0)sum+=i;}if(x==sum)System.out.println(x+);}}}7.whilefor8+88+888+,10:forclassTheSum{publicstaticvoidmain(Stringargs[]){longsum=0,data=8;for(inti=1;i=10;i++){sum=sum+data;data=data*10+8;}System.out.println(sum);}}whileclassTheSum{publicstaticvoidmain(Stringargs[]){longsum=0,data=8,i=0;while(++i=10){sum=sum+data;data=data*10+8;}System.out.println(sum);}}8.1+2+3+,+n8888n:classMaxn{publicstaticvoidmain(Stringargs[]){intk=1,sum=0;while((sum+=k)8888)k++;k--;System.out.println(:+k);}}41.:2.:3.(static)(static):4.:5.protected:protected6.::staticstatic:classA{intx;//staticinty;//}::A1.x=10;A1.y=20;A2.x10A2.y:classA{intx;staticinty;staticvoidfunc(){b=10;//a=20;//}}:classA{intx;staticinty;}classB{publicvoidfunc();{A.x=10;//A.y=20;//}}7.:privateprotectedpublic8.final:final9.abstract:abstractabstract10.:BABbAaAab11.:12.publicclassTakecare{inta=90;staticfloatb=10.98f;publicstaticvoidmain(Stringargs[]){floatc=a+b;System.out.println(c=+c);}}:floatc=a+b;maina13.classA{publicintf(intx){returnx+1;}}classBextendsA{publicintf(intx){returnx*x;}}publicclassE{publicstaticvoidmain(Stringargs[]){Aa=newB();intm=a.f(10);System.out.println(m);}}:10014.classA{intx;publicvoidsetX(intx){this.x=x;}intgetX(){returnx;}}classB{publicvoidf(Aa){a.setX(100);}}publicclassE{publicstaticvoidmain(Stringargs[]){Aa=newA();a.setX(8);System.out.println(a.getX());Bb=newB();b.f(a);System.out.println(a.getX());}}:810015.4.27:implementsabstractpublicvoidreturninterface{doubleallArea(doubler);}interface{doublevolu(doubler);}classSphimplements,{doublePI=3.14159;publicdoubleallArea(doubler){return4*PI*r*r;}publicdoublevolu(doubler){return4.0/3*PI*r*r*r;}}publicclassTest{publicstaticvoidmain(Stringargs[]){doubler=5.0;Spha=newSph();System.out.println(5:+a.allArea(r));System.out.println(5:+a.volu(r));}}16.AfBBAf()fg:classA{intm;voidf(){for(m=65;m91;m++)System.out.print((char)m+);for(m=97;m123;m++)System.out.print((char)m+);System.out.println();}}classBextendsA{inti;voidg(){for(i=913;i930;i++)System.out.print((char)i+);for(i=931;i938;i++)System.out.print((char)i+);for(i=945;i962;i++)System.out.print((char)i+);for(i=963;i970;i++)System.out.print((char)i+);System.out.println();}}publicclassTest{publicstaticvoidmain(Stringargs[]){Bb=newB();System.out.println(f:);b.f();System.out.println(g:);b.g();}}17.MyExceptionStudentpublicvoidspeak(intm)throwsMyExceptionm1

1 / 44
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功