java2实用教程课后习题答案(第三版编程题)

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

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

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

资源描述

3.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);}15.4.27:interface{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)throwsMyExceptionm1000MyExceptionmainStudentspeak:classMyExceptionextendsException{Stringstr1;MyException(intm){str1=m+1000;}publicvoidshowStr1(){System.out.println(str1);}}classStudent{publicvoidspeak(intm)throwsMyException{if(m1000){MyExceptionexception=newMyException(m);throwexception;}elseSystem.out.println(m);}}publicclassTest{publicstaticvoidmain(Stringagrs[]){intm;Studentstu1=newStudent();m=987;try{stu1.speak(m);m=1234;stu1.speak(m);}catch(MyExceptione){e.showStr1();}}18.publicintf(inta,intb)abfababm(a*b)/mf:classA{publicintf(inta,intb){if(ab){inttemp=0;temp=a;a=b;b=temp;}intr=a%b;while(r!=0){a=b;b=r;r=a%b;}returnb;}}classBextendsA{publicintf(inta,intb){intm;m=super.f(a,b);return(a*b)/m;}}publicclassTest{publicstaticvoidmain(Stringargs[]){Aa=newA();System.out.println(18102:+a.f(18,102));Bb=newB();System.out.println(18102:+b.f(18,102));}}1.StringpublicStringtoUpperCase()publicStringtoLowerCase():classTest{publicstaticvoidmain(Stringargs[]){Stringstr=IcanuseJava;System.out.println(:+str);Strings=str.toUpperCase();System.out.println(:+s);s=str.toLowerCase();System.out.println(:+s);}}2.StringpublicStringconcat(Stringstr)str:classTest{publicstaticvoidmain(Stringargs[]){Stringstr1=Icanu;Stringstr2=seJava;Strings=str1.concat(str2);System.out.println(+str1++str2+:);System.out.println(s);}}1.Data::importjava.util.*;importjava.text.*;classTest{publicstaticvoidmain(Stringargs[]){Date=newDate();SimpleDateFormats=newSimpleDateFormat(EHHmmss);System.out.println(s.format());}}3.main:importjava.util.*;classTest{publicstaticvoidmain(Stringargs[]){Calendarc=Calendar.getInstance();c.set(2000,0,1);longtime1=c.getTimeInMillis();c.set(2008,7,8);longtime2=c.getTimeInMillis();longdayCous=(time2-time1)/(1000*60*60*24);System.out.println(200888200011+dayCous+);}}5.BigInteger1+3+5+7+,30:importjava.math.*;publicclassTest{publicstaticvoidmain(Stringargs[]){BigIntegersum=newBigInteger(0),jieCheng=newBigInteger(1),ONE=newBigInteger(1),i=ONE;intk=0;while(++k=30){sum=sum.add(jieCheng);i=i.add(ONE);jieCheng=jieCheng.multiply(i);i=i.add(ONE);jieCheng=jieCheng.multiply(i);}System.out.println(sum);}}4.FlowLayout:importjava.awt.*;importjava.awt.event.*;importjava.util.*;classCalculatedextendsFrameimplementsTextListener{TextAreatext1,text2;//2Calculated(Strings)//{super(s);setLayout(newFlowLayout());//FlowLayouttext1=newTextArea(5,23);text2=newTextArea(5,23);add(text1);add(text2);text2.setEditable(false);//text1.addTextListener(this);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setBounds(100,100,400,160);setVisible(true);validate();}publicvoidtextValueChanged(TextEvente){Strings=text1.getText();StringTokenizergeshu=newStringTokenizer(s);intn=geshu.countTokens();doublea[]=newdouble[n];for(inti=0;in;i++){Stringtemp=geshu.nextToken();

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

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

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

×
保存成功