You have to create a program that can compute the Surface Area and Volumes of Pe
ID: 3694650 • Letter: Y
Question
You have to create a program that can compute the Surface Area and Volumes of
Perimeter Calculations (for your getTopPerimeter methods)
Circle-
return 2.0 * Math.PI * radius;
Rectangle-
return 2.0 * (width + length);
Triange-
return side_a + side_b + side_c;
Regular Polygon-
return numSides * side;
Area Calculations (for your getTopArea methods)
Circle-
return Math.PI * radius * radius;
Rectangle-
return width * length;
Triange-
double s = (side_a + side_b + side_c)/2.0;
return Math.sqrt(s*(s-side_a)*(s-side_b)*(s-side_c));
Regular Polygon-
return numSides * side * side / ( 4*Math.tan(Math.PI/numSides));
Explanation / Answer
public class Driver {
public static void main(String[] args) {
ContainerCollection cc = new ContainerCollection(10);
cc.addContainer(new CircularContainer(10., 2.0));
cc.addContainer(new RectangularContainer(10., 2.0, 3.0));
cc.addContainer(new TriangularContainer(10., 4.0, 3.0, 5.0));
cc.addContainer(new RegularPolygonContainer(10., 1.0, 4));
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.690, 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());
}
}
CircularContainer.j
public class CircularContainer extends Container {
private double radius;
public CircularContainer(double height, double raduis){
super(height);
this.radius = raduis;
}
@Override
public double getTopArea() {
return (Math.PI * radius * radius);
}
@Override
public double getTopPerimeter() {
return (2.0*Math.PI * radius);
}
}
ContainerCollection.java
public class ContainerCollection {
private Container[] collection;
private int numContainers;
private int containerCount = 0;
public ContainerCollection(int num) {
collection = new Container[(numContainers = num)];
}
public void addContainer( Container c) {
if(containerCount >= numContainers)
{
System.err.println("Can not add the given container " );
return ;
}
collection[containerCount++] = c;
}
public double getTotalVolume() {
double sum = 0;
for (int i = 0; i < containerCount; i++)
sum += collection[i].getVolume();
return sum;
}
public double getTotalSurfaceArea() {
double sum = 0;
for (int i = 0; i < containerCount; i++)
sum += collection[i].getSurfaceArea();
return sum;
}
}
Container.java
public abstract class Container {
private double height;
public Container(double height) {
this.height = height;
}
public abstract double getTopArea();
public abstract double getTopPerimeter();
public double getVolume() {
return height * getTopArea();
}
public double getSurfaceArea() {
return ((2.0 * getTopArea()) + (height * getTopPerimeter()));
}
}
RectangularContainer.java
public class RectangularContainer extends Container {
private double width;
private double lenght;
public RectangularContainer(double height, double width, double len) {
super(height);
this.width = width; this.lenght = len;
}
@Override
public double getTopArea() {
return (width * lenght);
}
@Override
public double getTopPerimeter() {
return (2.0 * (lenght + width));
}
}
RegularPolygonContainer.java
public class RegularPolygonContainer extends Container {
private double numSides;
private double side;
public RegularPolygonContainer(double height , double side, double num) {
super(height); this.side = side; numSides = num;
}
@Override
public double getTopArea() {
return ((numSides * side * side) / ( 4*Math.tan(Math.PI/numSides)));
}
@Override
public double getTopPerimeter() {
return (numSides * side);
}
}
TriangularContainer.java
public class TriangularContainer extends Container {
private double side_a;
private double side_b;
private double side_c;
public TriangularContainer(double height, double a, double b , double c) {
super(height);
side_a = a; side_b = b; side_c = c;
}
@Override
public 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));
}
@Override
public double getTopPerimeter() {
return (side_a+side_b+side_c);
}
}
sample output
Can not add the given container
Can not add the given container
Total Volume of all containers = 420.6283004441059
Total Surface Area of all containers = 763.0265241302609
Can not add the given container
Total Volume of all containers for cc2 = 88.14
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.