在上一题的基础上,让抽象类GeometricObject实现接口Comparable,UML图如下:
注意:实现接口的连线是空心三角形+虚线形式的箭头,接口前面加<>,接口和接口里面的方法为斜体,要求掌握绘制方法。
(1) 编写父类GeometricObject、矩形类 Rectangle和圆类Circle,分别实现从父类和接口继承而来的方法,Rectangle和Circle对象通过面积比较大小;
提示:
(2) 编写测试类,分别创建圆形数组和矩形数组,按照面积进行排序,并输出结果。
提示:
java.util包中的Arrays类,可以对数组进行排序,自己查看JDK帮助文档(查看sort(Object[]
a)方法的用法)并参考教材“程序清单13-10”,对两个数组进行排序;
//Comparable接口不需要写,java.lang中有,可以直接用
public abstract class GeometricObject {
public abstract double getArea();
public abstract double getPerimeter();
public int compareTo(GeometricObject g) {
if(this.getArea() < g.getArea()) {
return -1;
}
else if(this.getArea() == g.getArea()) {
return 0;
}
else {
return 1;
}
} //比较一类图形面积大小(升序),在父类中写compareTo方法,使子类直接继承,使程序更加简明
}
Rectangle类:
public class Rectangle extends GeometricObject implements Comparable<Rectangle> {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public int compareTo(Rectangle r) {
return super.compareTo(r);
}//继承父类,比较各个矩形对象面积
/*如果不在父类中写compareTo方法,则如是写
public int compareTo(Rectangle r) {
if(this.getArea() < r.getArea()) {
return -1;
}
else if(this.getArea() == r.getArea()) {
return 0;
}
else {
return 1;
}
}*/
public String toString() {
return "矩形的高:" + height + " 宽:" + width + " 面积:" + getArea() + "\n";
}
}
Circle类:
public class Circle extends GeometricObject implements Comparable<Circle> {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}//计算圆的面积
public double getPerimeter() {
return 2 * Math.PI * radius;
}//计算圆的周长
public int compareTo(Circle c) {
return super.compareTo(c);
}//继承父类,比较各个圆对象面积
/*如果不在父类中写compareTo方法,则如是写
public int compareTo(Circle c) {
if(this.getArea() < c.getArea()) {
return -1;
}
else if(this.getArea() == c.getArea()) {
return 0;
}
else {
return 1;
}
}*/
public String toString() {
return "圆的半径:" + radius + " 面积:" + getArea() + "\n";
}
}
测试类:
import java.util.Arrays;
public class twoTest {
public static void main(String[] args) {
Rectangle[] rs = {
new Rectangle(3.4, 5.4),
new Rectangle(13.24, 55.4),
new Rectangle(1.4, 25.4),
new Rectangle(7.4, 35.4)
};//创建4个矩形对象
Arrays.sort(rs);//对矩形面积进行升序排序
for(Rectangle r: rs) {
System.out.print(r + "");
System.out.println();
}//以矩形面积升序输出矩形数组
System.out.println("------------------------------------");
Circle[] cs = {
new Circle(1.0),
new Circle(4.3),
new Circle(98.0),
new Circle(5.6),
new Circle(8.0)
};//创建5个圆形对象
Arrays.sort(cs);//对圆形面积进行升序排序
for(Circle c: cs) {
System.out.print(c + "");
System.out.println();
}//以圆形面积升序输出圆形数组
/*另一种方法
Rectangle[] r = new Rectangle[4];
Circle[] c = new Circle[5];
r[0] = new Rectangle(3.4, 5.4);
r[1] = new Rectangle(1.4, 25.4);
r[2] = new Rectangle(7.4, 35.4);
r[3] = new Rectangle(13.24, 55.4);
c[0] = new Circle(1.0);
c[1] = new Circle(4.3);
c[2] = new Circle(5.6);
c[3] = new Circle(8.0);
c[4] = new Circle(98.0);//也可以这样来创建对象
System.out.println(Arrays.toString(r));
System.out.println("-----------------------------");
System.out.println(Arrays.toString(c));使用toString输出,首尾会带有“[]”,且中间有逗号分隔,看起来不好看
//.replace("[", "").replace("]", "").replace(",", "")
使用这个即可消去“[]”和逗号,但实际是以空格代替了,看起来还是不好看。若要使用,直接在Arrays.toString(r)后跟上即可
*/
}
}