我为我的Java类做了这个任务:
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()));
}
}
}
答案 0 :(得分:0)
试试这个
Scanner scanner = new Scanner(System.in);
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:");
int choice = scanner.nextInt();
System.out.println("\n");
switch (choice) {
case 1:
System.out.println("Please enter Coordinate 1: ");
int x = scanner.nextDuoble();
System.out.println(x);
System.out.println("Please enter Coordinate 2: ");
int y = scanner.nextDouble();
System.out.println(y);
point = new Point(x,y);
arrayOfShapes[i] = point;
System.out.println("(" + point.getX() + "," + point.getY() + ")");
break;
...
...
您的班级中的所有数据字段都应该有匹配的getData()
方法,以便您可以将其打印出来,如System.out.println("(" + point.getX() + "," + point.getY() + ")");
如果您只是System.out.println(point)
,您将得到一个奇怪的打印out,除非你覆盖每个类继承的toString()
类的Object
方法。例如:
// this should be somewhere in each of your classes (if you choose to)
@Override
public String toString() {
// Whatever you want the print out to be for this class
}
用课程编辑
// Classes should look something like this
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
编辑:使用示例conpareTo()
方法
@Override
public int compareTo(Shape o) {
if (this.getArea() > o.getArea())
return 1;
else if (this.getArea() == o.getArea())
return 0;
else
return -1;
}