Question
Description In this assignment, you'll create a hierarchy of Java classes representing various Shapes (eg. circle, rectangle, square). Each different shape class will have its
Description
In this assignment, you'll create a hierarchy of Java classes representing various Shapes (eg. circle, rectangle, square). Each different shape class will have its own .java source file, and will be derived from the base class Shape.
For the root of the Shape class hierarchy, write a class named Shape that will be the base class of all the other shapes. It should have one private String instance variable representing the shape's color, and should have the following public methods:
public Shape(String color) - a constructor that sets the color instance value.
public String getColor() - this method returns the object's color value.
public double area() - this method computes and returns the object's area. It must be overridden in each derived class. For the base Shape class, this method returns 0.0.
public String toString() - this method returns the object's description (color, type, measurements, and area) as a String. It must be overridden in each derived class. For the base Shape class, this method returns the string "generic shape".
Write a Circle class that extends Shape. It should have one private double instance variable representing the radius, and should have the following public methods:
public Circle(String color, double radius) - a constructor that invokes the base constructor (passing the color), then sets the radius instance value.
public double area() - this overriding method computes and returns the Circle object's area value.
public String toString() - this overriding method returns the Circle object's description (color, type, measurements, and area).
Write a Square class with one private double instance variable representing the side length, with a constructor and overriding methods for area() and toString().
Write a Rectangle class with double instance variables representing the width and length, with a constructor and overriding methods for area() and toString().
Each derived class constructor must use super to call the base Shape constructor with the color parameter.
Derived classes do not provide a method override for getColor(). The base Shape class implementation is inherited.
In the ShapeDemo class (download provided below), you must add code to sort the array of shapes into ascending order by area. Add a sortArray method to the ShapeDemo class, and call it from main(), passing the array of Shape references as a parameter. The sortArray method must implement the sort logic to sort the array of Shape references into ascending order. Start with the sortArray method from the ArraySort assignment, changing the logic to call the area()method on the two shapes being compared to get their respective areas.
The order that you compile your Java source files is important. You must have your Shape.java file written and compiled before you can compile any of the derived class files (eg. Circle.java) that extend from class Shape. All the Shape derived classes must be compiled before you can compile your ShapeDemo source file containing main() and getShape(). The requirement is that if you use a class name in your code, the Java compiler must be able to find the compiled .class file for that class while compiling the code.
Sample output
Enter a list of shapes - 'done' to end
Enter the shape's color (or 'done')... red Enter shape type... circle Enter the radius... 5.0
Enter the shape's color (or 'done')... green Enter shape type... rectangle Enter the length and width... 2.0 4.0
Enter the shape's color (or 'done')... blue Enter shape type... square Enter the length of a side... 3.0
Enter the shape's color (or 'done')... done
The list of shapes entered... red circle with radius of 5.0 and area of 78.53975 green Rectangle with length of 2.0 and width of 4.0 and area of 8.0 blue Square with side length of 3.0 and area of 9.0
Sorting shapes into order by area...
The sorted list of shapes... green Rectangle with length of 2.0 and width of 4.0 and area of 8.0 blue Square with side length of 3.0 and area of 9.0 red circle with radius of 5.0 and area of 78.53975
ShapeDemo Code with main() and getShape()
Use the attached ShapeDemo.java source file as a starting template for writing your main(). This provides the getShape() method to read and create the desired shapes, and part of the main() method.
You need to add the sortArray() method to the class and call it from main() to sort the array of shape references. This is the same sorting logic as your previous sorting code for an array of integer values. The one difference for this homework is that instead of comparing two values in the array, you call the area() method on the two derived Shape objects that are being compared. The area() method is called using dynamic binding, so the correct area()method implementation is called for the type of Shape referenced in order to return the area value to be compared. If the two area() results are out of order, then you swap the Shape references in the array elements.
Note that the main() method in ShapeDemo is an example of generic object oriented programming. It fills an array of Shapes, prints them, sorts them, and prints them again, without knowing the exact types of the Shape objects it is working with (and doesn't need to)! This is a very common and powerful OOP technique. All Shape derived objects implement the same set of methods, and thus code using them can be written to generically work with Shape objects.
Test Data
Use the following set of test data:
red circle, radius 3.0 blue square, side length 4.0. green rectangle, length 3.0, width 2.0 blue circle, radius 2.0 red rectangle, length 8.0, width 2.0 green circle, radius 1.0 red square, side length 2.0
-----------------------------------------------------------------------------------------------------------
/** * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the derived classes * Circle, Rectangle, and Square can be compiled. * * The Shape, Circle, Rectangle, and Square classes * must be compiled before this democlass can be compiled. * */ import java.util.Scanner; public class ShapeDemo { /** * getShape - read a Shape's description and create the specific object */ public static Shape getShape() { Scanner sc = new Scanner(System.in); // Continue asking for input until a valid shape entered, or 'done' do { System.out.println(" Enter the shape's color (or 'done')..."); String color = sc.nextLine(); if (color.equalsIgnoreCase("done")) { return null; } if (!color.equalsIgnoreCase("red") && !color.equalsIgnoreCase("blue") && !color.equalsIgnoreCase("green")) { System.out.println(" Error - color must be 'red', 'blue', or 'green'"); continue; } System.out.println("Enter shape type..."); String name = sc.nextLine(); if (name.equalsIgnoreCase("circle")) { System.out.println("Enter the radius..."); double radius = sc.nextDouble(); if (radius < 0.0) { System.out.println("Radius must be non-negative"); continue; } return new Circle(color, radius); } else if (name.equalsIgnoreCase("rectangle")) { System.out.println("Enter the length and width..."); double length = sc.nextDouble(); double width = sc.nextDouble(); if (length < 0.0 || width < 0.0) { System.out.println("length and width must be non-negative"); continue; } return new Rectangle(color, length, width); } else if (name.equalsIgnoreCase("square")) { System.out.println("Enter the length of a side..."); double length = sc.nextDouble(); if (length < 0.0) { System.out.println("length must be non-negative"); continue; } return new Square(color, length); } else { System.out.println("shape name must be 'circle', 'rectangle', or 'square'"); continue; } } while (true); } /** * main */ public static void main (String[] args) { Scanner sc = new Scanner(System.in); // Create an array of Shape references larger than the maximum number // of Shapes to be read (could also prompt for size). Shape[] slist = new Shape[50]; // Read shapes until null returned. Note that this code doesn't know what // specific type of Shape has been returned. System.out.println("Enter a list of shapes - 'done' to end"); Shape sp; int numShapes = 0; while (null != (sp = getShape())) { slist[numShapes++] = sp; } // Print the list of shapes. Get the specific Shape's information using // dynamic binding to call the implementation of toString() defined for // the actual type of Shape referenced. System.out.println(" The list of shapes entered..."); for (int n = 0; n < numShapes; ++n) { System.out.println(" " + slist[n].toString()); } // Sort the list of shapes into ascending order by area. Get the each // specific shape's area using dynamic binding to call the implementation // of area() defined for the the actual type of Shape referenced. System.out.println(" Sorting shapes into order by area..."); // TODO - call the sortArray method to sort the array of Shapes. // Pass slist and numShapes as parameters. // Print the sorted list of shapes. System.out.println(" The sorted list of shapes..."); for (int n = 0; n < numShapes; ++n) { System.out.println(" " + slist[n].toString()); } // Keep console window alive until 'enter' pressed (if needed). System.out.println(); System.out.println("Done - press enter key to end program"); String junk = sc.nextLine(); } // TODO - add the sortArray method to sort the array of Shapes into // ascending order. The array and number of shapes in it must be passed // as parameters. }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started