announcements‎ > ‎

2015-11-10 HW15

posted Nov 10, 2015, 9:25 AM by Samuel Konstantinovich   [ updated Nov 10, 2015, 11:14 AM ]

While you write this code think about the following questions:

1. The semi-perimeter method is concrete in your abstract class. How can this not be abstract? Hint: Think about your Actor's toString.

2. The class will be required to call super-class methods that have been overridden. This is totally OK! Just use super.method(___)


Write the 4 classes below: (be sure to read all of the parts a-f)

Shape, Circle, Rectangle, Square

While you do that write a Driver.java class that demonstrates how the methods work.


abstract class Shape.

a) It has the following variables:

    private String color   //will store "red" "blue" "green" "white" etc.

    private boolean filled

b) It has the following public methods:

    Shape() //default to "black" and not filled (false)

    Shape(String,boolean)

    String getColor()

    void setColor(String)

    boolean isFilled()

    void setFilled(boolean)

    double getSemiPerimeter()

    String toString() 

      //returns: "A Shape with color of xxx and is yyy" 

      //where yyy is “filled” or “not filled”

c) It has the followin ABSTRACT METHODS:

    double getArea()

    double getPerimeter()


There are two sub classes of Shape: Circle and Rectangle

d) Circle

    double radius

    set/get methods for radius

    Circle(double)

    Circle() //sets the radius to 1.0

    toString()

        //returns: "A Circle with radius=xxx, which is a subclass of yyy", 

        //where yyy is the output of the toString() method from the superclass.


e) Rectangle

    double width

    double length

    set and get methods for width/length

    Rectangle() //sets the width/length to 1.0

    Rectangle(double,double) //width and length

    toString() 

       //returns: "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.


Rectangle has one subclass Square:

f) Square

    Square() //same as superclass constructor

    Square(double)

    Override the setLength() and setWidth() to change both the width and length, so as to maintain the square geometry.

    toString()

       //returns: "A Square with side=xxx, which is a subclass of yyy"

       //where yyy is the output of the toString() method from the superclass.


Comments