【问题描述】 1)设计一个通用类GeometricObject可以用来建模所有的几何对象。 包括成员变量: •颜色color是一个字符串,在无参构造函数中将其初始化为"white"。 •filled表示圆是否填充,为boolean类型,在无参构造方法中将其初始化为false。 2)设计一个接口Computable,该接口中包括: •常量PI=3.14,可以用于计算圆的面积。 •double getArea(); \\计算面积 •double getPerimeter(); \\计算周长 3)设计一个名为Triangle的类来扩展GeometricObject类,并实现接口Computable。该类包括: •三个名为side1、side2和side3的double成员变量表示这个三角形的三条边,它们的默认值是1.0。 •一个无参构造方法创建默认的三角形,将三角形的三条边设为默认值1.0。 •一个能创建带指定side1、side2和side3的三角形的构造方法。 4)设计一个Circle类来扩展GeometricObject,并实现接Computable。该类包括了: •一个成员变量:radius,表示圆的半径; •一个带参的构造方法用于设定圆的半径:radius; 5)设计一个类 Factory,该类中包括一个方法: Computable getComputableShapeInstance(int shape, Scanner reader); 当shape 为 1时输入圆的半径,创建一个Circle的对象, 并返回该对象的引用, 当shape 为 2时输入三角形的三条边,创建一个Triangle的对象, 并返回该对象的引用, 为了便于在作业系统中进行测试,将传入一个Scanner 对象的引用,并在该方法中使用reader输入数据。 提示:可以用下面方法按四舍五入输出两位小数: System.out.printf("%.2f", s); 6)编写一个测试程序,创建相应对象,显示相应信息,主方法程序如下: import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner reader = new Scanner(System.in); Computable shape; Factory shapeFactory = new Factory(); shape = shapeFactory.getComputableShapeInstance(1, reader); System.out.println("Circle area:" + shape.getArea()); shape = shapeFactory.getComputableShapeInstance(2, reader); System.out.println("Triangle area:" + shape.getArea()); } } 【输入出格式】:下划线为输入 3 Circle area:28.26 3 4 5 Triangle area:6.0
时间: 2023-12-03 21:42:02 浏览: 181
代码实现如下:
GeometricObject.java
```java
public class GeometricObject {
protected String color;
protected boolean filled;
public GeometricObject() {
color = "white";
filled = false;
}
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;
}
}
```
Computable.java
```java
public interface Computable {
double PI = 3.14;
double getArea();
double getPerimeter();
}
```
Triangle.java
```java
import java.util.Scanner;
public class Triangle extends GeometricObject implements Computable {
private double side1;
private double side2;
private double side3;
public Triangle() {
side1 = side2 = side3 = 1.0;
}
public Triangle(double side1, double side2, double side3) {
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 = getPerimeter() / 2;
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
}
```
Circle.java
```java
import java.util.Scanner;
public class Circle extends GeometricObject implements Computable {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * PI * radius;
}
}
```
Factory.java
```java
import java.util.Scanner;
public class Factory {
public Computable getComputableShapeInstance(int shape, Scanner reader) {
if (shape == 1) {
System.out.print("请输入圆的半径:");
double radius = reader.nextDouble();
return new Circle(radius);
} else if (shape == 2) {
System.out.print("请输入三角形的三条边:");
double side1 = reader.nextDouble();
double side2 = reader.nextDouble();
double side3 = reader.nextDouble();
return new Triangle(side1, side2, side3);
} else {
return null;
}
}
}
```
Test.java
```java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
Computable shape;
Factory shapeFactory = new Factory();
shape = shapeFactory.getComputableShapeInstance(1, reader);
System.out.printf("Circle area: %.2f\n", shape.getArea());
shape = shapeFactory.getComputableShapeInstance(2, reader);
System.out.printf("Triangle area: %.2f\n", shape.getArea());
}
}
```
阅读全文