《Java编程语言:原理与范例》课后实验源代码 下载本文

内容发布更新时间 : 2024/4/29 1:18:47星期一 下面是文章的全部内容请认真阅读。

}

}

a[0] = last;

void print(int[] a) { for (int i = 0; i < a.length; i++) { System.out.printf(\ } System.out.println(); }

public static void main(String[] args) { Exp4_5 t = new Exp4_5(); int[] a = { 7, 4, 8, 9, 1, 5 }; t.print(a); for (int i = 0; i < a.length - 1; i++) { t.shift(a); t.print(a); } }

第五章 实验一

/*********************************

2. 在例6.17的基础上,将draw方法改为计算形状自身面积的calcArea方法并在测试类中测试。

提示:可根据需要在Shape的每个子类中增加用以计算面积所必须的字段,如为Square类增加边长

字段、Circle类增加半径字段等,然后编写含相应字段的构造方法以构造具体的形状对象。

输出结果: s1的面积:4.0 s2的面积:6.0

s3的面积:12.566370614359172

**********************************/ package ch06;

class Shape { public double calcArea() { return 0; } }

class Square extends Shape { float width; public Square(int width) { this.width = width; } public double calcArea() { return width * width; } }

class Triangle extends Shape { float a, b, c; public Triangle(float a, float b, float c) { this.a = a; this.b = b; this.c = c; } public double calcArea() {

float s = (a + b + c) / 2; return Math.sqrt(s * (s - a) * (s - b) * (s - c)); } }

class Circle extends Shape { float radius; public Circle(float radius) { this.radius = radius; } public double calcArea() { return Math.PI * radius * radius; } }

public class Exp5_2 { public static void main(String[] args) { Shape s1 = new Square(2); Shape s2 = new Triangle(3, 4, 5); Shape s3 = new Circle(2); System.out.println(\的面积:\ System.out.println(\的面积:\ System.out.println(\的面积:\ } }

实验二

/*********************************

3. 编写TimeCounter类,其静态方法startCount(int begin)方法根据指定的初始 秒数begin在命令行显示倒计时(每隔1秒刷新,时间为0时退出程序),然后在main方法 中调用startCount方法。

**********************************/ package ch06;

public class Exp5_3 { static void startCount(int begin) { while (begin > 0) { System.out.println(begin); long init = System.currentTimeMillis(); while (true) { long now = System.currentTimeMillis(); if (now - init >= 1000) { begin--;

break; } } } System.out.println(\时间到!\ } public static void main(String[] args) { startCount(5); } }

实验三

/*********************************

4. 编写Complex类表示数学上的复数概念,具体包括: ① real和image字段,分别表示复数的实部和虚部。 ② 读取和设置real/image字段的get和set方法。 ③ 根据实部和虚部参数构造复数对象的构造方法。

④ 打印当前复数对象内容以及与另一复数相加的方法,原型为: void printInfo();

Complex add(Complex anotherComplex);

⑤ 重写父类Object的equals方法,相等逻辑为“若2个复数对象的实部和虚部分别对应相等,则这2个复数相等”。

最后编写一个带main方法的测试类ComplexTest,分别测试Complex中的各个方法。 **********************************/ package ch06;

class Complex { float real; float image; public float getReal() { return real; } public void setReal(float real) { this.real = real; } public float getImage() { return image; } public void setImage(float image) { this.image = image;

} public Complex(float real, float image) { this.real = real; this.image = image; } void printInfo() { System.out.println(real + (image > 0 ? \ } Complex add(Complex anotherComplex) { Complex c = new Complex(0, 0); c.setReal(this.real + anotherComplex.getReal()); c.setImage(this.image + anotherComplex.getImage()); return c; } public boolean equals(Object obj) { Complex c = (Complex) obj; return this.real == c.getReal() && this.image == c.getImage(); } }

public class Exp5_4 { public static void main(String[] args) { Complex c1 = new Complex(1, -2); Complex c2 = new Complex(2, 6); Complex c3 = c1.add(c2); Complex c4 = c2.add(c1); System.out.print(\ c1.printInfo(); System.out.print(\ c2.printInfo(); System.out.print(\ c3.printInfo(); System.out.print(\ c4.printInfo(); System.out.println(c1.equals(c2) ? \