Question: java program. Design a class named Shape and its two subclasses named Rectangle and Circle . Let Square be a subclass of Rectangle . A
java program.
Design a class named Shape and its two subclasses named Rectangle and Circle. Let Square be a subclass of Rectangle.
- A Shape has a field called origin of Point2D type and 3 abstract methods: getPerimeter(), getArea() and getVolume(). Add getter and setter methods to the Shape class. Override toString() and equals() methods in Shape class. Define the full-argument, no-argument and copy instructors in the Shape class. Two Shape objects are equal if they have the same origin. The toString() method should return:
Shape(1, 2)
Hint: The toString() output assumes a Shape object with origin field pointing to a Point2D object with values 1 and 2 in its x_coord and y_coord fields, respectively.
- The Rectangle class has two int fields: length and width. Add getter and setter methods to the Rectangle class. Override toString() and equals() methods in Rectangle class. Define the full-argument, no-argument and copy instructors in the Rectangle class. Make sure to call the full-argument constructor of the Shape class in the full-argument constructor of the Rectangle class. Two Rectangle objects are equal if they have the same origin, length and width. The toString() method should return:
Rectangle(7, 5, 4, 5)
Hint: The toString() output assumes a Rectangle object with origin field pointing to a Point2D object with values 7 and 5 in its x_coord and y_coord fields, respectively. The length and width fields are set to 4 and 5, respectively.
- The Square class has no fields as it is a Rectangle with equal width and height values. Define the full-argument, no-argument and copy instructors in the Square class. Make sure to call the full-argument constructor of the Rectangle class in the full-argument constructor of the Square class. Override the toString() method to return:
Square(9, 2, 11)
Note: The toString() in Square must call the toString() method in Rectangle.
- The Circle class has one int field: radius. Add getter and setter methods to the Circle class. Override toString() and equals() methods in Circle class. Define the full-argument, no-argument and copy instructors in the Circle class. Make sure to call the full-argument constructor of the Shape class in the full-argument constructor of the Circle class. Two Circle objects are equal if they have the same origin and radius. The toString() method should return:
Circle(7, 5, 11)
Hint: The toString() output assumes a Circle object with origin field pointing to a Point2D object with values 7 and 5 in its x_coord and y_coord fields, respectively. The radius field is set to 11.
Note: The toString() in Circle must call the toString() method in Shape.
- Write a test class where you will create an array with 10 Shape references. Create random shapes (Rectangle, Circle and Square). Make sure to show the use of all the classes methods in your code
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
