Thu 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 Square
adds its own attribute that defines the length of
the sides of the square (so that we can draw smaller and larger squares). It also adds its own
methods to get and set the length. Square
overrides the toString
method so that it can return string that also contains information about the length.
Square
implements the draw
method because it has enough
information to actually draw a square using the position, length, and color of the square.
Ellipse
adds its own attributes to define the width and height of
the ellipse. It provides its own methods to get and set the width and height.
Ellipse
overrides the toString
method so that it
can return a string that also contains information about the width and height.
Ellipse
implements the draw
method because it has enough
information to actually draw a ellipse using the position, width, height, and color of the ellipse.
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:
TestShape2
should produce the following output:
submit 1030Z L7R Square.java Ellipse.java