耿祥义张跃平java大学实用教程-编程题答案.doc_第1页
耿祥义张跃平java大学实用教程-编程题答案.doc_第2页
耿祥义张跃平java大学实用教程-编程题答案.doc_第3页
耿祥义张跃平java大学实用教程-编程题答案.doc_第4页
耿祥义张跃平java大学实用教程-编程题答案.doc_第5页
已阅读5页,还剩44页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

习题解答第一章 作业题1public class Hello public static void main (String args ) System.out.pintln(“早上好,good Morning”); 2import java.applet.*;import java.awt.*;public class Boy extends Applet public void paint(Graphics g) g.setColor(Color.blue); g.drawString(你好,hello,12,30); 第二章 作业题1public class ZuoYe2_1 public static void main (String args ) char c=; System.out.println(字母+c+在unicode表中的顺序位置:+(int)c); System.out.println(字母表:); while(c=) System.out.print( +c); c=(char)(c+1); 2import java.util.*;public class ZuoYe2_2 public static void main (String args ) Scanner reader=new Scanner(System.in); long chengji=1; int m=0; while(reader.hasNextInt() int x=reader.nextInt(); m=m+1; chengji=chengji*x; System.out.println(m+个数的乘积为+chengji); 第三章 作业题1import java.util.*;public class ZuoYe3_1 public static void main (String args ) Scanner reader=new Scanner(System.in); double y=0,x=0; x=reader.nextDouble(); if(x0) y=-1+3*x; System.out.println(y); 2public class ZuoYe3_2 public static void main(String args) int sum=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); 3public class ZuoYe3_3 public static void main(String args) long sum=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); 4public class ZuoYe3_4 public static void main(String args) double sum=0,a=1,b=1,fuhao=1,item=a/b; int i=1; while(i=1000) sum=sum+fuhao*item; i+; fuhao=fuhao*(-1); b=b+2; item=a/b; System.out.println(sum=+sum); 5public class ZuoYe3_5 public static void main(String args) double sum=0,a=1; int i=1; while(i=10) sum=sum+a; i+; a=a*i; System.out.println(sum=+sum); 8第四章 作业题1class DengCha int start,d; DengCha() DengCha(int start,int d) this.start=start; this.d=d; void setStart(int s) start=s; void setD(int d) this.d=d; int getSum(int n) int sum=0,i=1; while(i=n) sum=sum+start; start=start+d; i+; return sum; public class ZuoYe4_1 public static void main (String args ) DengCha shulie=new DengCha(2,3); System.out.println(shulie.getSum(100); shulie.setStart(10); shulie.setD(5); System.out.println(shulie.getSum(9); 2 class Letter public void printLetter() for(char c=a;c=0) root1=(-b+Math.sqrt(disk)/(2*a); root2=(-b-Math.sqrt(disk)/(2*a); System.out.printf(方程的根:%f,%fn,root1,root2); else System.out.printf(方程没有实根n); else System.out.println(不是一元2次方程); public void setCoefficient(double a,double b,double c) this.a=a; this.b=b; SquareEquation.c=c; if(a!=0) boo=true; else boo=false; public class ZuoYe4_3 public static void main(String args ) SquareEquation equation1=new SquareEquation(4,5,1); SquareEquation equation2=new SquareEquation(3,5,-7); equation1.getRoots(); equation2.getRoots(); 4import java.util.Scanner;class A int f(int m,int n) if(m*n0) System.out.println(有负数,程序退出); System.exit(0); if(mn) int temp=m; m=n; n=temp; int a=m,b=n; int r=m%n; while(r!=0) m=n; n=r; r=m%n; return n; class B A a; B() a=new A(); int g(int m,int n) int temp=a.f(m,n); return m*n/temp; public class ZuoYe4_4 public static void main (String args ) Scanner reader=new Scanner(System.in); System.out.println(输入2个正整数,程序计算出它们的最大公约数和最小公倍数); System.out.print(输入第一个整数:); int m=reader.nextInt(); System.out.print(输入第二个整数:); int n=reader.nextInt(); A a=new A(); B b=new B(); System.out.println(m+和+n+的最大公约数是+a.f(m,n); System.out.println(m+和+n+的最小公倍数是+b.g(m,n); 5 import java.applet.Applet;import java.awt.*;public class Example4_10 extends Applet Button redbutton; public void init() redbutton=new Button(我是一个红色的按钮); redbutton.setBackground(Color.red);redbutton.setForeground(Color.white); add(redbutton); 第五章 作业题1import java.util.Scanner;class A public int f(int m,int n) if(mn) int temp=m; m=n; n=temp; int r=m%n; while(r!=0) m=n; n=r; r=m%n; return n; class B extends A public int f(int m,int n) int division=super.f(m,n); return (m*n)/division; public class ZuoYe5_1 public static void main (String args ) A a=new A(); B b=new B(); Scanner reader=new Scanner(System.in); System.out.println(输入2个整数,程序计算出它们的最大公约数和最小公倍数); System.out.print(输入第一个整数:); int m=reader.nextInt(); System.out.print(输入第二个整数:); int n=reader.nextInt(); if(m*n=160|age=0) throw new IntegerException(age); /方法抛出异常,导致方法结束 else this.age=age; public int getAge() System.out.println(年龄+age+合理); return age; public class ZuoYe6_5 public static void main(String args) People wang=new People(), zhang=new People(); try wang.setAge(189); System.out.println(wang.getAge(); catch(IntegerException e) System.out.println(e.toString(); try zhang.setAge(28); System.out.println(zhang.getAge(); catch(IntegerException e) System.out.println(e.toString(); 第六章 作业题1import java.util.regex.*;import java.util.*;public class ZuoYe6_1 public static void main(String args ) Scanner reader=new Scanner(System.in); String s1=reader.nextLine(); Pattern p; Matcher m; p=Ppile(24680A135792); m=p.matcher(s1); while(m.find() String str=m.group(); System.out.print(从+m.start()+到+m.end()+匹配模式子序列:); System.out.println(str); 2import java.util.regex.*;import java.util.*;public class ZuoYe4_1 public static void main(String args ) Scanner reader=new Scanner(System.in); String s1=reader.nextLine(); Pattern p; Matcher m; p=Ppile(d+); m=p.matcher(s1); while(m.find() String str=m.group(); System.out.print(str); 第七章 作业题1import java.util.*;public class ZuoYe7_2 public static void main(String args) int year,month; try year=Integer.parseInt(args0); month=Integer.parseInt(args1)+1; catch(NumberFormatException e) year=2004; month=1; System.out.println( 日 一 二 三 四 五 六); Calendar 日历=Calendar.getInstance(); 日历.set(year,month,1); int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1; String a=new String星期几+31; for(int i=0;i星期几;i+) ai=*; for(int i=星期几,n=1;i星期几+31;i+) if(n=9) ai=String.valueOf(n)+ ; else ai=String.valueOf(n) ; n+; for(int i=0;ia.length;i+) if(i%7=0) System.out.println(); System.out.print( +ai); 2.class ZuoYe7_2 public static void main(String args) int year1,month1,day1,year2,month2,day2; try year1=Integer.parseInt(args0); month1=Integer.parseInt(args1); day1=Integer.parseInt(args2); year2=Integer.parseInt(args3); month2=Integer.parseInt(args4); day2=Integer.parseInt(args5); catch(NumberFormatException e) year1=2009; month1=0; day1=1; year2=2008; month2=0; day2=1; Calendar calendar=Calendar.getInstance(); calendar.set(year1,month1,day1); long timeYear1=calendar.getTimeInMillis(); calendar.set(year2,month2,day2); long timeYear2=calendar.getTimeInMillis(); long 相隔天数=Math.abs(timeYear1-timeYear2)/(1000*60*60*24); System.out.println(+year1+年+month1+月+day1+日和+ year2+年+month2+月+day2+日相隔+相隔天数+天); 3. import java.math.*;public class ZuoYe4_1 public static void main(String args) BigInteger chengji=new BigInteger(1), ONE=new BigInteger(1), i=ONE, m=new BigInteger(10); while(pareTo(m)=0) chengji=chengji.multiply(i); i=i.add(ONE); System.out.println(chengji); 4import java.util.*;public class ZuoYe7_4 public static void main(String args) HashSet A=new HashSet(), B=new HashSet(), tempSet=new HashSet(); A.add(new Integer(1); A.add(new Integer(2); A.add(new Integer(3); A.add(new Integer(4); B.add(new Integer(1); B.add(new Integer(3); B.add(new Integer(7); B.add(new Integer(9); B.add(new Integer(11); tempSet=(HashSet)A.clone(); tempSet.retainAll(B); System.out.println(交:); Iterator iter=tempSet.iterator(); while(iter.hasNext() Integer te=iter.next(); System.out.printf(%d,,Value(); tempSet=(HashSet)A.clone(); tempSet.addAll(B); iter=tempSet.iterator(); System.out.println(并:); while(iter.hasNext() Integer te=iter.next(); System.out.printf(%d,,Value(); tempSet=(HashSet)A.clone(); tempSet.removeAll(B); System.out.println(差:); iter=tempSet.iterator(); while(iter.hasNext() Integer te=iter.next(); System.out.printf(%d,,Value(); 5import java.util.*;class MyKey implements Comparable double number=0; MyKey(double number) this.number=number; public int compareTo(Object b) MyKey st=(MyKey)b; if(this.number-st.number)=0) return -1; else return (int)(this.number-st.number)*1000); class 硬盘 int size; double price; 硬盘(int n, double p) size=n; price=p; public class ZuoYe4_1 public static void main(String args ) int size=10,3,7,12,10,22,100,4,6,2; double price=1.2,9.56,2.4,9.3,16.77,12.66,7.4,5.5,5.6,8.9,1.9; 硬盘 s=new 硬盘10; for(int i=0;is.length;i+) si=new 硬盘(sizei,pricei); TreeMap treemap=new TreeMap(); for(int i=0;is.length;i+) treemap.put(new MyKey(si.size),si); int number=treemap.size(); System.out.println(树映射中有+number+个对象:); Collection collection=treemap.values(); Iterator iter=collection.iterator(); while(iter.hasNext() 硬盘 te=iter.next(); System.out.println(te.size+,+te.price); treemap.clear(); for(int i=0;is.length;i+) treemap.put(new MyKey(si.price),si); number=treemap.size(); System.out.println(树映射中有+number+个对象:); collection=treemap.values(); iter=collection.iterator(); while(iter.hasNext() 硬盘 te=iter.next(); System.out.println(te.size+,+te.price); 第八章 作业题1属于操作题目,省略2参见例子8-23参见例子8-34. 参见例子8-10第九章 作业题1impor

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论