Instructions: Abstract Superclass Shape and Its Concrete Subclasses Implement the superclass Shape and its subclasses Circle, Rectangle and Square, as shown in the class diagram. Implement Unit Tests for the all classes except the abstract Shape class.   In this exercise, Shape shall 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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Instructions: Abstract Superclass Shape and Its Concrete Subclasses

Implement the superclass Shape and its subclasses Circle, Rectangle and Square, as shown in the class diagram.

Implement Unit Tests for the all classes except the abstract Shape class.

 

In this exercise, Shape shall 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 toString().
  • Two abstract methods getArea() and getPerimeter() (shown in italics in the class diagram).

The subclasses Circle and Rectangle shall override the abstract methods getArea() and getPerimeter() and provide the proper implementation. They also override the toString().

Run a test class to test these statements involving polymorphism and explain the outputs with comments inside the file "ShapeDriver.java". Some statements may trigger compilation errors. Explain the errors, if any.

Shape s1 = new Circle(5.5, "RED", false); // Upcast Circle to Shape System.out.println(s1); // which version?

System.out.println(s1.getArea()); // which version? System.out.println(s1.getPerimeter()); // which version? System.out.println(s1.getColor()); System.out.println(s1.isFilled()); System.out.println(s1.getRadius());

Circle c1 = (Circle)s1; // Downcast back to Circle

System.out.println(c1);

System.out.println(c1.getArea());

System.out.println(c1.getPerimeter());

System.out.println(c1.getColor());

System.out.println(c1.isFilled());

System.out.println(c1.getRadius());

Shape s2 = new Shape();

Shape s3 = new Rectangle(1.0, 2.0, "RED", false); // Upcast System.out.println(s3);

System.out.println(s3.getArea());

System.out.println(s3.getPerimeter());

System.out.println(s3.getColor());

System.out.println(s3.getLength());

Rectangle r1 = (Rectangle)s3; // downcast System.out.println(r1); System.out.println(r1.getArea());

System.out.println(r1.getColor());

System.out.println(r1.getLength());

Shape s4 = new Square(6.6); // Upcast

System.out.println(s4);

System.out.println(s4.getArea());

System.out.println(s4.getColor());

System.out.println(s4.getSide());

// Take note that we downcast Shape s4 to Rectangle,

// which is a superclass of Square, instead of Square

Rectangle r2 = (Rectangle)s4;

System.out.println(r2);

System.out.println(r2.getArea());

System.out.println(r2.getColor());

System.out.println(r2.getSide());

System.out.println(r2.getLength());

// Downcast Rectangle r2 to Square

Square sq1 = (Square)r2;

System.out.println(sq1);

System.out.println(sq1.getArea());

System.out.println(sq1.getColor());

System.out.println(sq1.getSide());

System.out.println(sq1.getLength());

<<abstract>> Shape
#color:String
#filled:boolean
+Shape ()
+Shape (color:String, filled:boolean)
+getColor ():String
+setColor (color:String):void
+isFilled ():boolean
+setFilled (filled:boolean):void
+getArea():double
+getPerimeter:double
+tostring ():String
Circle
Rectangle
#width:double
#length:double
#radius:double
+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
Transcribed Image Text:<<abstract>> Shape #color:String #filled:boolean +Shape () +Shape (color:String, filled:boolean) +getColor ():String +setColor (color:String):void +isFilled ():boolean +setFilled (filled:boolean):void +getArea():double +getPerimeter:double +tostring ():String Circle Rectangle #width:double #length:double #radius:double +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
Circle
Rectangle
#radius:double
#width:double
#length:double
+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,
filled:boolean)
+getSide():double
+setSide(side:double):void
+setwidth (side: double):void
+setlength(side:double):void
+toString ():String
Transcribed Image Text:Circle Rectangle #radius:double #width:double #length:double +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, filled: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
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY