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

-The code needs to be written in Java -This is one question with subparts -Thank

ID: 670510 • Letter: #

Question

-The code needs to be written in Java
-This is one question with subparts
-Thank you in advance for your help with this question !

Assignment Inheritance CSIS-1410 Learning Objectives: Declare a subclass that derives from a superclass Demonstrate polymorphic behavior Declare a variable of the superclass type and assign it an instance of the subclass type Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible Create an array of superclass type and use a foreach loop to iterate through it Description: Write a program to demonstrate the use of inheritance and polymorphism You will create 4 classes: Rectangle, Square, IsoscelesRightTriangle, and Circle In addition you will create a class called InheritanceApp. This class includes the main method. Here we test the four other classes and we demonstrate the polymorphic behavior Declare the classes as described below: ad Retangle Rectangle has 2 private final fields of type int: length and width has (exactly) one parameterized constructor that initializes both fields It provides a getter (get accessor method) for each of the fields (no setter) It overrides the toString method so that it produces a result of the form Rectangle (lengthxwidth) · e.g. Rectangle (5x4) ad Square Square extends Rectangle No fields are declared in class Square It has a parameterized constructor with (only) one parameter The parameter is used to initialize both fields of Rectangle . It has a method called getSide to expose the side-length of the square Override the toString method so that it will return a String of the following form: Square (side) e.g. Square (4)

Explanation / Answer

public class Shape {

   // Private member variable

   private String color;

  

   // Constructor

   public Shape (String color) {

      this.color = color;

   }

      @Override

   public String toString() {

      return "Shape of color="" + color + """;

   }

  

   // All shapes must has a method called getArea()

   public double getArea() {

      System.err.println("Shape unknown! Cannot compute area!");

      return 0;   // Need a return to compile the program

   }

}

// Define Rectangle, subclass of Shape

public class Rectangle extends Shape {

   // Private member variables

   private int length;

   private int width;

  

   // Constructor

   public Rectangle(String color, int length, int width) {

      super(color);

      this.length = length;

      this.width = width;

   }

  

   @Override

   public String toString() {

      return "Rectangle of length=" + length + " and width=" + width + ", subclass of " + super.toString();

   }

  

   @Override

   public double getArea() {

      return length*width;

   }

}

// Define Triangle, subclass of Shape

public class Triangle extends Shape {

   // Private member variables

   private int base;

   private int height;

  

   // Constructor

   public Triangle(String color, int base, int height) {

      super(color);

      this.base = base;

      this.height = height;

   }

  

   @Override

   public String toString() {

      return "Triangle of base=" + base + " and height=" + height + ", subclass of " + super.toString();

   }

  

   @Override

   public double getArea() {

      return 0.5*base*height;

   }

}

// A test driver program for Shape and its subclasses

public class TestShape {

   public static void main(String[] args) {

      Shape s1 = new Rectangle("red", 4, 5);

      System.out.println(s1);

      System.out.println("Area is " + s1.getArea());

     

      Shape s2 = new Triangle("blue", 4, 5);

      System.out.println(s2);

      System.out.println("Area is " + s2.getArea());

   }

}