Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create java program to do the following : Problem Description: Using the Geometr

ID: 3846735 • Letter: C

Question

Create java program to do the following :

Problem Description: Using the GeometricObject class example in the book, create the following sub classes:

GeometricObject -> Circle -> Cone

GeometricObject -> Triangle -> TriBasePyramid

GeometricObject -> Rectangle -> RectBasePyramid

GeometricObject -> Hexagon -> HexagoneBasePyramid

The classes must be designed to have appropriate data fields and methods with every possible way to instantiate an object (different constructors). The test program should instantiate objects of the above types and calculates their volumes. Remember each sub class adds more detail to the super class. For example area must be used to calculate the volume of circle but should it be in Cone class? Or should it be used from other class?

Requirements:

1. Must use inheritance

2. Must draw UML diagram

Explanation / Answer

public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
Rectangle.java

public class Rectangle extends Shape {
private final double width, length; //sides

public Rectangle() {
this(1,1);
}
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}

@Override
public double area() {
// A = w * l
return width * length;
}

@Override
public double perimeter() {
// P = 2(w + l)
return 2 * (width + length);
}

}
Circle.java

public class Circle extends Shape {
private final double radius;
final double pi = Math.PI;

public Circle() {
this(1);
}   
public Circle(double radius) {
this.radius = radius;
}

@Override
public double area() {
// A = r^2
return pi * Math.pow(radius, 2);
}

public double perimeter() {
// P = 2r
return 2 * pi * radius;
}
}
Triangle.java

public class Triangle extends Shape {
private final double a, b, c; // sides

public Triangle() {
this(1,1,1);
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}

@Override
public double area() {
// Heron's formula:
// A = SquareRoot(s * (s - a) * (s - b) * (s - c))
// where s = (a + b + c) / 2, or 1/2 of the perimeter of the triangle
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}

@Override
public double perimeter() {
// P = a + b + c
return a + b + c;
}
}
And this is what I used to test it all, which it all works as intended:

TestShape.java

public class TestShape {
public static void main(String[] args) {

// Rectangle test
double width = 5, length = 7;
Shape rectangle = new Rectangle(width, length);
System.out.println("Rectangle width: " + width + " and length: " + length
+ " Resulting area: " + rectangle.area()
+ " Resulting perimeter: " + rectangle.perimeter() + " ");

// Circle test
double radius = 5;
Shape circle = new Circle(radius);
System.out.println("Circle radius: " + radius
+ " Resulting Area: " + circle.area()
+ " Resulting Perimeter: " + circle.perimeter() + " ");

// Triangle test
double a = 5, b = 3, c = 4;
Shape triangle = new Triangle(a,b,c);
System.out.println("Triangle sides lengths: " + a + ", " + b + ", " + c
+ " Resulting Area: " + triangle.area()
+ " Resulting Perimeter: " + triangle.perimeter() + " ");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote