Write a superclass called Shape(as shown in the class diagram), which contains: Two instance variables color(String) and filled (boolean). Two constructors: a no-arg (no-argument) constructor that initializes the colorto "green" and filled to true, and a two-argument constructor that initializes the color and filled to the parameters values. Getter and setter for all the instance variables. By convention, the getter for a booleanvariable filled is called isFilled() (instead of getFilled() for all the other types). A toString()method that returns "A << filled/Not filled >> Shape with << green >>". Methods getArea() andgetPerimeter() both reurn 0. // We can not write the implementation of getArea() and getPerimeter() until we know the specific shape.   There are two subclasses of Shapecalled Circle and Rectangle, as shown in the class diagram. Ignore the Circle class. The Rectangle class contains: Two instance variables width(double) and length (double). Three constructors as shown. The no-arg constructor initializes the widthand length to 0. All the constructors in Rectangle class should call the appropriate super-class (shape) constructors. Getter and setter for all the instance variables. Override methods getArea() -> length*widthand getPerimeter() -> 2*length + 2*width. Override the toString()method inherited, to return "A Rectangle with width=<< xxx >> and length= << zzz >>, which is a subclass of << yyy >>", where yyy is the output of the toString() method from the superclass.  Write a class called Square, as a subclass of Rectangle. Square has no instance variable, but inherits the instance variables width and length from its superclass Rectangle. Note that a square is a rectangle with same width and height, therefore, whenever the length is changed, width should also be changed to the same value for maintaining the square geometry and vice versa. Provide the appropriate constructors (as shown in the class diagram). All the constructors in the Square class should call the appropriate super-class (Rectangle) constructors. Use super public Square(double side) {    super(side, side);  // Call superclass Rectangle(double, double) } Override the toString()method to return "A Square with side=xxx, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass. Do you need to override the getArea()and getPerimeter()? Try them out. Override the setLength()and setWidth() to change both the width and length, so as to maintain the square geometry. The Test Class: Create 6 objects of rectangle and square (3 each) using each type of constructors in these classes using an array of Shape reference variables. Observe the order of constructor calling according to messages printed by your constructors. Display the state of each object via implicit call to toString. Use enhanced for to display the state of objects along with corresponding area and perimeter. Observation Experiments: Remove the no-argument constructor of shape class. Rerun your test class. Is there any error? Why? Remove all the constructors from each class, except the two-argument constructor from the shape class and no-argument constructors from the Rectangle and Square classes. Is there any error? Why? Write your observations as comments at the end of your test class of your program.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Attaching the picture of class diagram

Write a superclass called Shape(as shown in the class diagram), which contains:

  • Two instance variables color(String) and filled (boolean).
  • Two constructors: a no-arg (no-argument) constructor that initializes the colorto "green" and filled to true, and a two-argument constructor that initializes the color and filled to the parameters values.
  • Getter and setter for all the instance variables. By convention, the getter for a booleanvariable filled is called isFilled() (instead of getFilled() for all the other types).
  • A toString()method that returns "A << filled/Not filled >> Shape with << green >>".
  • Methods getArea() andgetPerimeter() both reurn 0. // We can not write the implementation of getArea() and getPerimeter() until we know the specific shape.

 

There are two subclasses of Shapecalled Circle and Rectangle, as shown in the class diagram. Ignore the Circle class.

The Rectangle class contains:

  • Two instance variables width(double) and length (double).
  • Three constructors as shown. The no-arg constructor initializes the widthand length to 0. All the constructors in Rectangle class should call the appropriate super-class (shape) constructors.
  • Getter and setter for all the instance variables.
  • Override methods getArea() -> length*widthand getPerimeter() -> 2*length + 2*width.
  • Override the toString()method inherited, to return "A Rectangle with width=<< xxx >> and length= << zzz >>, which is a subclass of << yyy >>", where yyy is the output of the toString() method from the superclass. 

Write a class called Square, as a subclass of Rectangle. Square has no instance variable, but inherits the instance variables width and length from its superclass Rectangle.

Note that a square is a rectangle with same width and height, therefore, whenever the length is changed, width should also be changed to the same value for maintaining the square geometry and vice versa.

  • Provide the appropriate constructors (as shown in the class diagram). All the constructors in the Square class should call the appropriate super-class (Rectangle) constructors. Use super

public Square(double side) {

   super(side, side);  // Call superclass Rectangle(double, double)

}

  • Override the toString()method to return "A Square with side=xxx, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.
  • Do you need to override the getArea()and getPerimeter()? Try them out.
  • Override the setLength()and setWidth() to change both the width and length, so as to maintain the square geometry.

The Test Class:

  1. Create 6 objects of rectangle and square (3 each) using each type of constructors in these classes using an array of Shape reference variables.
  2. Observe the order of constructor calling according to messages printed by your constructors.
  3. Display the state of each object via implicit call to toString. Use enhanced for to display the state of objects along with corresponding area and perimeter.

Observation Experiments:

  1. Remove the no-argument constructor of shape class. Rerun your test class. Is there any error? Why?
  2. Remove all the constructors from each class, except the two-argument constructor from the shape class and no-argument constructors from the Rectangle and Square classes. Is there any error? Why?

Write your observations as comments at the end of your test class of your program.

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
-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
+Rectangle()
+Rectangle(width:double,
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:double)
+Square(side:double,
color:String, filied:boolean)
+getSide():double
+setSide(side:double):void
+setwidth (side: double):void
+setlength(side:double):void
+tostring ():String
Transcribed Image Text: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 -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 +Rectangle() +Rectangle(width:double, 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:double) +Square(side:double, color:String, filied:boolean) +getSide():double +setSide(side:double):void +setwidth (side: double):void +setlength(side:double):void +tostring ():String
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Developing computer interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education