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

By Using Java Program (NetBeans). In this exercise, Shape all be defined as an a

ID: 3776840 • Letter: B

Question

By Using Java Program (NetBeans).

In this exercise, Shape all be defined as an abstract class, Which contains :

* Two protected instance variables color(String) and filled (boolean). the protected variables can be accessed by its subclasses and classes in the same package. they are denoted with a '#' sign in the class diagram.

* Getter and setter for all the instance variables, and toSring().

* Two abstract methods getArea() and getPerimeter() ( shown in italics in the class diagram ) .

* Subclasses Cricle and Rectangle shall override the abstract methods getArea() getPerimeter() and provide the proper implementation

They also override the toString().

Write a test class to test these statments involving polymorphism.

Explanation / Answer

Shape.java

public abstract class Shape {
   //Declaring variables
   protected String color;
   protected boolean filled;
  
   //Zero Argumented constructor
   public Shape() {
  
   }
  
   //Parameterized constructor
   public Shape(String color, boolean filled) {
       super();
       this.color = color;
       this.filled = filled;
   }
  
   //Setters and getters
   public String getColor() {
       return color;
   }
   public void setColor(String color) {
       this.color = color;
   }
   public boolean isFilled() {
       return filled;
   }
   public void setFilled(boolean filled) {
       this.filled = filled;
   }
  
   //Declaring abstract methods
   public abstract double getArea();
  
   public abstract double getPerimeter();
  
   /* toString() method is used to display
   * the contents of an object inside it.
   */
   @Override
   public String toString() {
       return " Shape# Color=" + color + " Filled=" + filled;
   }
        

}

______________

Circle.java

public class Circle extends Shape {

   //Declaring variables
   private double radius;
  
   //Zero Argumented Constructor
   public Circle() {
  
   }

   //Parameterized constructor
   public Circle(double radius) {
       super();
       this.radius = radius;
   }

   //Parameterized constructor
   public Circle(double radius,String color, boolean filled) {
       super(color, filled);
       this.radius=radius;
   }

//Setters and getters
   public double getRadius() {
       return radius;
   }

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

   @Override
   public double getArea() {
      
       return Math.PI*getRadius()*getRadius();
   }

   @Override
   public double getPerimeter() {
      
       return 2*Math.PI*getRadius();
   }

   /* toString() method is used to display
   * the contents of an object inside it.
   */
   @Override
   public String toString() {
       return " Circle# Radius=" + radius + " Color=" + color + " Filled="
               + filled;
   }

}

__________________

Rectangle.java

public class Rectangle extends Shape {

   //Declaring variables
   protected double length;
   protected double width;
  
   //Zero Argumented Constructor
   public Rectangle() {
       super();
   }

   //Parameterized constructor
   public Rectangle(double length, double width) {
       this.length = length;
       this.width = width;
   }

   //Parameterized constructor
   public Rectangle(double length, double width,String color, boolean filled) {
       super(color, filled);
       this.length = length;
       this.width = width;
   }

   //Setters and getters
   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 getArea() {
       return getLength()*getWidth();
   }

   @Override
   public double getPerimeter() {

       return 2*(getLength()+getWidth());
   }

   /* toString() method is used to display
   * the contents of an object inside it.
   */
   @Override
   public String toString() {
       return " Rectangle# Length=" + length + " Width=" + width + " Color="
               + color + " Filled=" + filled;
   }
  
  
  

}

_________________________

Square.java

public class Square extends Rectangle {
   private double side;

   //Zero Argumented Constructor
   public Square() {
       super();
   }

   //Parameterized constructor
   public Square(double side) {
       super(side,side);
      
   }

   //Parameterized constructor
   public Square(double side, String color, boolean filled) {
       super(side, side, color, filled);
      
   }
   //Setters and getters
public double getSide()
{
   return side;
}

   public void setSide(double side) {
       this.side = side;
   }
  
   public void setLength(double length) {
       this.length = length;
   }
   public void setWidth(double width) {
       this.width = width;
   }

   /* toString() method is used to display
   * the contents of an object inside it.
   */
   @Override
   public String toString() {
       return " Square# Side=" + side + " Length=" + length + " Width="
               + width + " Color=" + color + " Filled=" + filled;
   }
  

}

______________________

Test.java

public class Test {

   public static void main(String[] args) {
      
   //Creating an Array of Shape objects
   Shape s[]={new Circle(5.5,"Blue",true),
           new Rectangle(4,5,"Red",true),
           new Square(3.5,"Green",true)};
  
   //Displaying the contents of Each object by using the for loop
   for(int i=0;i<s.length;i++)
   {
       System.out.println(s[i].toString());
       System.out.println("_____________");
   }

   }

}

____________________

Output:


Circle#
Radius=5.5
Color=Blue
Filled=true
_____________

Rectangle#
Length=4.0
Width=5.0
Color=Red
Filled=true
_____________

Square#
Side=0.0
Length=3.5
Width=3.5
Color=Green
Filled=true
_____________

___________Thank You