Question: Develop a class hierarchy of shapes and write a program that computes the amount of paint needed to paint different objects. The hierarchy will consist

Develop a class hierarchy of shapes and write a program that computes the amount of paint needed to paint different objects. The hierarchy will consist of a parent class Shape with three derived classes - Sphere, Rectangle, and Cylinder. For the purposes of this exercise, the only attribute a Shape will have is a name, and the method of interest will be one that computes the area of the shape (surface area in the case of three-dimensional shapes). You have been provided with an abstract class Shape.
You must produce output in the precise form shown in the "Example Interaction" below, and you must write and use (at least) the following public classes or methods as part of your solution:
(a) Define a class Sphere which inherits from Shape and implements its abstract methods. A sphere has an instance variable radius which is initialised in the parameterised constructor for the class. A sphere's area (surface area) is given by the formula 4**** radius ?2.
(b) Define similar classes for a rectangle and a cylinder. Both the class Rectangle and Cylinder are descendants of the class Shape. A rectangle is defined by its instance variables length and width which are initialised in its parameterised constructor, and its area is the length ** width. Similarly a cylinder is defined by a its instance variables radius and height which are initialized by its parameterised constructor, and its area (surface area) is ** radius ?2** height. Define the toString () method for each class in a way similar to that for the Sphere class.
(c) Define a class Paint which has an instance variable coverage which is initialized (e.g.350) by its parameterised constructor, and a method double amount (Shape) which takes in an object (Sphere, Rectangle, or Cylinder) as a parameter. The method computes and returns the amount of paint needed to paint a shape. Use the fact that the amount of paint needed is the area of the shape divided by the coverage for the paint. Hint - amount () should invoke the toString () method of the object.
(d) In your main () method instantiate three objects of type Shape: deck to be a 20x35 foot Rectangle, bigBall to be a Sphere of radius =15, and tank to be a Cylinder of radius =10 and height =30, and compute the amount of paint needed to paint each shape. Make the appropriate method calls to assign the correct values to the three amount () parameters by making use of Java's Polymorphic properties.
Example Interaction:
Computing amount for Rectangle of length 20.0 and width 35.0
Amount of paint required is 2.0
Computing amount for Sphere of radius 15.0
Amount of paint required is 8.078381109230897
Computing amount for Cylinder of radius 10.0 and height 30.0
Amount of paint required is 26.927937030769655
Develop a class hierarchy of shapes and write a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!