This assignment must be completed by 1:45 PM on 29 October, and uploaded to the Homework 9 moodle page. You should upload only your ColorfulRectangle.java and TesterHW9.java files.
Why?
In this assignment you will create a new class using inheritence. This is another small example that you should be able do complete within a couple of hours.
Getting starting
Start by creating a Java application where the main class is called
TesterHW9
and resides in the
edu.unca.cs.csci202
package.
Next download a copy of
the RotatedRectangle
class and add it to your project.
This RotatedRectangle
is very close to being a solution for
Homework 7 but has been modified
to use double
s rather than int
s to store
x, y, width, and height values for the rectangle.
This was done to make it a little simpler to interface the
RotatedRectangle
with Processing.
This class also contains the iterator
method
that was written in lab on 10 October.
Task One
Define a new class ColorfulRectangle
that extends Rotating Rectangle
by introducing operations for storing a color for the rectangle.
I realize that it might be more natural to use aggregation rather than
inheritance, but we do need an inheritance exercise during the semester.
Represent the color of the rectangle with a single 32-bit integer. This isn’t near as sophisticated as Java’s Color class, but it’s similar to the way color is represented in both the Processing programming language and graphic accelerator cards.
To effectively do this, you need to add a new field for the stored
color and implement appropriate accessor and mutator methods
and a new toString
method.
public int getColor() ; public void setColor(int) ; public String toString() ;
You also need some constructors. We’re going to have six in all.
Add four constructors similar to the those developed for
the RotatedRectangle
in
Homework 6.
None of these four will require more than two Java
statements in the body. Be sure to initialize the color
in all of these constructors.
Add a constructor that can be used to initialize all six internal fields in the following order, x, y, width, height, rotation and color. And finally, implement a copy constructor.
Task Two
Add a few lines of code in the main
method
of DriverHW9
to construct objects with
all six of your constructors and to test your
getColor
and setColor
methods.
Use your toString
method to print the contents
of your ColorfulRectangle
objects.