On This Page
Overview
During this assignment, you will:
- Create a class hierarchy for drawing shapes
- Create a driver for drawing the shapes
- Automatically create colorful "artwork"
^ top
Operation
When run, the program automatically displays a JFrame window with colorful "artwork" like the following;

Console Output
The console displays the coordinates of each object it draws in a format like the following:
C:>java ArtOMatic
MyRect: x1 = 94, y1 = 355, width = 26, height = 172
MyOval: x1 = 155, y1 = 434, width = 277, height = 186
MyLine: x1 = 338, y1 = 178, x2 = 268, y2 = 0
MyRect: x1 = 92, y1 = 373, width = 281, height = 373
MyOval: x1 = 385, y1 = 242, width = 22, height = 131
MyRect: x1 = 293, y1 = 0, width = 171, height = 72
MyLine: x1 = 97, y1 = 20, x2 = 49, y2 = 21
MyOval: x1 = 257, y1 = 93, width = 86, height = 120
MyOval: x1 = 425, y1 = 368, width = 408, height = 81
MyLine: x1 = 19, y1 = 340, x2 = 108, y2 = 235
MyOval: x1 = 33, y1 = 335, width = 86, height = 268
MyLine: x1 = 436, y1 = 124, x2 = 27, y2 = 372
MyOval: x1 = 267, y1 = 365, width = 217, height = 265
MyRect: x1 = 371, y1 = 82, width = 286, height = 29
MyRect: x1 = 441, y1 = 138, width = 3, height = 188
^ top
Specifications
This assignment has several parts:
- Start with the starter code available here: ArtOMatic.java.
- Create a class hierarchy for a set of shapes as shown in the following diagram.

