0% found this document useful (0 votes)
29 views7 pages

Shape Class Hierarchy for Paint Calculation

This document describes a lab exercise to develop a class hierarchy for shapes (Sphere, Rectangle, Cylinder) that inherits from an abstract Shape class. It involves writing code for the Shape class and subclasses to calculate surface areas, as well as a Paint class to calculate the amount of paint needed for each shape based on its area and the paint coverage. The code provided implements these classes and a PaintThings class to instantiate shape objects, call the paint amount method, and output the results.

Uploaded by

Ansh Verma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views7 pages

Shape Class Hierarchy for Paint Calculation

This document describes a lab exercise to develop a class hierarchy for shapes (Sphere, Rectangle, Cylinder) that inherits from an abstract Shape class. It involves writing code for the Shape class and subclasses to calculate surface areas, as well as a Paint class to calculate the amount of paint needed for each shape based on its area and the paint coverage. The code provided implements these classes and a PaintThings class to instantiate shape objects, call the paint amount method, and output the results.

Uploaded by

Ansh Verma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TASK – 5

BCSE103E

Ansh Verma
21BEE0071
QUES)
In this lab exercise you will 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).
Do the following.
1. Write an abstract class Shape with the following properties:
a. An instance variable shapeName of type String
b. An abstract method area()
c. A toString method that returns the name of the shape
2. The file [Link] contains a class for a sphere which is a descendant of Shape. A sphere
has a radius and its area (surface area) is given by the formula 4*PI*radius^2. Define similar
classes for a rectangle and a cylinder. Both the Rectangle class and the Cylinder class are
descendants of the Shape class. A rectangle is defined by its length and width and its area is
length times width. A cylinder is defined by a radius and height and its area (surface area) is
PI*radius^2*height. Define the toString method in a way similar to that for the Sphere class.
3. The file [Link] contains a class for a type of paint (which has a “coverage” and a
method to compute the amount of paint needed to paint a shape). Correct the return
statement in the amount method so the correct amount will be returned.
Use the fact that the amount of paint needed is the area of the shape divided by the
coverage for the paint. (NOTE: Leave the print statement - it is there for illustration
purposes, so you can see the method operating on different types of Shape objects.)
4. The file [Link] contains a program that computes the amount of paint needed
to paint various shapes. A paint object has been instantiated.
Add the following to complete the program:
a. Instantiate the three shape objects: deck to be a 20 by 35 foot rectangle, bigBall to be a
sphere of radius 15, and tank to be a cylinder of radius 10 and height 30.
b. Make the appropriate method calls to assign the correct values to the three amount
variables.
c. Run the program and test it. You should see polymorphism in action as the amount
method computes the amount of paint for various shapes.
CODE:
package verma;
public abstract class Shape
{
protected String shapeName;
public Shape (String shapeName)
{
[Link] = shapeName;
}
abstract double area();
public String toString()
{
[Link] = shapeName;
return shapeName;
}
}

public class Sphere extends Shape


