(将GeometricObject类变成可比较的)修改GeometricObject类以实现Comparable接 口,并且在GeometricObject类中定义一个静态的求两个GeometricObject对象中较大者 的max方法。实现这个新的GeometricObject类,并编写一个测试程序,使用max方法 求两个圆中的较大者和两个矩形中的较大者java
时间: 2023-06-18 13:02:48 浏览: 135
下面是修改后的GeometricObject类:
```java
public abstract class GeometricObject implements Comparable<GeometricObject>{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
@Override
public int compareTo(GeometricObject o) {
if (this.getArea() > o.getArea())
return 1;
else if (this.getArea() < o.getArea())
return -1;
else
return 0;
}
public static GeometricObject max(GeometricObject o1, GeometricObject o2) {
return (o1.compareTo(o2) > 0) ? o1 : o2;
}
}
```
然后我们可以定义一个Circle类和一个Rectangle类来继承GeometricObject类:
```java
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
@Override
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
@Override
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/** Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
```
```java
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public Rectangle(double width, double height, String color, boolean filled) {
super(color, filled);
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
@Override
public double getArea() {
return width * height;
}
/** Return perimeter */
@Override
public double getPerimeter() {
return 2 * (width + height);
}
}
```
最后,我们可以编写一个测试程序来演示max方法的使用:
```java
public class TestGeometricObject {
public static void main(String[] args) {
Circle circle1 = new Circle(5);
Circle circle2 = new Circle(10);
Rectangle rectangle1 = new Rectangle(5, 10);
Rectangle rectangle2 = new Rectangle(10, 20);
GeometricObject maxCircle = GeometricObject.max(circle1, circle2);
GeometricObject maxRectangle = GeometricObject.max(rectangle1, rectangle2);
System.out.println("The larger circle has area " + maxCircle.getArea());
System.out.println("The larger rectangle has area " + maxRectangle.getArea());
}
}
```
输出:
```
The larger circle has area 314.1592653589793
The larger rectangle has area 200.0
```