Homework 3 - Inheritance and polymorphism
[Homepage] |
[General Lab Info] |
[TA's & Tutors] |
[FAQ's] |
[Homework] |
[Excellent Programs] |
[Exams]
Reading: Chapter 7.
Due At 8:00am, February 4, 2002, submit is turned off
automatically. Work turned in after that time will not be accepted
for grading.
Program Description - A Simple Drawing Package
For this assignment your 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 Console.in.readXXX()),
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. Your Frame class
should implement only the methods shown in this
Frame class template. If you have completed the
assignment and find this a hardship to follow, please contact me as soon as possible. You are of course welcome to and any private methods you want, but
only the methods shown in the template should be visible outside of the class.
(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.
NEW ADDITION: 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 Frame is expected 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
this current frame. The first command input must be an F command.
- 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.
- 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.
- 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).
- 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 inputs 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).
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
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.
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
**************************************************************
* *
* ** *
* **** *
* * **** *
* * *** *
* * *** *
* * *** *
* * *** *
* * **** *
* * *** *
* * ** *
* * ** *
* * * *
* * ** *
* * * *
* * ** *
* * * *
* * ** *
* * * *
* * ** *
* * *
* *
* *
* *
* *
**************************************************************
Sample Input2
The commands do not need to be one per line.
F 1111 60 25 R 100 3 2 10 20 L 200 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
Sample Ouput2
The output is the same as for the first input except that the
intermixing of commands between frames would be different.
[Homepage] |
[General Lab Info] |
[TA's & Tutors] |
[FAQ's] |
[Homework] |
[Excellent Programs] |
[Exams]
This page maintained by Charlie McDowell.
Email regarding this site.