183k views
4 votes
You have to create a program that can compute the Surface Area and Volumes of various containers that are all "Right Prisms". This means that the ends of the container are identical and the sides are perpendicular to the ends. Each of your containers has different shapes: Circular, Rectangular, Triangular, and Regular Polygon. All of these containers are derived from a common abstract Container class.

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

abstract class Container {

double height;

Container(double height)

{

this.height = height;

}

abstract double getTopArea();

abstract double getTopPerimeter();

double getVolume()

{

return height * getTopArea();

}

double getSurfaceArea()

{

return 2*getTopArea() + height * getTopPerimeter();

}

}

class CircularContainer extends Container

{

// add appropriate data definitions

double radius;

CircularContainer(double height, double radius)

{

super(height);

this.radius = radius;

}

// implement required abstract methods

double getTopArea()

{

return Math.PI * radius * radius;

}

double getTopPerimeter()

{

return 2.0 * Math.PI * radius;

}

}

class RectangularContainer extends Container

{

// add appropriate data definitions

double width;

double length;

RectangularContainer(double height, double width, double length)

{

// Fill in details

super(height);

this.width = width;

this.length = length;

}

// implement required abstract methods

double getTopArea()

{

return width * length;

}

double getTopPerimeter()

{

return 2.0 * (width + length);

}

}

class TriangularContainer extends Container

{

// add appropriate data definitions

double side_a, side_b, side_c;

TriangularContainer(double height, double side_a, double side_b, double side_c)

{

// Fill in details

super(height);

this.side_a = side_a;

this.side_b = side_b;

this.side_c = side_c;

}

// implement required abstract methods

double getTopArea()

{

double s = (side_a + side_b + side_c)/2.0;

return Math.sqrt(s*(s-side_a)*(s-side_b)*(s-side_c));

}

double getTopPerimeter()

{

return side_a + side_b + side_c;

}

}

class RegularPolygonContainer extends Container

{

// add appropriate data definitions

double side;

int numSides;

RegularPolygonContainer(double height, double side, int numSides)

{

// Fill in details

super(height);

this.side = side;

this.numSides =numSides;

}

// implement required abstract methods

double getTopArea()

{

return numSides * side * side / ( 4*Math.tan(Math.PI/numSides));

}

double getTopPerimeter()

{

return numSides * side;

}

}

class ContainerCollection

{

Container[] collection;

int numContainers;

int containerCount;

double totalv;

double totala;

ContainerCollection(int numContainers)

{

// Fill in details

this.numContainers = numContainers;

this.containerCount =0;

this.totalv=0;

this.totala=0;

collection = new Container[numContainers];

}

void addContainer( Container c) throws NullPointerException

{

// Fill in details

// Don't forget to check to see if there is enough room

// in collection before adding. Complain if you can't add Container.

if(containerCount>=numContainers)

{

System.out.println("Sorry no more containers allowed");

}

else

{

collection[containerCount] = c;

containerCount++;

}

}

double getTotalVolume()

{

// Fill in details to return the total volume of

// all Containers in collection

for(int i=0;i<containerCount;i++)

{

totalv+=collection[i].getVolume();

}

return totalv;

}

double getTotalSurfaceArea()

{

// Fill in details to return the total surface area of

// all Containers in collection

for(int i=0;i<containerCount;i++)

{

totala+=collection[i].getSurfaceArea();

}

return totala;

}

public static void main(String[] args) throws Exception

{

ContainerCollection cc = new ContainerCollection(10);

cc.addContainer(new CircularContainer(10., 2.0)); // height, radius

cc.addContainer(new RectangularContainer(10., 2.0, 3.0)); // height, width, length

cc.addContainer(new TriangularContainer(10., 4.0, 3.0, 5.0)); // height, side_a, side_b, side_c

cc.addContainer(new RegularPolygonContainer(10., 1.0, 4)); // height, side, num_sides

cc.addContainer(new CircularContainer(5., 2.0)); // height, radius

cc.addContainer(new RectangularContainer(5., 2.0, 3.0)); // height, width, length

cc.addContainer(new TriangularContainer(5., 4.0, 3.0, 5.0)); // height, side_a, side_b, side_c

cc.addContainer(new RegularPolygonContainer(5., 1.0, 4)); // height, side, num_sides

cc.addContainer(new CircularContainer(2., 2.0)); // height, radius

cc.addContainer(new RectangularContainer(2., 2.0, 3.0)); // height, width, length

cc.addContainer(new TriangularContainer(2., 4.0, 3.0, 5.0)); // height, side_a, side_b, side_c

cc.addContainer(new RegularPolygonContainer(2., 1.0, 4)); // height, side, num_sides

System.out.println("Total Volume of all containers = " + cc.getTotalVolume());

System.out.println("Total Surface Area of all containers = " + cc.getTotalSurfaceArea());

ContainerCollection cc2 = new ContainerCollection(2);

cc2.addContainer(new RectangularContainer(5.504, 2.0, 3.0)); // height, width, length

cc2.addContainer(new RectangularContainer(9., 2.0, 3.0)); // height, width, length

cc2.addContainer(new RectangularContainer(8., 2.0, 3.0)); // height, width, length

System.out.println("Total Volume of all containers for cc2 = " + cc2.getTotalVolume());

}

}

User Thusitha Deepal
by
4.4k points