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 threedimensional 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 radius
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 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 eg 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 x foot Rectangle, bigBall to be a Sphere of radius and tank to be a Cylinder of radius and height 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 and width
Amount of paint required is
Computing amount for Sphere of radius
Amount of paint required is
Computing amount for Cylinder of radius and height
Amount of paint required is
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
