0% found this document useful (0 votes)
62 views

Task - 5 BCSE103E: Ansh Verma 21BEE0071

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Task - 5 BCSE103E: Ansh Verma 21BEE0071

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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 Sphere.java 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 Paint.java 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 PaintThings.java 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)
{
this.shapeName = shapeName;
}
abstract double area();
public String toString()
{
this.shapeName = shapeName;
return shapeName;
}
}

public class Sphere extends Shape


{
private double radius;
public Sphere(double radius)
{
super("Sphere");
this.radius = radius;
}

public double area()


{
return 4*Math.PI*radius*radius;
}
}
public class Cylinder extends Shape
{
private double radius;
private double height;
public Cylinder (double radius, double height)
{
super("Cylinder");
this.radius = radius;
this.height = height;
}
public double area()
{
return Math.PI * radius * radius * height;
}
}
public class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle (double length, double width)
{
super("Rectangle");
this.length = length;
this.width = width;
}
public double area()
{
return length * width;
}
}
public class Paint
{
private double coverage;
public Paint(double coverage)
{
this.coverage = coverage;
}

public double amount(Shape s)


{
System.out.println ("Computing amount for " +
s);
return s.area()/coverage;
}
}
import java.text.DecimalFormat;
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 = paint.amount(deck);
double ballAmt = paint.amount(bigBall);
double tankAmt = paint.amount(tank);

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


System.out.println ("\nNumber of gallons of paint
needed...");
System.out.println ("Deck " + fmt.format(deckAmt));
System.out.println ("Big Ball " +
fmt.format(ballAmt));
System.out.println ("Tank " + fmt.format(tankAmt));
}
}

OUTPUT:
Class Shape:
Class Rectangle:

Class Sphere:

Class Cylinder:
Class Paint:

Class PaintThings:

You might also like