2022年java实用教程课后习题答案 .pdf





《2022年java实用教程课后习题答案 .pdf》由会员分享,可在线阅读,更多相关《2022年java实用教程课后习题答案 .pdf(22页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、3. 编写应用程序,求1!+2!+,+10! 。答: class Fact public static void main(String args) int fact,sum=0; for(int i=1;i=10;i+) fact=1; for(int j=1;j=i;j+) fact*=j; sum+=fact; System.out.println(1到10的 阶 乘 之 和是:+sum); 4. 编写一个应用程序,求100 以内的全部素数。答: class Primes public static void main(String args) int w=1; for(int i=2;
2、i=100;i+) for(int j=2;ji;j+) w=i%j; if(w=0) break; if(w!=0) System.out.println(i+是素数 ); 5. 分别用 dowhile和 for 循环计算 1+1/2 !+1/3 !+1/4 !+,的前 20 项和。答: for循环class Sum public static void main(String args) int fact; double sum=0; for(int i=1;i=20;i+) fact=1; for(int j=1;j=i;j+) fact*=j; sum+=1.0/fact; Syste
3、m.out.println(sum); dowhile 循环class Sum public static void main(String args) int i=1; int fact; double sum=0; do fact=1; int j=0; while(+j=i) fact*=j; sum+=1.0/fact; while(+i=20); System.out.println(sum); 6. 一个数如果恰好等于它的因子之和,这个数就称为“完数”。编写应用程序,求1000 之内的所有完数。答: class Wanshu public static void main(Stri
4、ng args) int x,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+是完数 ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 22 页 - - - - - - - - - 7. 编写应用程序, 分别使用while 和 for循环计算 8+88+888+,前 10 项之和。答: for循环class TheSum p
5、ublic static void main(String args) long sum=0,data=8; for(int i=1;i=10;i+) sum=sum+data; data=data*10+8; System.out.println(sum); while 循环class TheSum public static void main(String args) long sum=0,data=8,i=0; while(+i=10) sum=sum+data; data=data*10+8; System.out.println(sum); 8. 编写应用程序, 输出满足1+2+3
6、+, +n8888 的最大正整数n。答: class Maxn public static void main(String args) int k=1,sum=0; while(sum+=k)8888) k+; k-; System.out.println(最大能取到 :+k); 15. 模仿例子 4.27 ,编写一个类实现两个接口的程序。答: interface 表面积 double allArea(double r); interface 体积 double volu(double r); class Sph implements 表面积 , 体积 double PI=3.14159;
7、public double allArea(double r) return 4*PI*r*r; public double volu(double r) return 4.0/3*PI*r*r*r; public class Test public static void main(String args) double r=5.0; Sph a=new Sph(); System.out.println(半径为5 的球的表面积是 :+a.allArea(r); System.out.println(半径为5 的球的体积是 :+a.volu(r); 名师资料总结 - - -精品资料欢迎下载
8、- - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 22 页 - - - - - - - - - 16. 编写一个类A, 该类创建的对象可以调用方法f 输出英文字母表,然后再编写一个该类的子类B,要求子类B 必须继承A类的方法 f( 不允许重写 ) , 子类创建的对象不仅可以调用方法f 输出英文字母表, 而且调用子类新增的方法g 输出希腊字母表。答: class A int m; void f() for(m=65;m91;m+) System.out.print(char)m+ ); for(m=97;m123;m
9、+) System.out.print(char)m+ ); System.out.println( ); class B extends A int i; void g() 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;i1000) MyException exception=new MyException(m); throw except
10、ion; else System.out.println(m); public class Test public static void main(String agrs) int m; Student stu1=new Student(); m=987; try stu1.speak(m); m=1234; stu1.speak(m); catch(MyException e) e.showStr1(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 22 页 -
11、- - - - - - - - 18. 编写一个类,该类有一个方法public int f(int a,int b) ,该方法返回a 和 b 的最大公约数。然后再编写一个该类的子类,要求子类重写方法f,而且重写的方法将返回a 和 b 的最小公倍数。要求在重写的方法的方法体中首先调用被隐藏的方法返回 a 和 b 的最大公约数m ,然后将乘积 (a*b)/m返回。要求在应用程序的主类中分别使用父类和子类创建对象,并分别调用方法f 计算两个正整数的最大公约数和最小公倍数。答:class A public int f(int a,int b) if(ab) int temp=0; temp=a; a=
12、b; b=temp; int r=a%b; while(r!=0) a=b; b=r; r=a%b; return b; class B extends A public int f(int a,int b) int m; m=super.f(a,b); return (a*b)/m; public class Test public static void main(String args) A a=new A(); System.out.println(18和 102 的最大公约数是 :+a.f(18,102); B b=new B(); System.out.println(18和 10
13、2 的最小公倍数是 :+b.f(18,102); 1. 使用 String类的 public String toUpperCase() 方法可以将一个 字 符串 中 的 小 写字 母变成 大写 字 母 , 使 用public String toLowerCase()方法可以将一个字符串中的大写字母变成小写字母。编写一个程序,使用这两个方法实现大小写的转换。答: class Test public static void main(String args) String str=I can use Java; System.out.println(要转换的字符串是 :+str); String
14、s=str.toUpperCase(); System.out.println(转换成大写字符串是 :+s); s=str.toLowerCase(); System.out.println(转换成小写字符串是 :+s); 2. 使用 String类的 public String concat(String str)方法可以把调用该方法的字符串与参数指定的字符串连接,把 str指定的串连接到当前串的尾部获得一个新的串。编写一个程序通过连接两个串得到一个新串,并输出这个新串。答: class Test public static void main(String args) String str
15、1=I can u; String str2=se Java; String s=str1.concat(str2); System.out.println(将 字符串 +str1+与 字符串 +str2+连接后得到的新字符串是:); System.out.println(s); 1. 用 Data 类不带参数的构造方法创建日期,要求日期的输出格式是 : 星期 小时分 秒。答:import java.util.*; import java.text.*; class Test public static void main(String args) Date 时间 =new Date(); S
16、impleDateFormat s=new SimpleDateFormat(E HH时 mm 分 ss秒); System.out.println(s.format(时间 ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 22 页 - - - - - - - - - 3. 计算某年、 某月、某日和某年、 某月、 某日之间的天数间隔。要求年、月、日通过main 方法的参数传递到程序中。答:import java.util.*; class Test public st
17、atic void main(String args) Calendar c=Calendar.getInstance(); c.set(2000,0,1); long time1=c.getTimeInMillis(); c.set(2008,7,8); long time2=c.getTimeInMillis(); long dayCous=(time2-time1)/(1000*60*60*24); System.out.println(2008年 8 月 8 日和2000 年 1 月 1 日相隔 +dayCous+ 天); 5. 使用 BigInteger类计算 1!+3!+5!+7!
18、+,的前30 项的和。答: import java.math.*; public class Test public static void main(String args) BigInteger sum=new BigInteger(0), jieCheng=new BigInteger(1), ONE=new BigInteger(1), i=ONE; int k=0; while(+k=30) sum=sum.add(jieCheng); i=i.add(ONE); jieCheng=jieCheng.multiply(i); i=i.add(ONE); jieCheng=jieChe
19、ng.multiply(i); System.out.println(sum); 4. 编写应用程序,有一个标题为“计算的窗口”的窗口,窗口的布局为FlowLayout布局。窗口中添加两个文本区,当我们在一个文本区中输入若干个数时,另一个文本区,同时对输入的数进行求和运算并求出平均值,也就是说随着输入的变化,另一个文本区不断地更新求和及平均值。答:import java.awt.*; import java.awt.event.*; import java.util.*; class Calculated extends Frame implements TextListener TextAr
20、ea text1,text2; /定义了 2 个文本区Calculated(String s) /标题为“计算的窗口”的窗口 super(s); setLayout(new FlowLayout(); /窗口布局为 FlowLayout text1=new TextArea(5,23); text2=new TextArea(5,23); add(text1); add(text2); text2.setEditable(false); /显示求和结果和平均值的文本区禁止编辑text1.addTextListener(this); addWindowListener(new WindowAda
21、pter() public void windowClosing(WindowEvent e) System.exit(0); ); setBounds(100,100,400,160); setVisible(true); validate(); public void textValueChanged(TextEvent e) String s=text1.getText(); StringTokenizer geshu=new StringTokenizer(s); int n=geshu.countTokens(); double a=new doublen; for(int i=0;
22、in;i+) String temp=geshu.nextToken(); double date=Double.parseDouble(temp); ai=date; double sum=0,average; for(int i=0;i0) String str1=text1.getText()+n+text2.getText()+n+text3.getText()+n; String str2=textarea.getText(); textarea.setText(str2+str1); else text2.setText(输 入 了 非 法 格 式 的E-mail地址 ); pub
23、lic class Test public static void main(String args) new WindowBox(); 8. 写一个应用程序,要求编写一个Panel 的子类MyPanel,MyPanel 中有一个文本框和一个按钮,要求 MyPanel 的实例作为其按钮的ActionEvent事件的监视器,当单击按钮时,程序获取文本框中的文本,并将该文本作为按钮的名称。然后在 编 写 一 个Frame 的 子 类 , 即 窗 口 。 窗 口 的 布 局 为BorderLayout布局。窗口中添加两个MyPanel 面板,分别添加到窗口的东部区域和西部区域。答:import ja
24、va.awt.*; import java.awt.event.*; class MyPanel extends Panel implements ActionListener String name; TextField text; Button button; MyPanel() text=new TextField(10); button=new Button(确定 ); add(text); add(button); button.addActionListener(this); addWindowListener(new WindowAdapter() public void win
25、dowClosing(WindowEvent e) System.exit(0); ); public void actionPerformed(ActionEvent e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 22 页 - - - - - - - - - if(e.getSource()=button) name=text.getText(); button.setLabel(name); class MyFrame extends Frame MyPane
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年java实用教程课后习题答案 2022 java 实用教程 课后 习题 答案

限制150内