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

Write a program to calculate the perimeter and area of a shape. Define a Shape c

ID: 3691317 • Letter: W

Question

Write a program to calculate the perimeter and area of a shape.
Define a Shape class as an abstract class. The Shape class should have necessary fields and methods to calculate perimeter and area of the shape. Define Circle, Triangle, Rectangle classes by inheriting the Shape class and implements the abstract methods. Define a test class (TotalArea class) that will allow a user to input shapes. The TotalArea Class will not calculate the areas of any of the shapes; the CalcArea Methods must be called from the TotalArea Class and return the area values accordingly

Explanation / Answer

Shape.java


public abstract class Shape {

   protected String color = null;

   private int borderWidth = 0;

   public Shape() {
       System.out.println("This is Default Constructor");
   }

   public Shape(String name) {
       System.out.println(" You passed " + name + " in constructor");
   }

   public Shape(String c, int w) {
       color = c;
       borderWidth = w;
       System.out.println("Cunstructor : Color is " + c);
   }

   public int getBorderWidth() {
       return borderWidth;
   }

   public void setBorderWidth(int borderWidth) {
       this.borderWidth = borderWidth;
   }

   public String getColor() {
       return color;
   }

   public void setColor(String color) {
       this.color = color;
   }

   public double area() {
       return 0;
   }
}


Triangle.java

public class Triangle extends Shape {

   private int base = 0;
   private int height = 0;

   public int getBase() {
       return base;
   }

   public void setBase(int base) {
       this.base = base;
   }

   public int getHeight() {
       return height;
   }

   public void setHeight(int height) {
       this.height = height;
   }

   public double area() {
       return (base * height) / 2;
   }

}


Rectangle.java

public class Rectangle extends Shape {

   private int length;
   private int width;

   /**
   * @return the length
   */
   public int getLength() {
       return length;
   }

   /**
   * @param length
   *            the length to set
   */
   public void setLength(int length) {
       this.length = length;
   }

   /**
   * @return the width
   */
   public int getWidth() {

       return width;
   }

   /**
   * @param width
   *            the width to set
   */
   public void setWidth(int width) {
       this.width = width;
   }

   public double area() {
       return length * width;
   }

}


TestShape.java

public class TestShape {

   public static void main(String[] args) {
       testCircle();
       // testTriangle();

       Circle c = new Circle();
       Shape s = new Circle();

       Object o = new Circle();
       Object o1 = new Rectangle();

   }

   public static void testCircle() {
       Circle c = new Circle();
       c.setColor("Red");
       c.setBorderWidth(34);
       c.setRadius(3);

       System.out.println("Area is " + c.area());

   }

   public static void testTriangle() {
       Triangle t = new Triangle();
       t.setColor("Red");
       t.setBorderWidth(34);
       t.setBase(10);
       t.setHeight(100);

       System.out.println("Area is " + t.area());

   }

   public static void testShape() {

       Shape s = null; // Declaration
       // s = new Shape("Triangle"); // Instantiation
       s = new Circle();// Instantiation

       s.setColor("Lal");

       s.area();

       System.out.println("---------------");
       s.setColor("Red");
       // s.setBorderWidth(3);

       System.out.println(s.getBorderWidth());
   }
}


Circle.java
public class Circle extends Shape {

   private int radius;

   public int getRadius() {
       return radius;
   }

   public void setRadius(int radius) {
       this.radius = radius;
   }

   public double area() {

       return 3.14 * radius * radius;
   }

}


sample output

This is Default Constructor                                                                                                                                  
Area is 28.259999999999998                                                                                                                                   
This is Default Constructor                                                                                                                                  
This is Default Constructor                                                                                                                                  
This is Default Constructor                                                                                                                                  
This is Default Constructor                                                                                                                                  

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