Question

In: Computer Science

Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive...

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.)

Solutions

Expert Solution

/***********************************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 :)


Related Solutions

Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create...
Please do this in C++ 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...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the opportunity to overload or override an inherited member function. What is the difference? and which one is the better?
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
Lab 7   - Rectangle class-   (Lec. 7) 1.) Create a new project and name it:    Rectangle...
Lab 7   - Rectangle class-   (Lec. 7) 1.) Create a new project and name it:    Rectangle /* OUTPUT: Enter the width of the court: 60 Enter the length of the court: 120 The width of the court is 60 feet. The length of the court is 120 feet. The area of the court is 7200 square feet. Press any key to continue . . . */ 2.) Create the following 3 files: Rectangle.h Rectangle.cpp Source.cpp 3.) Write a program that...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home,...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home, vehicle , life 2 member functions should be included: - calcpremium    - Home- 0.5% of the value of the home    - Life- 1% of the value of the policy    - Vehicle - 0.5% of the value of the policy calcCommission Home- 0.2% of the value of the home Life- 1% of the value of the policy Vehicle - 0.5% of the...
Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In...
Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In the base class, provide methods that are common to all rodents based on behaviours you find with a quick Internet search. Be sure to document the behaviours you implement (e.g., eat, sleep, groom, move, etc.). Each behaviour should print its action to standard output (e.g., rodent eating). Next, refine these behaviours in the child classes to perform different behaviours, depending on the specific type...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT