Homework 1 - Inheritance and polymorphism
Reading: Java by Dissection, Chapter 7.
Due Monday January 14th, at 10pm.
Program Description - A Simple Drawing Package
For this assignment you are to implement a collection of classes that
support a very simplified graphics system.
Your system will be capable of drawing various shapes (rectangles,
lines, and polygons). The shapes will be drawn on a particular "device"
within a "frame". To test your classes, you will read specifications for
the various shapes from System.in (e.g. using a java.util.Scanner)
and then create the appropriate frame or frames.
The classes you create must include:
- GraphicsTest - the class containing main() and the test code.
- Rectangle - for representing rectangles.
- Line - for representing straight lines.
- Polygon - for representing arbitrary polygons.
- Frame - for representing a frame. The only public methods your Frame class
is allowed to implement are shown in this
Frame class template.
You are of course welcome to add any private methods you want, but
only the methods shown in the template should be visible outside of the class.
Specifically the Frame class should have no knowledge of Frame or Shape ids.
(Note: My solution does not require any additional private methods.)
The following are provided as part of this exercise. These classes are NOT TO BE
MODIFIED in any way for this assignment.
Your classes (especially Frame) must provide the methods
shown in the example Main below. Your classes must work with the
sample Main below.
- Shape - an interface implemented by all shapes.
- TGraphics - the interface implemented by a device you draw on.
- ConsoleGraphics - an implementation of TGraphics for System.out. Your Frame class is the ONLY class that is allowed to mention this class by name.
- Main - a simple test of how a the Frame and shape classes are to be used.
Input
Input will be a series of commands taken from the following list. The
shapes are all given an integer id, to allow the shape to be modified
or deleted.
- F id width height - where id, width, and height are integers. Create a
frame with the specified dimensions and the specified id. Make this
the current frame. The first command input must be an F command.
Ignore this command and print a warning if an attempt is made to create two frames with the same id.
- R id left top width height - where id, left, top, width, and height
are integers. Draw a rectangle with the specified values on the
current frame.
If the current frame already has a shape with this id, replace that shape with this one.
- L id x1 y1 x2 y2 - where id, x1, y1, x1, and y2, are integers. Draw
a line from (x1, y1) to (x2, y2) on the current frame.
If the current frame already has a shape with this id, replace that shape with this one.
- P id n x1 y1 x2 y2 ... xn yn - where id, n, xi, and yi are integers. n
is the number of points in a polygon. The (xi, yi) are the points of
the polygon to be drawn on the current frame.
Draw lines from (x1, y1) to (x2, y2), from (x2, y2) to
(x3, y3) etc, ending with a line from (xn, yn) back to (x1, y1).
If the current frame already has a shape with this id, replace that shape with this one.
- D - display the current frame.
- S id - make frame id, the current frame and display it. If there
is no such frame, print an error message and leave the current frame
unchanged.
- C - clear the current frame, removing all displayed shapes.
- X id - delete shape id from the current frame. If there is not
such shape, print a warning and continue.
All input values will be white space delimited. The main program is
intended to be an interactive test program for your other classes, so
some type of simple prompt would be appropriate.
In addition your program must support a command line arugment of -q
for quiet mode. In quite mode, no prompt is printed. This is intended
to be used for testing where input comes from a file (via file
redirection) and output also goes to a file (via file redirection).
Be sure and test your program using file redirection. A common mistake is
to create multiple Scanner objects which will fail when using file redirection.
Output
Output is a series of frame displays, possibly with some intermixed
error messages. There should be one frame display for each D and S
command in the input.
Sample Input
F 1111 60 25
R 100 2 2 50 20
P 300 3 1 1 30 10 15 20
D
F 2222 70 40
R 100 3 2 50 20
L 200 12 2 50 20
D
S 1111
X 100
D
Q
Sample Output
The following shows the input and output mixed as would be the case in
interactive mode. If input was from a file using file redirection, the
input commands would of course not be shown in the output.
os-prompt: java GraphicsTest
enter command>F 1111 60 25
enter command>R 100 2 2 50 20
enter command>P 300 3 1 1 30 10 15 20
enter command>D
**************************************************************
* *
* ** *
* *************************************************** *
* * **** * *
* ** *** * *
* * * *** * *
* * * *** * *
* * * *** * *
* * * **** * *
* * * *** * *
* * * ** * *
* * * ** * *
* * * * * *
* * * ** * *
* * * * * *
* * * ** * *
* * * * * *
* * * ** * *
* * * * * *
* * * ** * *
* * * * *
* * * *
* *************************************************** *
* *
* *
**************************************************************
enter command>F 2222 70 40
enter command>R 100 3 2 50 20
enter command>L 200 12 2 50 20
enter command>D
************************************************************************
* *
* *
* *************************************************** *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * *** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * ** * *
* * * *
* *************************************************** *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
************************************************************************
enter command>S 1111
**************************************************************
* *
* ** *
* *************************************************** *
* * **** * *
* ** *** * *
* * * *** * *
* * * *** * *
* * * *** * *
* * * **** * *
* * * *** * *
* * * ** * *
* * * ** * *
* * * * * *
* * * ** * *
* * * * * *
* * * ** * *
* * * * * *
* * * ** * *
* * * * * *
* * * ** * *
* * * * *
* * * *
* *************************************************** *
* *
* *
**************************************************************
enter command>X 100
enter command>D
**************************************************************
* *
* ** *
* **** *
* * **** *
* * *** *
* * *** *
* * *** *
* * *** *
* * **** *
* * *** *
* * ** *
* * ** *
* * * *
* * ** *
* * * *
* * ** *
* * * *
* * ** *
* * * *
* * ** *
* * *
* *
* *
* *
* *
**************************************************************
enter command>Q
Sample Input2
The commands do not need to be one per line.
F 1111 60 25 R 100 2 2 50 20 P 300 3 1 1 30 10 15 20 D
F 2222 70 40 R 100 3 2 50 20 L 200 12 2 50 20 D S 1111 X 100 D
Q
Sample Ouput2
The output is the same as for the first input except that the
intermixing of commands between frames would be different, or with the -q flag and file redirection
there would be no command output, just the frames.
Getting Started
I strongly recommend that you first create the Frame, Line, and Rectangle classes that are required for
the test program, Main.java, to work. This demonstrates most of the key inheritance ideas in this assignment.
When that is working, then create your GraphicsTest program. It is important to not mix up your "application"
(GraphicsTest) which requires integer ids to be associated with frames and shapes, and your "graphics package"
which knows nothing about ids.
You can recieve 70% on this assignment if you complete the Frame, Rectangle, Line, and Polygon classes.
For the GraphicsTest portion for of the assignment, I'm providing one additional file,
FrameMap.java that I used to simply the association of shape ids with frames.
Please note that two different frames can contain different shapes with the same id which is why I
create a separate table (I used a HashMap) of shapes ids for each frame.
You are welcome to use this class but it is not required.
Design Points
25 points - properly structure your files Frame, Line, Polygon, and
Rectangle using ConsoleGraphics and TGraphics as appropriate.
Correctness Points
20 points - your files work correctly with the test Main.java.
3 points for each of the commands (F, R, L, P, D, S, C, X)
3 points quite mode
3 points miscellaneous
This page maintained by Charlie McDowell.
Email regarding this site.