Graphics and Inheritance
-
Create a useful inheritance hierarchy.
-
Use the hierarchy to create a program that draws different graphics primitives.
-
Gain further understanding of inheritance and polymorphism.
-
Copy
P2
and rename itP3
. Here is a tutorial on how to do this inEclipse
. -
Download the following files into your resources folder:
The second part of the assignment requires you to modify the inheritance hierarchy
and some of the code in the DrawProgram
class.
Note
|
this mostly consists of getting rid of code and attributes in the primitive classes, and moving them to one of the new classes that we are going to introduce. |
The UML
diagram below shows the changes you must make and the JavaDoc describes the end result.
Here are the steps for completing the second part of the assignment:
-
Make the
Primitive
classabstract
, -
Add an abstract
draw
method to thePrimitive
class that takesUserInterface
as a parameter. -
Move the
color
andisFilled
attributes and setters to thePrimitive
class, and remove them from all the other classes. Your program should still work. -
Add a
RoundPrimitive
class that extendsPrimitive
, with the appropriate attributes and a genericdraw
method forCircle
andOval
. -
Add a
PolygonPrimitive
class that extendsPrimitive
, with the appropriate attributes and a genericdraw
method forSquare
,Rectangle
, andTriangle
. -
Remove everything but the
constructor
fromOval
,Circle
,Square
,Rectangle
, andTriangle
. You may have to change the constructors in each class to conform to the parent constructor. -
Add a Polygon primitive that implements a generic Polygon, only a constructor is needed.
-
In the
load
method for theDrawProgram
class add a case forPolygons
-
the format is for a polygon is:
"POLYGON,color,filled,numberPoints,x0,y0,x1,y1,x2,y2,…"
-
-
Delete the code for the
draw
method in theDrawProgram
class. -
Re-implement that
draw
method so that it iterates over theArrayList
of primitives and calls draw.-
The resulting code should be a loop with one statement and no instanceOf calls.
-
Consider using a foreach loop.
-
The image below shows the output from the complexPrimitives.txt file:
Your program must meet the following specifications:
-
The code must produce the correct upload file and assertion messages.
-
Work on your own, as always.
-
You must package all source files into P3.jar for submission.
-
Assignments should be implemented using Java 1.8.
-
Make sure your code runs on machines in the COMCS 120 lab.
-
Submit your program to the Checkin tab as you were shown in the recitation.
-
Read the syllabus for the late policy.
-
We will be checking programs for plagiarism, so please don’t copy from anyone else.
-
100 points for perfect submission
-
0 points for no submission, if the program does not compile, submitted class files, etc
-
Preliminary Tests:
-
testCompile: verifies that all classes exist and compile. (0 points)
-
testSimple: verifies the drawing of
simplePrimitives.txt
. (20 points) -
testComplex: verifies the drawing of
complexPrimitives.txt
. (20 points) -
testPrimitive: verifies that
Primitive
class has the correct attributes and methods. (15 points) -
testRound: verifies that
RoundPrimitive
class has the correct attributes and methods. (10 points) -
testPolygon: verifies that
PolygonPrimitive
class has the correct attributes and methods. (10 points) -
testText: verifies that
Text
class has the correct attributes and methods. (5 points) -
testOther: verifies that all other primitive classes have only the constructor. (10 points)
-
testInstanceOf: verifies that
DrawProgram
does not use theinstanceOf
operator. (10 points)
-
-
Final Tests are identical to the preliminary tests.
Important
|
Submit P3.jar to Checkin |