用Java写一大段代码,生成测试类TestGeometricObject1设计要求如下: ■ 一个能随机生成Circle类和Triangle类对象的静态方法 GeometricObject[] RandomCreateGeometricObject() ■ 以随机生成的数组为参数,输出数组中每个对象的基本信息、周长和面积。 ■ 类中其它方法的测试
时间: 2024-03-02 14:52:04 浏览: 81
好的,以下是代码实现:
```java
import java.util.Random;
public abstract class GeometricObject {
private String color;
private boolean filled;
public GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public abstract double getArea();
public abstract double getPerimeter();
public abstract String getInfo();
}
class Circle extends GeometricObject {
private double radius;
public Circle(String color, boolean filled, double radius) {
super(color, filled);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public String getInfo() {
return "Circle: radius = " + radius + ", color = " + getColor() + ", filled = " + isFilled();
}
}
class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
public Triangle(String color, boolean filled, double side1, double side2, double side3) {
super(color, filled);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
@Override
public double getArea() {
double p = (side1 + side2 + side3) / 2;
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
@Override
public String getInfo() {
return "Triangle: side1 = " + side1 + ", side2 = " + side2 + ", side3 = " + side3 + ", color = " + getColor() + ", filled = " + isFilled();
}
}
public class TestGeometricObject1 {
public static GeometricObject[] RandomCreateGeometricObject() {
Random random = new Random();
GeometricObject[] objects = new GeometricObject[10];
for (int i = 0; i < 10; i++) {
if (random.nextBoolean()) {
double radius = random.nextDouble() * 10;
objects[i] = new Circle("red", true, radius);
} else {
double side1 = random.nextDouble() * 10;
double side2 = random.nextDouble() * 10;
double side3 = random.nextDouble() * 10;
objects[i] = new Triangle("blue", false, side1, side2, side3);
}
}
return objects;
}
public static void printInfo(GeometricObject[] objects) {
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i].getInfo());
System.out.println("Area: " + objects[i].getArea());
System.out.println("Perimeter: " + objects[i].getPerimeter());
System.out.println();
}
}
public static void main(String[] args) {
GeometricObject[] objects = RandomCreateGeometricObject();
printInfo(objects);
}
}
```
说明:
1. 抽象类`GeometricObject`表示几何图形,包含颜色、是否填充等属性,并定义了计算面积和周长的抽象方法。
2. 子类`Circle`表示圆形,包含半径属性,并实现了抽象方法。
3. 子类`Triangle`表示三角形,包含三边属性,并实现了抽象方法。
4. `TestGeometricObject1`类包含了静态方法`RandomCreateGeometricObject`,用于随机生成数组中的对象,以及静态方法`printInfo`,用于输出数组中每个对象的基本信息、周长和面积。
5. 在`main`方法中,先调用`RandomCreateGeometricObject`方法随机生成数组,再调用`printInfo`方法输出基本信息、周长和面积。
阅读全文