1、习题解答第一章 作业题1public class Hellopublic 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_1public
2、static void main (String args )char c=;System.out.println(“字母“+c+“在 unicode 表中的顺序位置 :“+(int)c);System.out.println(“字母表:“);while(c0)y=-1+3*x;System.out.println(y);2public class ZuoYe3_2public static void main(String args)int sum=0,m=3,n=7,a=1;while(a=10)sum=sum+a;i+;a=a*i;System.out.println(“sum=“+su
3、m);8第四章 作业题1class DengChaint 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=0)root1=(-b+Math.sqrt(disk)/(2*a);root2=(-b-Math.sqrt(disk)/(2*a);System.out.printf(“方程的根:%f,%fn“,root1,root2);
4、 elseSystem.out.printf(“方程没有实根n“);elseSystem.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; elseboo=false;public class ZuoYe4_3public static void main(String args )SquareEquation equation1=new SquareEquation(4,5
5、,1);SquareEquation equation2=new SquareEquation(3,5,-7);equation1.getRoots();equation2.getRoots();4import java.util.Scanner;class Aint 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
6、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(“输入第二个整数:
7、“);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); 5import java.applet.Applet;import java.awt.*;public class Example4_10 extends Applet Button redbutton;public void init() redbutton=new Button(“我是一个红色的按钮 “);
8、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);r
9、eturn (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();
10、if(m*n0) System.out.println(“有负数,程序退出“);System.exit(0); System.out.printf(“%d 和%d 的最大公约数是%dn“,m,n,a.f(m,n);System.out.printf(“%d 和%d 的最小公倍数是%dn“,m,n,b.f(m,n); 2abstract class Apublic abstract void f(int x);public abstract void g(int x,int y);public abstract double h(double x);class A1 extends Apublic void f(int x)System.out.println(x); public void g(int x,int y)int z=x+y;System.out.println(z); public double h(double x)return x*x;class A2 extends Apublic void f(int x)System.out.println(“Hello:“+x); public void g(int x,int y)int z=x-y;