Shape -color :String = \"red\" -filled: boolean - true +Shape() +Shape (color:St
ID: 3878825 • Letter: S
Question
Shape -color :String = "red" -filled: boolean - true +Shape() +Shape (color:String, filled: boolean) +getColor():String +setColor (color:String) void +isFilled ():boolean +setFilled(filled:boolean):void +toString):String Circle Rectangle -width : double 1.0 -length : double = 1.0 +Rectangle() +Rectangle(width:double, -radius : double = 1.0 +Circle() +Circle(radius: double) +Circle(radius: double, color: String,filled:boolean) +getRadius():double +setRadius(radius:double):void +getArea():double +getPerimeter):double +toString ():String length:double) +Rectangle(width:double, length:double, color: String,filled:boolean) +getwidth):double +setwidth (width:double):void +getLength):double +setLength(legnth:double):void getArea():double +getPerimeter():double +toString ():String Square +Square() +Square (side:doubleExplanation / Answer
Shape.java
public class Shape {
//Declaring instance variables
private String color;
private boolean filled;
//Zero argumented constructor
public Shape() {
this.color = "red";
this.filled = true;
}
// getters and setters
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;
}
//Parameterized constructor
public Shape(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "A Shape with color of "+color+" and "+filled;
}
}
_______________
Circle.java
public class Circle extends Shape {
private double radius;
public Circle() {
this.radius = 1.0;
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius,String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
public double getArea()
{
return Math.PI*radius*radius;
}
public double getPerimter()
{
return 2*Math.PI*radius;
}
@Override
public String toString() {
return "A Circle with radius "+radius+" which is a subclass of "+super.toString();
}
}
__________________
Rectangle.java
public class Rectangle extends Shape {
//Declaring instance variables
private double width;
private double length;
//Zero argumented constructor
public Rectangle()
{
this.length=1.0;
this.width=1.0;
}
//Parameterized constructor
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
//Parameterized constructor
public Rectangle(String color, boolean filled, double width, double length) {
super(color, filled);
this.width = width;
this.length = length;
}
//getters and setters
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
//This method will calculate the area of the Rectangle
public double getArea()
{
return length*width;
}
//This method will calculate the Perimeter of the Rectangle
public double getPerimeter()
{
return 2*(length+width);
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "A Rectangle with width="+getWidth()+" and length="+getLength()+" which is a subclass of "+super.toString();
}
}
__________________
Square.java
public class Square extends Rectangle {
//Zero argumented constructor
public Square()
{
}
//Parameterized constructor
public Square(double side)
{
super(side,side);
}
//Parameterized constructor
public Square(double side,String color, boolean filled) {
super(color, filled,side,side);
}
// getters and setters
public void setWidth(double side)
{
setWidth(side);
}
public void setLength(double side)
{
setLength(side);
}
public void setSide(double side)
{
setLength(side);
}
//This method will calculate the area of the Square
@Override
public double getArea()
{
return getLength()*getWidth();
}
//This method will calculate the Perimeter of the Square
@Override
public double getPerimeter()
{
return 4*getLength();
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "A Square with side="+getLength()+" which is a subclass of "+super.toString();
}
}
____________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an Instance of Circle class Object
Shape c=new Circle(5.5,"Blue", true);
//Creating an Instance of Rectangle class Object
Shape r=new Rectangle("Pink",true,3,4);
//Creating an Instance of Square class Object
Shape s=new Square(5,"Green",true);
System.out.println(c.toString());
System.out.println(r.toString());
System.out.println(s.toString());
}
}
__________________
Output:
A Circle with radius 5.5 which is a subclass of A Shape with color of Blue and true
A Rectangle with width=3.0 and length=4.0 which is a subclass of A Shape with color of Pink and true
A Square with side=5.0 which is a subclass of A Rectangle with width=5.0 and length=5.0 which is a subclass of A Shape with color of Green and true
___________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.