Wed Mar 06 2:30-4:00 PM
This lab uses inheritance to implement a simple hierarchy of shapes that are drawn to a window. In this lab, you will:
super
keyword to invoke a parent class constructor
super
keyword to invoke a parent class method
This is intended to be a relatively short lab; you should submit your work sometime today, and preferable before the end of the lab.
Implementing a hierarchy of drawable shapes is a common example of inheritance in object oriented programming. Consider the shape hierarchy shown in the UML diagram below. Each shape has:
toString
method that returns a string containing
information about the position and color
draw
method that subclasses must implement;
the method is abstract because Shape
does not contain enough
information to draw actual shapes such as circles and rectangles
Subclasses of Shape
are allowed to add their own new attributes and new
methods; subclasses may also override the public (and protected) methods of
Shape
to customize their behavior.
We see that Circle
adds its own attribute that defines the radius of
the circle (so that we can draw smaller and larger circles). It also adds its own
methods to get and set the radius. Circle
overrides the toString
method so that it can return string that also contains information about the radius.
Circle
implements the draw
method because it has enough
information to actually draw a circle using the position, radius, and color of the circle.
Rectangle
adds its own attributes to define the width and height of
the rectangle. It provides its own methods to get and set the width and height.
Rectangle
overrides the toString
method so that it
can return a string that also contains information about the width and height.
Rectangle
implements the draw
method because it has enough
information to actually draw a rectangle using the position, width, height, and color of the rectangle.
Implement the Shape
hierarchy. The APIs for the various classes can be found here:
A jar file containing Point2D
is here:
Point2D
(not needed if you are compiling from the command line)
StdDraw
(not needed if you are compiling from the command line)
Starter code for each class and a small tester is listed here:
TestShape
should produce the following output:
submit 1030Z L7W Circle.java Rectangle.java