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:

  1. Read the brief introduction to the STL at the following URL:
    http://www.msoe.edu/eecs/cese/resources/stl/index.htm
  2. 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.
  3. 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.
  4. Implement the Point class.
  5. Implement the Circle class.
  6. Implement the Rectangle class.
  7. Implement the DisplayObject class.
  8. Implement the Raster class.
  9. Write Proj1.cpp and any other support files needed to implement the functionality of this project.
  10. Copy the makefile from:
  11. /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.

  12. Answer the questions posed in 341-Fall04-proj1_questions.txt. Copy the file
  13. /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:
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:
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:

Please note the following.

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.