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

Write another subclass of Geometric Object called Rectangle. Save it in the file

ID: 3686917 • Letter: W

Question

Write another subclass of Geometric Object called Rectangle. Save it in the file Rectangle. java. Include member variables for the width and height of the rectangle. Make a constructor that takes the width and height as parameters. Finally, override the methods from Geometric Object, similar to what you did with Circle. Include an appropriate block comment at the top of your class. (like the one for Geometric Object above) that gives your name and section number. Part 3) write another subclass of Geometric Object called Rectangle. Save it in the file Rectangle. java. Include member variables for the width and height of the rectangle. Make a constructor that takes the width and height as parameters. Finally, override the methods from Geometric Object, similar to what you did with Circle. Include an appropriate block comment at the top of your class. Part 4) Write a third subclass of Geometric Object called Equilateral Triangle. Save it in the file Equilateral Triangle. java. Include a member variable for the side length of the triangle, and a constructor that takes the side length as a parameter. Override the methods from Geometric Object. Note that you may wish to use Heron's formula to compute the area of the triangle: Include a block comment at the top of your class.

Explanation / Answer

GeometricObject.java

/*
* GeometricObject abstract class
*
* */
public abstract class GeometricObject {
   public abstract double getArea();
   public abstract double getPerimeter();
   public String toString(){
       return String.format("Circle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
   }

}

Circle.java

/*
* Circle Area formula : PI * R * R
* Circle Perimeter formula: 2 * PI * R
*
* */
public class Circle extends GeometricObject{
   private double radius;
   private final double PI = 3.14;
   public double getRadius() {
       return radius;
   }
   public void setRadius(double radius) {
       this.radius = radius;
   }
   public Circle(double radius){
       this.radius = radius;
   }
   public double getArea(){
       return PI * (radius * radius);
   }
   public double getPerimeter(){
       return 2 * PI * radius;
   }
   public String toString(){
       return String.format("Circle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
   }  
}

Rectangle.java

/*
* Rectangle Area formula : width * height
* Rectangle Perimeter formula: 2 * (height + width)
*
* */
public class Rectangle extends GeometricObject{
   private double width;
   private double height;
   private final double PI = 3.14;
   public Rectangle(double width, double height){
       this.width = width;
       this.height = height;
   }
   public double getArea(){
       return width * height;
   }
   public double getWidth() {
       return width;
   }
   public void setWidth(double width) {
       this.width = width;
   }
   public double getHeight() {
       return height;
   }
   public void setHeight(double height) {
       this.height = height;
   }
   public double getPerimeter(){
       return 2 * (height + width);
   }
   public String toString(){
       return String.format("Rectangle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
   }

}

EquilateralTriangle.java

/*
* EquilateralTriangle Area formula : (Math.sqrt(3) / 4) * a * a
* EquilateralTriangle Perimeter formula: 3 * a
*
* */
public class EquilateralTriangle extends GeometricObject {
   private double sideLength;

   private final double PI = 3.14;
   public EquilateralTriangle(double sideLength){
       this.sideLength = sideLength;
   }
   public double getArea(){
       return (Math.sqrt(3) / 4) * sideLength * sideLength ;
   }
   public double getPerimeter(){
       return 3 * sideLength;
   }
   public String toString(){
       return String.format("EquilateralTriangle: Area = %.2f , Perimeter = %.2f ",getArea(),getPerimeter());
   }
   public double getSideLength() {
       return sideLength;
   }
   public void setSideLength(double sideLength) {
       this.sideLength = sideLength;
   }
}

ObjectCalculator.java


import java.util.ArrayList;

public class ObjectCalculator {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       ArrayList<GeometricObject> objects = new ArrayList<GeometricObject>();
       //Create a Scanner to read input from the keyboard
       java.util.Scanner scanner = new java.util.Scanner(System.in);
       //Prompt the user for a radius, get the radius from the keyboard, and add circle to the list of objects
       System.out.println("Enter the radius for Circle");
       double radius = scanner.nextDouble();
       Circle c = new Circle(radius);
       objects.add(c);

       //Prompt the user for a side length for a equilateral triangle.
       System.out.println("Enter the side length for Equilateral Triangle");
       double sideLength = scanner.nextDouble();
       EquilateralTriangle e = new EquilateralTriangle(sideLength);
       objects.add(e);      
      
       //Prompt the user for a width and height for a Rectangle.
       System.out.println("Enter the width for Ractangle");
       double width = scanner.nextDouble();
       System.out.println("Enter the height for Ractangle");
       double height = scanner.nextDouble();      
       Rectangle r = new Rectangle(width, height);
       objects.add(r);              
      
       //Write a for loop that will print out all of the objects in the list, along with their attributes
       for(int i=0; i<objects.size(); i++){
          
           GeometricObject obj = objects.get(i);
           if(obj instanceof Circle){
               Circle cc = (Circle) obj;
               System.out.println("Circle Radius :"+cc.getRadius());
           }
           else if(obj instanceof Rectangle){
               Rectangle rr = (Rectangle) obj;
               System.out.println("Rectangle Width :"+rr.getWidth()+" Height : "+rr.getHeight());              
           }
           else if(obj instanceof EquilateralTriangle){{
               EquilateralTriangle ee = (EquilateralTriangle) obj;
               System.out.println("EquilateralTriangle Side Length :"+ee.getSideLength());              
           }
       }

       }
         
       //Write a for loop that will calculate and print total perimeter and area
       for(int i=0; i<objects.size(); i++){
           GeometricObject obj = objects.get(i);
           System.out.println(obj.toString());
       }
  
   }

}

Output:

Enter the radius for Circle
2.4
Enter the side length for Equilateral Triangle
2
Enter the width for Ractangle
3
Enter the height for Ractangle
4
Circle Radius :2.4
EquilateralTriangle Side Length :2.0
Rectangle Width :3.0 Height : 4.0
Circle: Area = 18.09 , Perimeter = 15.07
EquilateralTriangle: Area = 1.73 , Perimeter = 6.00
Rectangle: Area = 12.00 , Perimeter = 14.00

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