In: Computer Science
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior
1. Create a Rectangle
2. Class Derive a Square Class from the Rectangle Class
3. Derive a Right Triangle Class from the Rectangle Class
4. Create a Circle Class
5. Create a Sphere Class
6. Create a Prism Class
7. Define any other classes necessary to implement a solution
8. Define a Program Driver Class for Demonstration
(Create a Container to hold objects of the types described above Create and Add two(2) objects of each type described above Compute the area, perimeter, and volume in a uniform manner for each object in the Container. Display the results. Note: the perimeter is not defined for a 3D object and volume is not defined for a 2D object.)
/***********************************Shape2D.java************************/
public interface Shape2D {
public double calculatePerimeter();
public double calculateArea();
}
/*****************************Shape3D.java********************************/
public interface Shape3D {
public double calculateVolume();
public double calculateSurfaceArea();
}
/***********************************************Rectangle.java***************************/
public class Rectangle implements Shape2D {
//data field for class Rectangle
private double length;
private double width;
//no argument constructor
public Rectangle() {
this.length = 1;
this.width = 1;
}
/**
*
* @param length
* @param width
*/
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double calculatePerimeter() {
return 2 * (length + width);
}
@Override
public double calculateArea() {
return length * width;
}
@Override
public String toString() {
return "Rectangle [length=" +
length + ", width=" + width + ", perimeter= " +
calculatePerimeter() + ", area= "
+ calculateArea() + "]";
}
}
/*****************************Square.java************************************/
public class Square extends Rectangle implements Shape2D {
public Square() {
super();
}
/**
*
* @param side
*/
public Square(double side) {
super(side, side);
}
@Override
public double calculatePerimeter() {
return 4 * getLength();
}
@Override
public double calculateArea() {
return getLength() *
getWidth();
}
@Override
public String toString() {
return "Square [side= " +
getLength() + ", perimeter= " + calculatePerimeter() + ", area= " +
calculateArea()
+ "]";
}
}
/***********************************RightTriangle.java*****************************/
public class RightTriangle extends Rectangle implements Shape2D
{
public RightTriangle() {
super();
}
/**
*
* @param base
* @param height
*/
public RightTriangle(double base, double height) {
super(base, height);
}
@Override
public double calculatePerimeter() {
return getLength() + getWidth()
+ Math.sqrt(Math.pow(getLength(), 2) + Math.pow(getWidth(),
2));
}
@Override
public double calculateArea() {
return getLength() * getWidth()
/ 2;
}
@Override
public String toString() {
return "RightTriangle [base=" +
getLength() + ", height=" + getWidth() + ", perimeter= " +
calculatePerimeter()
+ ", area= " + calculateArea() + "]";
}
}
/************************************Circle.java*************************/
public class Circle implements Shape2D {
//data field
private double radius;
public Circle() {
this.radius = 1;
}
/**
*
* @param radius
*/
public Circle(double radius) {
super();
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI *
radius;
}
@Override
public double calculateArea() {
return Math.PI * radius *
radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius +
", perimeter= " + calculatePerimeter() + ", area= " +
calculateArea() + "]";
}
}
/*************************************Sphere.java****************************/
public class Sphere implements Shape3D {
//data field
private double radius;
public Sphere() {
super();
this.radius = 1;
}
/**
*
* @param radius
*/
public Sphere(double radius) {
this.radius = radius;
}
//getter and setter
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double calculateVolume() {
return ((Math.PI *
Math.pow(radius, 3)) * 4) / 3;
}
@Override
public double calculateSurfaceArea() {
return 4 * Math.PI *
Math.pow(radius, 2);
}
@Override
public String toString() {
return "Sphere [radius=" + radius +
", surface area= " + calculateSurfaceArea() + ", volume= "
+ calculateVolume() + "]";
}
}
/********************************Prism.java***************************/
public class Prism implements Shape3D {
//data field
private double base, side1, side2, side3;
private double heightOfPrism, heightOfTrianle;
public Prism() {
this.base = 1;
this.side1 = 1;
this.side2 = 1;
this.side3 = 1;
this.heightOfTrianle = 1;
this.heightOfPrism = 1;
}
/**
*
* @param base
* @param side1
* @param side2
* @param side3
* @param heightOfPrism
* @param heightOfTrianle
*/
public Prism(double base, double side1, double side2,
double side3, double heightOfPrism, double heightOfTrianle) {
super();
this.base = base;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
this.heightOfPrism =
heightOfPrism;
this.heightOfTrianle =
heightOfTrianle;
}
//getter and setter
public double getBase() {
return base;
}
public void setBase(double base) {
this.base = base;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
public double getHeightOfPrism() {
return heightOfPrism;
}
public void setHeightOfPrism(double heightOfPrism)
{
this.heightOfPrism =
heightOfPrism;
}
public double getHeightOfTrianle() {
return heightOfTrianle;
}
public void setHeightOfTrianle(double
heightOfTrianle) {
this.heightOfTrianle =
heightOfTrianle;
}
@Override
public double calculateVolume() {
return base * heightOfTrianle *
heightOfPrism / 2;
}
@Override
public double calculateSurfaceArea() {
return ((base * heightOfTrianle)
+ ((side1 + side2 + side3) * heightOfPrism));
}
@Override
public String toString() {
return "Prism [base=" + base + ",
side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + ",
heightOfPrism="
+ heightOfPrism + ", heightOfTrianle=" +
heightOfTrianle + ", surface area= " + calculateSurfaceArea()
+ ", volume= " + calculateVolume() + "]";
}
}
/******************************Driver.java***********************/
import java.util.ArrayList;
public class Driver {
public static void main(String[] args) {
ArrayList<Shape3D>
container = new ArrayList<>();
ArrayList<Shape2D> container1
= new ArrayList<>();
container.add(new
Sphere(5.0));
container.add(new Prism(2.0, 3.0,
4.0, 5.0, 8.0, 5.0));
container1.add(new Rectangle(3.0,
4.0));
container1.add(new
Square(4.0));
container1.add(new
RightTriangle(3.0, 4.0));
container1.add(new
Circle(5.0));
System.out.println("2D Shape
objects: ");
for (Shape2D shape2d : container1)
{
System.out.println(shape2d.toString());
}
System.out.println("3D Shape
objects: ");
for (Shape3D shape3d : container)
{
System.out.println(shape3d.toString());
}
}
}
/*************************output******************************/
2D Shape objects:
Rectangle [length=3.0, width=4.0, perimeter= 14.0, area=
12.0]
Square [side= 4.0, perimeter= 16.0, area= 16.0]
RightTriangle [base=3.0, height=4.0, perimeter= 12.0, area=
6.0]
Circle [radius=5.0, perimeter= 31.41592653589793, area=
78.53981633974483]
3D Shape objects:
Sphere [radius=5.0, surface area= 314.1592653589793, volume=
523.5987755982989]
Prism [base=2.0, side1=3.0, side2=4.0, side3=5.0,
heightOfPrism=8.0, heightOfTrianle=5.0, surface area= 106.0,
volume= 40.0]
Please let me know if you have any doubt or modify the answer, Thanks :)