我的 Java 类有这个任务:
- 使您的 Shapes 具有可比性添加:“实现 Comparable”并创建方法:public int compareTo(Object o){ ... } 到抽象类 Shape (因此所有 Shapes 都继承它)。为了比较,请使用“形状”区域。
- 创建一个驱动程序(称为 Driver.java),它:a)创建一个形状数组(向用户询问大小)
b) 在循环中,填充数组。(询问用户要创建什么形状以及创建每个形状所需的参数)
c) 打印出未排序的数组
d) 对数组进行排序(我将在名为 SelectioSort.java 的文件中发布 SelectionSort 的实现) e) 打印出排序后的数组
额外功劳 使用作者 ReadInt 和 ReadDouble 获取所有用户输入(在名为 ReadData.java 的文件中)
我的问题是填充数组以在类 Driver 中创建每个形状。我不知道如何使用 switch 从每个子类继承变量。
// Driver.java
// Driver for testing the sorting of simple Shapes (hierarchy: point, square, cube, circle, & cylinder)
// it creates an array of shapes
// it populate the array
// print out the unsorted array (area & volume of each shape)
// sort the array using an implementation of SelectionSort (shapes need to implement Comparable)
// and lastly print out sorted array (area & volume of each shape)
// Needs: Shape.java, Point.java, Square.java, Cube.java, Circle.java, Cylinder.java & SelectioSort.java
import java.io.*;
import java.text.*;
public class Driver {
public static void main(String[] args) throws IOException {
Shape[] arrayOfShapes; //holds the list
int choice; //code number for each type of figure
System.out.println("How many data you want to input? ");
int size = ReadData.readInt();
System.out.println("Enter: " + size);
System.out.println("\n");
arrayOfShapes = new Shape[size];
for (int i = 0; i < arrayOfShapes.length; i++) {
Point point;
Square square;
Cube cube;
Circle circle;
Cylinder cylinder;
System.out.println("What Shape do you want to create?");
System.out.println("1. Point");
System.out.println("2. Square");
System.out.println("3. Cube");
System.out.println("4. Circle");
System.out.println("5. Cylinder");
System.out.println("Choose one:");
choice = ReadData.readInt();
System.out.println("\n");
switch (choice) {
case 1:
arrayOfShapes[i] = new Point(x, y);
System.out.println("Please enter Coordinate 1: ");
System.out.println("Please enter Coordinate 2: ");
s = ReadData.readDouble();
System.out.println(x);
y = ReadData.readDouble();
System.out.println(y);
System.out.println(point);
break;
case 2:
arrayOfShapes[i] = new Square(side, x, y);
System.out.println("Please enter the 3 size of square: ");
side = ReadData.readDouble();
x = ReadData.readDouble();
y = ReadData.readDouble();
System.out.println(square);
break;
case 3:
arrayOfShapes[i] = new Cube(depth, x, y);
System.out.println("Please enter the 3 size of Cube: ");
depth = ReadData.readDouble();
x = ReadData.readDouble();
y = ReadData.readDouble();
System.out.println(cube);
break;
case 4:
arrayOfShapes[i] = new Circle(radius, x, y);
radius = ReadData.readDouble();
System.out.println("Please enter the radius for circle: ");
radius = ReadData.readDouble();
System.out.println(circle);
break;
case 5:
arrayOfShapes[i] = new Cylinder(height, x, y, radius);
System.out.println("Please enter the height of Cylinder: ");
height = ReadData.readDouble();
System.out.println(cylinder);
default:
break;
}
}
DecimalFormat precision2 = new DecimalFormat("#0.00");
// Loop through arrayOfShapes and print the name, area, and volume of each object.
System.out.println(" Before we sort on area we have :");
for (int i = 0; i < arrayOfShapes.length; i++) {
System.out.print(arrayOfShapes[i].getName() + ": " + arrayOfShapes[i].toString());
System.out.print(" volume = " + precision2.format(arrayOfShapes[i].volume()));
System.out.println(" AREA = " + precision2.format(arrayOfShapes[i].area()));
}
SelectionSort.sort(arrayOfShapes, arrayOfShapes.length);
System.out.println(" After we sort the array we have :");
for (int i = 0; i < arrayOfShapes.length; i++) {
System.out.print(arrayOfShapes[ i].getName() + ": " + arrayOfShapes[ i].toString());
System.out.print(" volume = " + precision2.format(arrayOfShapes[ i].volume()));
System.out.println(" AREA = " + precision2.format(arrayOfShapes[ i].area()));
}
}
}