CMSC-341 Fall 2004
Project 1
Assigned |
13 Sep 2004 |
Due |
26 Sep 2004 at 11:59PM |
Background
Abstract Data Types (ADTs) are the central focus of this course and of
object-oriented programming in general. Recall that an ADT has two parts:
(1) a description of the elements that make up the type, and (2) a
description of the operations allowed on instances of the type. The ADT
idea is implemented in C++ as a class.
The ADT is one of the most powerful and important ideas in computer
science. This project will give you some exercise in using ADTs. Another
important OOP idea, parametric polymorphism, is implemented in C++
by templates. This project will give you some practice with C++ templates.
Description
In this project you will implement a simple command-driven, text-based
drawing program. Programs of this type, like Microsoft's PowerPoint
are (sometimes literally) textbook examples of the use of
polymorphism.
You will implement five classes - Point, Circle,
Rectangle, DisplayObject, and Raster. Objects of
type Point specify a location in 2D space by X and Y coordinates.
Objects of type Circle specify a circle via its center (a Point) and
radius. Objects of type Rectangle specify a rectangle by its
upper-left and lower-right corners (both Points).
Objects of type Circle and Rectangle contain information needed to
display that particular shape. Regardless of whether the shape to be
displayed is a circle or rectangle, it has other properties, namely, a
color and a name (unique identifier). The DisplayObject class is a
wrapper class. It contains a data member which is either a Circle or
a Rectangle (the actual type is specified with a template parameter)
and data members for the shape's color and name. This design is
preferred over replicating the color and name slots in the Circle and
Rectangle classes.
Objects of type Raster are simply 2D buffers in which
DisplayObjects are drawn for subsequent display.
The ADTs that you will implement are described fully in the "ADT" section below. Use the description there to
design your classes. Please remember that you must provide good
documentation as described in the "Project Organization"
handout.
Here are your tasks:
- Read the brief introduction to the STL at the following URL:
http://www.msoe.edu/eecs/cese/resources/stl/index.htm
- Read and understand the description of the STL version of the
vector ADT at the following URL:
http://www.msoe.edu/eecs/cese/resources/stl/vector.htm
You will use vectors to store DisplayObjects so that there
are no hard-coded limits on how many there can be.
- Read and understand the description of the ANSI/ISO C++ standard
library version of the string ADT at the following URL:
http://www.msoe.edu/eecs/cese/resources/stl/string.htm
DisplayObject names are strings.
- Implement the Point class.
- Implement the Circle class.
- Implement the Rectangle class.
- Implement the DisplayObject class.
- Implement the Raster class.
- Write Proj1.cpp and any other support files needed to implement
the functionality of this project.
-
Copy the makefile from:
/afs/umbc.edu/users/o/a/oates/pub/CMSC341/Proj1/Makefile
to your own directory and modify it as needed. NOTE: Your makefile
must produce an executable named Proj1.
-
Answer the questions posed in 341-Fall04-proj1_questions.txt. Copy the
file
/afs/umbc.edu/users/o/a/oates/pub/CMSC341/Proj1/341-Fall04-p1_questions.txt
to your own directory and edit it to provide your answers to the questions.
Don't forget to submit the edited file; it's 10% of this project's
grade.
Definition of the ADT
Overview
For each class described below, all data members must be private.
Other than this requirement and any others explicitly stated in the
description of a class, you have the freedom to design the class as
you see fit. However, remember that you will be graded on the quality
of that design. Be sure to note that it is generally a good idea to
write an explicit destructor, copy constructor, and assignment
operator, even if they are never called explicitly in your code,
because they are often called implicitly.
Point
Points specify a location in 2D space by its X and Y coordinates, both
of which are integers.
Circle
Circles specify a circle by its center, which is a Point, and its
radius, which is an integer that must be greater than zero. You must
implement a public method named inside that takes a point as an
argument and returns true if that point lies inside the circle. If cx
and cy are the coordinates of the center of the circle, and px and py
are the coordinates of the point, then p lies inside the circle if
sqrt((cx - px)^2 + (cy - py)^2) <= radius.
Rectangle
Rectangles specify a rectangle by its upper-left and lower-right
corners, both of which are Points. You must implement an
inside method for Rectangles that returns true if point p (the
method's sole argument) lies inside or on the lines that define the
rectangle. Use the obvious test for this. This method can assume
that its arguments are valid, i.e., the the upper-left point is above
and to the left of the lower right point.
DisplayObject
The DisplayObject class is templated. The template parameter is used
to specify whether the object to be displayed is either a Circle or a
Rectangle. For example, the following code declares a DisplayObject
where the underlying shape to be displayed is a Circle:
In addition, each DisplayObject must have the following data members:
- char color - The "color" is simply the character that
will be used when drawing the underlying shape. That is, the area
covered by the shape in the raster will be filled with characters of
type color.
- string name - The name is a unique identifier that is
used in the command file to specify an object to be modified.
Raster
The Raster class contains a 2D array of characters that can be
displayed. DisplayObjects are drawn in this array by placing
characters of the DisplayObject's color in the appropriate locations.
The Raster class must support the following:
- You must implement a two argument constructor that takes the
height (number of rows) and width (number of columns) in the 2D
display. If these arguments are H and W, then the coordinates of the
upper-left corner of the raster are X = 0 and Y = 0, and the
coordinates of the lower-right corner of the raster are X = W - 1 and
Y = H - 1.
- You must implement a clear method that fills the raster
with the '.' character. Always call clear before processing a DRAW
command (see below).
- You must implement a setValue method that takes the X and Y
coordinates of a Point (or a Point object, you choose) and a
character, and sets the value of the raster at that point to the
character.
- You must implement an operator<< using the method shown
on page 35 of the Weiss text.
- You must implement a draw method that takes as input a
DisplayObject and draws it inside the raster. The inside
method will be useful here. Note that the draw method will
need to be templated to allow for circles and rectangles to be
drawn by the same method.
The main routine
As part of your main routine you will read commands from a command
file that create Circles and Rectangles. These will be stored in
vectors of DisplayObjects, one for Circles and one for Rectangles.
Other commands will ask you to modify properties of these
DisplayObjects, where the identify of the DisplayObject is specified
by its name. Therefore, you should write a templated function called
find_by_name that takes as arguments a vector of DisplayObjects
and a name and returns a pointer to the DisplayObject with that name,
or NULL if no such DisplayObject exists.
The Command Line
Project 1 will be invoked with a command line that consists of three
arguments - the height and width of the raster and the name of a file
that contains a set of operations that must be performed, in that
order. The format of this file is described in the command file section below.
Note that you must check command line arguments to ensure that they
are valid, e.g. that the command file can be opened and that the
height and width are greater than zero, and print an appropriate
message if an error is found.
The Command File
Commands in the file specify operations to be performed. Each line in
the file represents one command. Blank lines may appear anywhere in
the file and should be ignored. Otherwise, you can assume that any
line containing a command is syntactically well-formed. We make this
assumption so you don't have to spend lots of time making the command
file parser bullet proof.
The command file format follows:
- CIRCLE NAME X Y RADIUS COLOR - Define a Circle named NAME (a
string without any white space) with center (X, Y) (both ints) and
radius RADIUS (an int). When the circle is drawn, the area it covers
in the raster will be filled with characters of type COLOR (a
character).
- RECTANGLE NAME X1 Y1 X2 Y2 COLOR - Define a Rectangle named NAME
(a string without any white space) whose upper-left corner is (X1, Y1)
(both ints) and whose lower-right corner is (X2, Y2) (both ints).
When the rectangle is draw, the area it covers in the raster will be
filled with characters of type COLOR (a character).
- DRAW - Print the current contents of the raster to the screen. Be
sure to call the clear method first. Draw all of the
Rectangles in the order they were defined, and then draw all of the
Circles in the order they were defined.
- SET_COLOR NAME COLOR - Find the object named NAME (a string
without any white space) and change its color to COLOR (a character).
Print an error message if no object named NAME exists.
- SHIFT NAME DELTA_X DELTA_Y - Find the object named NAME (a string
without any white space) and move it by DELTA_X (an int) units in the
X direction and DELTA_Y (an int) units in the Y direction. DELTA_X
and DELTA_Y can be positive, negative, or zero. Print an error
message if no object named NAME exists.
Please note the following.
- It is possible for circles and rectangles to lie completely
within the raster, partially within the raster, or completely outside
the raster. Likewise, objects may be shifted partially or completely
outside the raster. This is OK and there is no need to issue a
warning about this if it occurs.
- All commands will be well-formed and values will be of the
appropriate type. For example, X and Y coordinates will be ints.
- You must check to ensure that the radius of a circle is greater
than zero and that the upper-left and lower-right corners of a
rectangle are valid (e.g., the lower-right corner cannot be above the
upper-left corner). Issue an error message as appropriate but keep
processing input.
- The command file can have multiple commands of all types,
including DRAW.
- No two objects with the same name will be created in the command
file, so you don't have to check for duplicates. However, you must
check to ensure that names specified in SET_COLOR and SHIFT commands
are valid.
Sample Output
The following input file:
CIRCLE amy 20 10 3 A
CIRCLE bob 40 10 4 B
CIRCLE charley 60 10 5 C
RECTANGLE donna 10 9 70 11 0
DRAW
Creates the following output (given height and width arguments of 20
and 75):
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
............................................................C..............
........................................B................CCCCCCC...........
....................A.................BBBBB.............CCCCCCCCC..........
..................AAAAA..............BBBBBBB............CCCCCCCCC..........
..........00000000AAAAA00000000000000BBBBBBB000000000000CCCCCCCCC000000....
..........0000000AAAAAAA000000000000BBBBBBBBB0000000000CCCCCCCCCCC00000....
..........00000000AAAAA00000000000000BBBBBBB000000000000CCCCCCCCC000000....
..................AAAAA..............BBBBBBB............CCCCCCCCC..........
....................A.................BBBBB.............CCCCCCCCC..........
........................................B................CCCCCCC...........
............................................................C..............
...........................................................................
...........................................................................
...........................................................................
...........................................................................
The following input file:
CIRCLE amy 20 10 3 A
CIRCLE bob 40 10 4 B
CIRCLE charley 60 10 5 C
RECTANGLE donna 10 9 70 11 0
SET_COLOR amy +
SHIFT donna 0 -10
DRAW
Creates the following output (given height and width arguments of 20
and 75):
..........0000000000000000000000000000000000000000000000000000000000000....
..........0000000000000000000000000000000000000000000000000000000000000....
...........................................................................
...........................................................................
...........................................................................
............................................................C..............
........................................B................CCCCCCC...........
....................+.................BBBBB.............CCCCCCCCC..........
..................+++++..............BBBBBBB............CCCCCCCCC..........
..................+++++..............BBBBBBB............CCCCCCCCC..........
.................+++++++............BBBBBBBBB..........CCCCCCCCCCC.........
..................+++++..............BBBBBBB............CCCCCCCCC..........
..................+++++..............BBBBBBB............CCCCCCCCC..........
....................+.................BBBBB.............CCCCCCCCC..........
........................................B................CCCCCCC...........
............................................................C..............
...........................................................................
...........................................................................
...........................................................................
...........................................................................
Files To Be Submitted
Submit all files required to build an executable named Proj1 by
running make.
Submit the files using the procedure given to you for your section of
the course.
If your makefile is set up correctly, you should be able to execute
the command make submit.
Submit Tools
There are a number of tools available to you to check on your submittal.
It is your responsibility to ensure that the submittal is correct and will
result in a successful
compilation of your project. Do not wait till the last minute to submit
your files. Give yourself enough time to check that your submittal is correct.
If you don't submit a project correctly, you will not get credit
for it. Why throw away all that hard work you did to write the project?
Check your
submittals. Make sure they work. Do this before the due date.
Documentation for the submit program is on the web at http://www.gl.umbc.edu/submit/.
One of the tools provided by the submit program is
submitls. It lists the names of the files you have submitted.
Additionally, there are two programs for use only by CMSC-341 students
(not part of the UCS submit program). They are in the directory
/afs/umbc.edu/users/o/a/oates/pub/CMSC341/ and are
named submitmake and submitrun. You can
use these programs to
make or run your submitted projects.
The syntax is similar to that for submit:
submitmake <class> <project>
Example: submitmake cs341 Proj1
This makes the project, and shows you the report from the make utility.
It cleans up the directory after making the project (removes .o and ii_files),
but leaves the
executable in place.
submitrun <class> <project> [command-line args]
Example: submitrun cs341Proj1 checkers checkfile.dat
This runs the project, assuming there is an executable (i.e. submitmake
was run successfully).
Grading and Academic Integrity
Your project will be tested using a variety of command lines, some of which
will be invalid.
Your project will also be tested using a variety of command files which
will test various conditions which your code should handle.
Project grading is described in the Project
Policy handout.
Your answers to 341-Fall04-proj1_questions.txt
are worth 10% of your project grade.
Cheating in any form will not be tolerated. Please re-read the Project
Policy handout for further details on honesty in doing projects for
this course.
Remember, the due date is firm. Submittals made after midnight of the
due date will not be accepted. Do not submit any files after that time.