Java クラスに次の割り当てがあります。
- Shapes を比較可能に add: "implements Comparable" し、メソッド public int compareTo(Object o){ ... } を抽象クラス Shape に作成します (したがって、すべての Shapes はそれを継承します)。比較には、シェイプの領域を使用します。
- a) 形状の配列を作成する (ユーザーにサイズを尋ねる) ドライバー (Driver.java と呼ばれる) を作成します。
b) ループで、配列にデータを入力します。(作成する形状と、各形状を作成するために必要なパラメーターをユーザーに尋ねます)
c)ソートされていない配列を出力します
d) 配列をソートします (SelectioSort.java というファイルに SelectionSort の実装を投稿します) e) ソートされた配列を出力します
特別クレジット 著者の ReadInt および ReadDouble を使用して、すべてのユーザー入力を取得します (ReadData.java というファイル内)。
私の問題は、クラス Driver で各形状を作成するために配列を作成することです。スイッチを使用して各サブクラスから変数を継承する方法がわかりません。
// 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()));
}
}
}