{
private double radius;
public Sphere(double radius)
{
super("Sphere");
[Link] = radius;
}

public double area()


{
return 4*[Link]*radius*radius;
}
}
public class Cylinder extends Shape
{
private double radius;
private double height;
public Cylinder (double radius, double height)
{
super("Cylinder");
[Link] = radius;
[Link] = height;
}
public double area()
{
return [Link] * radius * radius * height;
}
}
public class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle (double length, double width)
{
super("Rectangle");
[Link] = length;
[Link] = width;
}
public double area()
{
return length * width;
}
}
public class Paint
{
private double coverage;
public Paint(double coverage)
{
[Link] = coverage;
}

public double amount(Shape s)


{
[Link] ("Computing amount for " +
s);
return [Link]()/coverage;
}
}
import [Link];
public class PaintThings
{
public static void main (String[] args)
{
final double COVERAGE = 350;
Paint paint = new Paint(COVERAGE);
Rectangle deck = new Rectangle (20,35);
Sphere bigBall = new Sphere (15);
Cylinder tank = new Cylinder (10,30);
double deckAmt = [Link](deck);
double ballAmt = [Link](bigBall);
double tankAmt = [Link](tank);

DecimalFormat fmt = new DecimalFormat("0.#");


[Link] ("\nNumber of gallons of paint
needed...");
[Link] ("Deck " + [Link](deckAmt));
[Link] ("Big Ball " +
[Link](ballAmt));
[Link] ("Tank " + [Link](tankAmt));
}
}

OUTPUT:
Class Shape:
Class Rectangle:

Class Sphere:

Class Cylinder:
Class Paint:

Class PaintThings:

Common questions

Powered by AI

In the provided design, inheritance is used to allow different shape classes (`Sphere`, `Rectangle`, and `Cylinder`) to inherit common characteristics from the `Shape` class. Each derived class inherits the `shapeName` attribute and the `toString` method. By defining `area()` as an abstract method in the base class `Shape`, each subclass is required to implement its specific logic for calculating area. This not only prevents code duplication by providing shared functionality through inheritance but also allows each subclass to incorporate specific details related to its geometry type .

To enhance the `PaintThings` program, several improvements could be made. Firstly, correcting the `area()` formula for the `Cylinder` class to correctly compute the surface area would ensure accuracy. Additionally, introducing constants for mathematical values like `Math.PI` and configuring the `coverage` value to be dynamically set or read from an external configuration file could enhance flexibility. Implementing an interface for paintable objects could allow further extension. Lazy loading or caching computed areas in shape objects could also improve performance if the same shape's area is calculated multiple times .

The `PaintThings` program adheres to the principle of cohesion by ensuring each class has a clear, focused responsibility. For example, the `Shape` classes are responsible for defining properties and behaviors related to different shapes, while the `Paint` class handles paint coverage computation, ensuring high cohesion. The program exhibits low coupling by using interfaces to interact between objects, such as using the `Shape` interface in the `Paint` class's `amount()` method. This reduces dependency between classes, allowing individual parts of the system to change without affecting others, facilitating maintenance and scalability .

The `PaintThings` program demonstrates encapsulation by creating instances of `Rectangle`, `Sphere`, and `Cylinder` classes, and interacting with these objects through defined interfaces without directly accessing their internal states. For example, the program calculates the amount of paint needed by calling the `area()` method on each shape object and `amount()` on the `Paint` object. This ensures that details of how the area is calculated for each shape (e.g., through specific formulas) remain hidden from the `main` method, emphasizing encapsulation where each class manages its own attributes and operations .

The implementation of the `Shape` class enforces the principle of abstraction by defining it as an abstract class with an abstract method `area()`. This abstract method does not provide any implementation detail, thereby hiding the specifics of how area calculations are performed for each shape. Subclasses such as `Sphere`, `Rectangle`, and `Cylinder` are then required to provide concrete implementations of the `area()` method, which allows them to define how the area is specifically calculated for each shape .

Having the `area` method as abstract in the `Shape` class is beneficial because it enforces a uniform interface for all shapes, ensuring that every subclass implements its own version of the `area()` method. This design allows for flexibility in defining different formulas for different shapes, encapsulates the calculation logic within the respective classes, and promotes code reusability and scalability. It also provides a mechanism to prevent instantiation of the `Shape` class itself, which is appropriate given its role as a template for specific shape classes .

Polymorphism is demonstrated in the `PaintThings` program through the `amount` method of the `Paint` class, which operates on a reference of the type `Shape`. This reference can be to any object of a subclass of `Shape` (like `Rectangle`, `Sphere`, or `Cylinder`). For instance, when `paint.amount(deck)` is called, polymorphism ensures that the `area()` method for `Rectangle` is called. Similarly, for `paint.amount(bigBall)`, the `Sphere`'s `area()` method is invoked, and for `paint.amount(tank)`, the `Cylinder`'s `area()` method is executed. This illustrates runtime polymorphism, where the method to be executed is determined at runtime based on the object's actual class .

The `Paint` class in this design serves as a utility to compute the amount of paint required to cover various shapes. It has a key component, `coverage`, which represents the amount of area a single unit of paint can cover. The class provides the `amount()` method that takes a `Shape` object as input and calculates the required paint by dividing the shape's area by the paint's coverage. This design encapsulates the logic for paint calculation separately from shape area logic, allowing for potential reuse with different shape types or coverage values .

The `toString` method in this class hierarchy provides a way to represent shape objects as strings. This method offers a simple mechanism to retrieve the name of the shape, allowing for easy identification and string representation when debugging or logging information. In the context of this design, it is particularly useful in the `amount` method of the `Paint` class, which prints the shape being processed, leveraging polymorphic behavior to dynamically retrieve the correct shape name via the overridden `toString` based on the actual object's type .

The formula used for calculating the area of a `Cylinder` in this design, indicated as `Math.PI * radius * radius * height`, is incorrect for surface area. This formula calculates the volume of the cylinder instead. For surface area, the correct formula should be `2 * Math.PI * radius * (radius + height)`, which considers both the lateral surface area and the top and bottom circles of the cylinder. An improvement would be to use the correct formula to accurately compute the surface area required for paint coverage .

You might also like