Write a superclass called Shape as shown in the class diagra

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 color to \"green\" and filled to true, and a constructor that initializes the color and filled to the given values.

Getter and setter for all the instance variables. By convention, the getter for a boolean variable xxx is called isXXX() (instead of getXxx() for all the other types).

A toString() method that returns \"A Shape with color of xxx and filled/Not filled\".

Write a test program to test all the methods defined in Shape.

Write two subclasses of Shape called Circle and Rectangle, as shown in the class diagram.

The Circle class contains:

An instance variable radius (double).

Three constructors as shown. The no-arg constructor initializes the radius to 1.0.

Getter and setter for the instance variable radius.

Methods getArea() and getPerimeter().

Override the toString() method inherited, to return \"A Circle with radius=xxx, which is a subclass of yyy\", where yyy is the output of the toString() method from the superclass.

The Rectangle class contains:

Two instance variables width (double) and length (double).

Three constructors as shown. The no-arg constructor initializes the width and length to 1.0.

Getter and setter for all the instance variables.

Methods getArea() and getPerimeter().

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. Convince yourself that Square can be modeled as a subclass of Rectangle. Square has no instance variable, but inherits the instance variables width and length from its superclass Rectangle.

Provide the appropriate constructors (as shown in the class diagram). Hint:

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.

Shape -color : String \"red\" filled:boolean true +Shape() +Shape (color:String, filled: boolean) +getColor) String +setColor (color:String):void tisFilled():boolean +setFilled(filled:boolean):void +toString ):String Circle Rectangle -radius:double1.0 +Circle() +Circle(radius: double) +Circle(radius: double, -width:double 1.0 -length: double = 1.0 +Rectangle() +Rectangle(width: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 SqueO +Square(side:double) Square(side:double, color:String,filled:boolean) getSide):double +setSide(side:double) void +sethidth (side: double):void +setLength(side :double):void +toString ):String

Solution

/* * The Circle class models a circle with a radius and color. */ public class Circle { // Save as \"Circle.java\" // private instance variable, not accessible from outside this class private double radius; private String color; // The default constructor with no argument. // It sets the radius and color to their default value. public Circle() { radius = 1.0; color = \"red\"; } // 2nd constructor with given radius, but color default public Circle(double r) { radius = r; color = \"red\"; } // A public method for retrieving the radius public double getRadius() { return radius; } // A public method for computing the area of circle public double getArea() { return radius*radius*Math.PI; } }
Write a superclass called Shape (as shown in the class diagram), which contains: Two instance variables color (String) and filled (boolean). Two constructors: a
Write a superclass called Shape (as shown in the class diagram), which contains: Two instance variables color (String) and filled (boolean). Two constructors: a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site