将上面的抽象类GeometricObject改为接口,接口只保留其中四个抽象方法,声明类Circle、Triangle实现该接口,类的基本要求如上,同时为每个类增加一个将当前对象序列化到指定文件的方法writeToFile(File f)。 测试类TestGeometricObject2设计要求如下: ■ 一个能随机生成Circle类和Triangle类对象的静态方法,该方法将随机生成的象序列化到指定的文件GeometricObjects.dat中,序列化成功返回真,否则返回假。 Boolean RandomCreateGeometricObject() ■ 将GeometricObjects.dat文件中对象全部读出,存储到GeometricObject对象数组中,然后以此数组为参数,输出数组中每个对象的基本信息、周长和面积。 ■ 类中其它方法的测试。 新增一个类Rectangle,也实现接口GeometricObject,同时修改测试类TestGeometricObject2。 版本3: 在第2步的基础上设计实现一个具有GUI界面的几何图形绘制系统系统,要求实现根据选择的几何图形类型来绘制和删除相应的图形,其中相关参数应通过界面输入,并可计算图形的周长和面积。 javafx
时间: 2024-03-13 10:44:15 浏览: 114
抽象接口GeometricObject如下:
```java
public interface GeometricObject {
public double getPerimeter();
public double getArea();
public void draw();
public void writeToFile(File f);
}
```
类Circle实现GeometricObject接口:
```java
public class Circle implements GeometricObject, Serializable {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public void draw() {
System.out.println("Draw a circle with radius " + radius);
}
@Override
public void writeToFile(File f) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(this);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
类Triangle实现GeometricObject接口:
```java
public class Triangle implements GeometricObject, Serializable {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
@Override
public double getArea() {
double s = getPerimeter() / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public void draw() {
System.out.println("Draw a triangle with sides " + side1 + ", " + side2 + ", " + side3);
}
@Override
public void writeToFile(File f) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(this);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
类Rectangle实现GeometricObject接口:
```java
public class Rectangle implements GeometricObject, Serializable {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public double getArea() {
return width * height;
}
@Override
public void draw() {
System.out.println("Draw a rectangle with width " + width + ", height " + height);
}
@Override
public void writeToFile(File f) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(this);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
测试类TestGeometricObject2如下:
```java
import java.io.*;
import java.util.Random;
public class TestGeometricObject2 {
public static void main(String[] args) {
// 随机生成Circle和Triangle类对象,并将对象序列化到GeometricObjects.dat文件中
RandomCreateGeometricObject(new File("GeometricObjects.dat"));
// 从GeometricObjects.dat文件中读取对象,并输出每个对象的基本信息、周长和面积
GeometricObject[] objects = readFromFile(new File("GeometricObjects.dat"));
for (GeometricObject obj : objects) {
System.out.println("Object: " + obj.getClass().getSimpleName());
System.out.println("Perimeter: " + obj.getPerimeter());
System.out.println("Area: " + obj.getArea());
obj.draw();
System.out.println();
}
// 测试Rectangle类的方法
GeometricObject rect = new Rectangle(5, 10);
System.out.println("Object: " + rect.getClass().getSimpleName());
System.out.println("Perimeter: " + rect.getPerimeter());
System.out.println("Area: " + rect.getArea());
rect.draw();
}
// 随机生成Circle和Triangle类对象,并将对象序列化到指定文件中
public static boolean RandomCreateGeometricObject(File f) {
try {
Random rand = new Random();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
for (int i = 0; i < 10; i++) {
if (rand.nextBoolean()) {
Circle c = new Circle(rand.nextDouble() * 10);
c.writeToFile(f);
} else {
Triangle t = new Triangle(rand.nextDouble() * 10, rand.nextDouble() * 10, rand.nextDouble() * 10);
t.writeToFile(f);
}
}
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
// 从指定文件中读取对象,并返回一个GeometricObject对象数组
public static GeometricObject[] readFromFile(File f) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
GeometricObject[] objects = new GeometricObject[10];
for (int i = 0; i < 10; i++) {
objects[i] = (GeometricObject) in.readObject();
}
in.close();
return objects;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
}
```
对于版本3,我们需要使用JavaFX来实现GUI界面。界面上应该有下拉框或单选按钮来选择要绘制的几何图形类型,以及输入框来输入相应的参数。当用户点击绘制按钮时,应该根据输入的参数来创建相应的几何图形对象,并在画布上绘制出来。同时,应该显示该几何图形的周长和面积。
由于JavaFX相关知识较多,这里无法详细讲解。建议您参考JavaFX官方文档或在线教程学习。
阅读全文