Hello everyone,
I have this assignment for my Java class:
1. Make your Shapes comparable add: "implements Comparable" and create the method: public int compareTo(Object o){ ... }
to the abstract class Shape (so all the Shapes inherit it). For the comparison use the Shapes' area.
2. Create a driver (called Driver.java) that:
a) creates an array of shapes (ask user for the size)
b) in a loop, populate the array. (ask user what shape to create and the parameters need to create each shape)
c) print out the unsorted array
d) sort the array (I will post an implementation of SelectionSort in a file called SelectioSort.java)
e) print out sorted array
Extra credit
Use the authors ReadInt and ReadDouble to get all users input (in a file called ReadData.java)
My problem is populating the array to create each shape in class Driver. I can't figure out how to inherit the variables from each subclass using 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())); } } }