0

我的 Java 类有这个任务:

  1. 使您的 Shapes 具有可比性添加:“实现 Comparable”并创建方法:public int compareTo(Object o){ ... } 到抽象类 Shape (因此所有 Shapes 都继承它)。为了比较,请使用“形状”区域。
  2. 创建一个驱动程序(称为 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()));
        }
    }
}
4

1 回答 1

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() + ")");If you just do 一样System.out.println(point),您将得到一个奇怪的打印输出,除非您覆盖类中的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;
}
于 2013-10-19T21:15:27.447 回答