耿祥义张跃平java大学实用教程-编程题答案

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

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

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

资源描述

习题解答第一章作业题1.publicclassHello{publicstaticvoidmain(Stringargs[]){System.out.pintln(“早上好,goodMorning”);}}2.importjava.applet.*;importjava.awt.*;publicclassBoyextendsApplet{publicvoidpaint(Graphicsg){g.setColor(Color.blue);g.drawString(你好,hello,12,30);}}第二章作业题1.publicclassZuoYe2_1{publicstaticvoidmain(Stringargs[]){charc='а';System.out.println(字母+c+在unicode表中的顺序位置:+(int)c);System.out.println(字母表:);while(c='я'){System.out.print(+c);c=(char)(c+1);}}}2.importjava.util.*;publicclassZuoYe2_2{publicstaticvoidmain(Stringargs[]){Scannerreader=newScanner(System.in);longchengji=1;intm=0;while(reader.hasNextInt()){intx=reader.nextInt();m=m+1;chengji=chengji*x;}System.out.println(m+个数的乘积为+chengji);}}第三章作业题1.importjava.util.*;publicclassZuoYe3_1{publicstaticvoidmain(Stringargs[]){Scannerreader=newScanner(System.in);doubley=0,x=0;x=reader.nextDouble();if(x0)y=-1+2*x;elseif(x==0)y=-1;elseif(x0)y=-1+3*x;System.out.println(y);}}2.publicclassZuoYe3_2{publicstaticvoidmain(Stringargs[]){intsum=0,m=3,n=7,a=1;while(a=1000){if(a%m==0&&a%n==0)sum=sum+a;a++;}System.out.println(sum=+sum);}}3.publicclassZuoYe3_3{publicstaticvoidmain(Stringargs[]){longsum=0,a=8,item=a,n=10,i=1;for(i=1;i=n;i++){sum=sum+item;item=item*10+a;}System.out.println(sum);}}4.publicclassZuoYe3_4{publicstaticvoidmain(Stringargs[]){doublesum=0,a=1,b=1,fuhao=1,item=a/b;inti=1;while(i=1000){sum=sum+fuhao*item;i++;fuhao=fuhao*(-1);b=b+2;item=a/b;}System.out.println(sum=+sum);}}5.publicclassZuoYe3_5{publicstaticvoidmain(Stringargs[]){doublesum=0,a=1;inti=1;while(i=20){if(i=10)sum=sum+a;i++;a=a*i;}System.out.println(sum=+sum);}}8.第四章作业题1.classDengCha{intstart,d;DengCha(){}DengCha(intstart,intd){this.start=start;this.d=d;}voidsetStart(ints){start=s;}voidsetD(intd){this.d=d;}intgetSum(intn){intsum=0,i=1;while(i=n){sum=sum+start;start=start+d;i++;}returnsum;}}publicclassZuoYe4_1{publicstaticvoidmain(Stringargs[]){DengChashulie=newDengCha(2,3);System.out.println(shulie.getSum(100));shulie.setStart(10);shulie.setD(5);System.out.println(shulie.getSum(9));}}2.classLetter{publicvoidprintLetter(){for(charc='a';c='z';c++)System.out.print(+c);}}publicclassZuoYe4_2{publicstaticvoidmain(Stringargs[]){Letterp=newLetter();p.printLetter();}}3.classSquareEquation{doublea,b;staticdoublec;doubleroot1,root2;booleanboo;publicSquareEquation(doublea,doubleb,doublec){this.a=a;this.b=b;SquareEquation.c=c;if(a!=0){boo=true;}else{boo=false;}}publicvoidgetRoots(){if(boo){System.out.println(是一元2次方程);doubledisk=b*b-4*a*c;if(disk=0){root1=(-b+Math.sqrt(disk))/(2*a);root2=(-b-Math.sqrt(disk))/(2*a);System.out.printf(方程的根:%f,%f\n,root1,root2);}else{System.out.printf(方程没有实根\n);}}else{System.out.println(不是一元2次方程);}}publicvoidsetCoefficient(doublea,doubleb,doublec){this.a=a;this.b=b;SquareEquation.c=c;if(a!=0){boo=true;}else{boo=false;}}}publicclassZuoYe4_3{publicstaticvoidmain(Stringargs[]){SquareEquationequation1=newSquareEquation(4,5,1);SquareEquationequation2=newSquareEquation(3,5,-7);equation1.getRoots();equation2.getRoots();}}4.importjava.util.Scanner;classA{intf(intm,intn){if(m*n0){System.out.println(有负数,程序退出);System.exit(0);}if(mn){inttemp=m;m=n;n=temp;}inta=m,b=n;intr=m%n;while(r!=0){m=n;n=r;r=m%n;}returnn;}}classB{Aa;B(){a=newA();}intg(intm,intn){inttemp=a.f(m,n);returnm*n/temp;}}publicclassZuoYe4_4{publicstaticvoidmain(Stringargs[]){Scannerreader=newScanner(System.in);System.out.println(输入2个正整数,程序计算出它们的最大公约数和最小公倍数);System.out.print(输入第一个整数:);intm=reader.nextInt();System.out.print(输入第二个整数:);intn=reader.nextInt();Aa=newA();Bb=newB();System.out.println(m+和+n+的最大公约数是+a.f(m,n));System.out.println(m+和+n+的最小公倍数是+b.g(m,n));}}5.importjava.applet.Applet;importjava.awt.*;publicclassExample4_10extendsApplet{Buttonredbutton;publicvoidinit(){redbutton=newButton(我是一个红色的按钮);redbutton.setBackground(Color.red);redbutton.setForeground(Color.white);add(redbutton);}}第五章作业题1.importjava.util.Scanner;classA{publicintf(intm,intn){if(mn){inttemp=m;m=n;n=temp;}intr=m%n;while(r!=0){m=n;n=r;r=m%n;}returnn;}}classBextendsA{publicintf(intm,intn){intdivision=super.f(m,n);return(m*n)/division;}}publicclassZuoYe5_1{publicstaticvoidmain(Stringargs[]){Aa=newA();Bb=newB();Scannerreader=newScanner(System.in);System.out.println(输入2个整数,程序计算出它们的最大公约数和最小公倍数);System.out.print(输入第一个整数:);intm=reader.nextInt();System.out.print(输入第二个整数:);intn=reader.nextInt();if(m*n0){System.out.println(有负数,程序退出);System.exit(0);}System.out.printf(%d和%d的最大公约数是%d\n,m,n,a.f(m,n));System.out.printf(%d和%d的最小公倍数是%d\n,m,n,b.f(m,n));}}2.abstractclassA{publicabstractvoidf(intx);publicabstractvoidg(intx,inty);publicabstractdoubleh(doublex);}classA1extendsA{publicvoidf(intx){System.out.println(x);}publicvoidg(intx,inty){intz=x+y;System.out.println(z);}publicdoubleh(doublex){returnx*x;}}classA2extendsA{publicvoidf(intx){System.out.println(Hello:+x);}publicvoidg(intx,inty){intz=x-y;System.out.println(z);}publicdoubleh(doublex){returnMath.sqrt(x);}}classA3extendsA{publicvoidf(intx){System.out.println(你好:+x);}publicvoidg(intx,inty){doublez=(double)x/y;System

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

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

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

×
保存成功