Question
9.1 LAB: Shapes Hierarchy (abstract classes and interfaces) In this lab, you will be implementing a Shapes class hierarchy. Shape Superclass Implement an abstract class
9.1 LAB: Shapes Hierarchy (abstract classes and interfaces) In this lab, you will be implementing a Shapes class hierarchy. Shape Superclass Implement an abstract class Shape containing abstract methods: area() perimeter() getShapeName() area() and perimeter() should return a doubles. getShapeName() returns a String, that shapes's name. For example, a Circle should return "circle". Also add a static variable shapeCount that is incremented every time a Shape is constructed. Implement a method getShapeCount() that returns the value of shapeCount. This variable must not be directly accessible outside of Shape; only its value should be able to be queried. Finally, override the toString() method from Object to return a String representing the shape, formatted exactly as follows. For a Square with a length of 2.0, the output would be: square Area: 4.00 Perimeter: 8.00 Hint: You can use String.format() to precisely format strings. Subclasses Circle, Rectangle, and Square Implement Circle and Rectangle as direct subclasses of Shape. Implement Square as a subclass of Rectangle. Circle's constructor should take in its radius. Rectangle's should take in its length and width. Square's should take in its length. Comparable Java has a built-in interface called Comparable which consists of a single method compareTo(T other). The T in Comparable is a generic type. When you implement the interface, you replace T with the type you want to compare to. For two objects x and y which are comparable, x.compareTo(y) should return a negative number is x < y, a positive number if x > y, or 0 otherwise. Modify Shape so that it implements Comparable. For the purposes of this, compare shapes based on their area. Shapes Runner Finally, complete the implementation for ShapesRunner.java. This program will use sentinel controlled iteration to read in a list of shapes, sort, and print them. A List of Shapes An ArrayList is constructed for you. Inside of the while loop, within the conditional, construct and add each type of Shape to the shapes arraylist. Sorting the list Since you have implemented the Comparable interface, you can built in java utilities to sort your list. Use Collections.sort() to sort shapes.
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