- Class
MyShape must be declared abstract.
- All the data used in the entire hierarchy must be declared in class
MyShape.
- Class
MyShape must contain the following instance variables:
private int x1, x2, y1, y2;
private Color color = Color.black;
Note that all data in all classes must be declared private. You are not allowed to declare any other variables in MyShape or any other part of the hierarchy. You will not need to use the Color variable unless you decide to do the extra credit.
- Code at least two constructors in the
MyShape class:
- A constructor with no arguments that sets the coordinates to 0.
public MyShape()
- A constructor with arguments that sets the coordinates to the supplied values.
public MyShape(int newX1, int newY1, int newX2, int newY2)
- Also, code
set and get methods for each of the instance variables of the MyShape class.
- In addition,
MyShape must declare the following two abstract methods that are overridden in each subclass:
public abstract void draw(Graphics g)
public abstract String toString()
- The subclasses of
MyShape must use the drawing methods of the Graphics object to draw their shape. The Graphics methods are called in each shape's draw() method.
- Also, the subclasses of
MyShape must implement a toString() method that returns information about the type of object and the coordinates or size as appropriate. Do not display any information to the console in the toString() method.
- All subclasses of
MyShape must provide two constructors which mimic those provided by class MyShape.
- There can be no
MyLine, MyOval, or MyRect variables anywhere in the program - only MyShape variables that contain references to MyLine, MyOval, and MyRect objects.
- Create a driver class named
ArtOMatic that extends JPanel as shown in the starter code.
- The
ArtOMatic class must use an array of MyShape variables to contain all the shapes. Within the constructor of ArtOMatic, use random numbers to pick the shape type and the coordinates of each shape and then assign the objects to the array. Call each shapes toString() method just after you put the shape into the array.
ArtOMatic must override the paintComponent() method of JPanel. Within this method, use a loop to call the draw() method of every MyShape object in the array, passing each object's method the Graphics object.
- The call to each shapes
toString() method must produce an output like that shown in the Console Output section above.
- Your program must display a
JFrame window with colorful "artwork" like that shown in the Operation section above, displaying at least 15 shapes.
- Do not use package statements at this time.
^ top
Suggestions for Getting Started
There are a lot of classes in this assignment. I suggest that you start by declaring class MyShape, class MyLine, and the ArtOMatic application to test your classes. The application should have a MyShape instance variable that can refer to one MyLine object that is created in the ArtOMatic constructor.
Next, override the paintComponent() method and call the draw() method of the shape object, passing it the Graphics object. Inside the draw() method of the MyLine class, add the code to draw the shape on the screen.
When this is working, change the single MyShape reference into an array of MyShape references, and hard code several MyLine objects into the program. Also, change the ArtOMatic paintComponent() method to iterate through each element of the array.
Once you can display several lines, declare the MyOval and MyRect classes and add objects of these classes into the existing array. This should give you an idea of how easy it is to add additional classes to a polymorphic system. Make sure each object's type is randomly created and randomly given coordinates.
^ top
Test Cases
Testing is an important part of software development. Usually, programmers develop tests for each unit of code they produce. This is known as "unit testing" and allows the programmer to verify that the code they develop works correctly.
To assist you in developing your code, I provide tests for this assignment. These tests, known collectively as "test cases", help to make sure that your assignment meets its requirements. I will use these test cases (and perhaps others) to test your assignment, and you should too. If the test cases do not pass, then you will lose points for the assignment.
Install the tests by copying the following file into the same directory as your .java source code files:
Run the tests as you would any other Java program. You may need to compile your source code before compiling and running the test code.
^ top
Extra Credit
The following are worth extra credit points:
- Add randomly chosen colors to each shape. (1 point)
- Add another subclass to
MyShape. (1 point)
- Add still another shape to the hierarchy that draws using a
Graphics2D object. (1 point)
- Submit your source code in a JAR file. (1 point)
Make certain that your README.txt file lists any extra credit attempted.
^ top
Grading Criteria
The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.
Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.
For information on grading policies, including interpretation of scores, see the course information page.
Program Compilation
- 4: Source code compiles with no errors or warnings
- 0: Does not compile
Functionality
- 10: Demonstrates mastery of the assignment
- Has extra features or demonstrates techniques beyond the assignment
- Applies concepts from the lessons appropriately
- Meets all specifications (see above) with particularly elegant solutions
- Runs to completion with no abnormal error conditions
- Generates correct output when run
- 8: Has all the functionality expected of the assignment
- Demonstrates many techniques from the lesson
- Meets all specifications (see above)
- Implementation seems more complicated than necessary.
- May have one minor error
- 6: Has most of the functionality expected of the assignment
- Demonstrates some techniques from the lesson
- Meets all but one of the specifications (see above)
- Implementation seems excessively complicated.
- May have 2-3 minor errors
- 4: Has some of the functionality expected of the assignment
- Demonstrates some techniques from the lesson
- Meets at least 1/2 of the specifications (see above)
- Implementation seems excessively complicated.
- May have more than 3 minor errors
- 2: Serious functional problems but shows some effort and understanding
- Meets less than 1/2 of the of the specifications (see above)
- Has a major error or many minor errors
- Implementation seems very convoluted
- Demonstrates few techniques from the lesson
- 0: Does not execute
Program Style
- 4: Code is well-documented
- 3: Code has minor documentation errors
- Has 1 documentation error
- 2: Code has some documentation errors
- Has 2-3 documentation errors
- 1: Code has many documentation errors
- Has more than 3 documentation errors
- 0: No apparent attempt to document code
README.txt File
- 2: README.txt file submitted with specified information included
- 1: README.txt submitted but some information was not included
- 0: No README.txt submitted
Maximum Score: 20, plus extra credit
^ top
What to Turn In
Submit your assignment following the instructions for homework. Include the following items for grading:
README.txt file
- All of your source code (i.e.
.java files)
- Any other source code needed to make your program function
You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files unless explicitly stated by the instructor. Your assignment must work as submitted.
^ top
Home
| WebCT
| Announcements
| Schedule
| Room Policies
| Course Info
Help
| FAQ's
| HowTo's
| Links
Last Updated: April 09 2005 @21:02:54
|