Project #2

CMSC 131

Due: Thurs., March 4  at 11:00PM

Object-Oriented Programming I

Type of project: CLOSED

Spring 2010

Letter Maker



This project will give you some practice with loops, static methods, removing code duplication and introduces the notion of having multiple classes working together in a single project.


The project consists of several classes working together -- most of them are provided for you -- only one class (LetterMaker) will be written by you.  The final product will draw large letters made from blocks; the letters 'C', 'U', 'H', 'O', 'I', 'N', and 'Z' will be drawn (scroll down for a while to see some examples.)

This project is considered "CLOSED". Please visit the course web page for information regarding the open/closed policy for projects of this course. 


Color Class

One of the classes  you will use for this project is provided in one of the available Java graphics packages.  It is the "Color" class, and it is part of a package called "java.awt".  That means that any class that wants to use Color objects must begin with the following statement at the top of the file:

 import java.awt.Color;

You do not have to actually create Color objects yourself (that is done for you in the code that is provided by us), but if you wanted to create your own Color object you could use a statement like the one below:

Color myColor = Color.BLACK;  // myColor now refers to the color "black"

There are other ways to generate just about any color you could imagine, but the simple syntax shown above works for the following built-in colors:  BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW.

DrawingGrid Class

The DrawingGrid class has been written for you.  It is part of a package called "CMSC131GridTools".  That means that any class that wants to use DrawingGrid objects must begin with the following import statement at the top of the file:

import CMSC131GridTools.DrawingGrid;

A DrawingGrid object is a window that you can draw on.  In the center of the window there is a square grid.  You can color in each region of the grid with any color you want.  Below is an example of an "empty" 11 by 9 DrawingGrid object:



Creating a DrawingGrid Object

You don't actually have to create any DrawingGrids yourself for this project (they're created by code that has been provided for you), but if you wanted to create a new DrawingGrid object, you could use a statement like this one:

DrawingGrid myGrid = new DrawingGrid(13);  // myGrid will refer to a

//13 by 13 drawing Grid


Coloring the Squares on the Grid

Once you have a DrawingGrid object, you can color in any of the squares you want using any color you want.  To do this, you use the method of the DrawingGrid class called setColor.  The only method that is provided colors in exactly one square of the grid no more.  The setColor method of the DrawingGrid class takes three parameters.  The signature for the method appears below. 

public void setColor(int row, int col, Color color)

The parameters "row" and "col" specify the location of the square on the grid that you would like to color in; the parameter "color" refers to the color you want to use. Note that the first row is row number 0 (not row number 1), and the first column is column number 0.  For example, below is a picture of what you would see if you executed the code fragment that follows.

DrawingGrid myGrid = new DrawingGrid(5);  // creates the 5 by 5 DrawingGrid

myGrid.setColor(3, 3, Color.RED);      // colors in the square at row 3, col 3, using red

myGrid.setColor(0, 2, Color.BLUE);     // colors in the square at row 0, col 2, using blue




If a particular square on the grid has already been colored, there is no harm in re-coloring that same square -- the previous color will simply be replaced with the new color.


Determining the size of the DrawingGrid

There is one other method of the DrawingGrid class that you will need.  Suppose you have a variable called "myGrid" that refers to an existing DrawingGrid, and you want to know how big it is.  You can use the method "getGridSize" to find out.  The signature for this method appears below:

public int getGridSize()    // the return value is the size of the grid

For example, if the variable "myGrid" refers to an existing 17 by 17 DrawingGrid object, then the following expression would be equal to the integer 17:



LetterMaker Class

OK, this is where YOU come in!  We have provided a skeleton for this class that you must complete.  There is basically just one method that you MUST fill in.  It is recommended that you create other methods in this class if you find it useful to do so.  You may find that writing additional methods can help to eliminate duplicative code.  Since we will not be creating any LetterMaker objects, any methods you choose to write in this class should be declared using the keyword "static".

The method that you must write has the following signature:

public static void drawLetter(DrawingGrid grid, String letter, Color color)

Remember that the three parameters (grid, letter, and color) are provided to your method by the method that calls it.  Those parameters contain the information that your method needs in order to do its job.

The parameter "grid" will refer to an existing DrawingGrid object that has already been created by the driver that calls this method.  Your method must not create a DrawingGrid, it is already there and if you create another it won't display or test correctly!  Your method will color squares on the grid that is passed in via this parameter.  You may assume that the grid starts off empty (all white squares).

The parameter "letter" will be equal to one of the following characters: "C", "U", "H", "O", "I", "N", "Z" or "error".  The "error" indicates that the user did not type in a valid letter.  This should display as dot in the bottom right corner as shown below.

The parameter "color" will indicate the color the user has selected or black if the user did not indicate a valid color option.

Using the Color specified by the parameter "color", your method will draw a big block-letter on the grid (see the section below for examples), depicting the letter specified by the "letter" parameter.  The style for this block letter must conform exactly to the examples below.  Note that the size of the grid can be determined by calling the "getGridSize" method of the DrawingGrid class.  The size of the grid dictates how big of a letter you are supposed to draw (the letter must fill up the entire grid, as in the examples shown below.)


Examples of Letters

What follows are examples of the style that you must use for drawing your letters.  We'll show two examples for each letter:  size 11 by 11 and size 21 by 21.  Note that the "brush-thickness" for the letters is always 3 squares wide in these examples.  This brush-thickness is set by a constant in the top of the skeleton code provided.  You must use that constant anywhere the brush-thickness is needed. (More comments about the "brush-thickness" constant below.)  Your letters must conform exactly to the styles below.  The letter must take up the entire DrawingGrid, no matter what size the grid is.  Reminder: the DrawingGrid is always square.

Here are examples of the letter X (which produces the error indicator). It is a dot in the bottom right corner. The height and width of this dot will be determined by the thickness of the brush stroke. Here it is shown with a brush stroke thickness of 3.



Here are examples of the letter C:



Here are examples of the letter U.


Here are examples of the letter H.



Here are examples of the letter O.


Here are examples of the letter I.


Here are examples of the letter N.



Here are examples of the letter 'Z'



 Brush Thickness -- a named constant

The thickness of the lines making the letters should be determined by a constant (a final variable) defined near the top of your code.  This brush thickness will not come from the user (there is no input for this factor), but if you were to change your code, the letter's appearance should change to indicate the change in line thickness.  Any positive odd width should be allowed for the brush stroke thickness.  (If the brush stoke is too large for the size of the grid the results can look very strange, but select a grid size appropriate to your brush thickness.)

For example, if the brush-thickness constant is changed to the value one the I and N in size 11 would look exactly like the following:


Start with using the brush thickness of 3 to get familiar with using loops, but use the constant to make sure there would need to be only that one change to have your letters look like those shown above.

Testing Your Work (the ExampleDriver Class)

Please note that the class you are writing does not include a "main" method.  The expectation is that your LetterMaker class will be used by some other class that does have a main method.  In other words, you are writing a module that can be plugged into a larger project.  To help you to test your class, we have included a class called "ExampleDriver" in the files you have been given.  This class does have a main method.  It provides a simple user interface that will allow you to experiment with the LetterMaker class that you are working on.  Please take a look at the code we have provided in this driver class -- you'll see that it asks the user what kind of letter the user wants to see, then it creates it's own DrawingGrid and then calls the drawLetter method that you will be writing. Note that the letters that you enter to test your project using this driver should be UPPER CASE letters. 

Please note that the ExampleDriver class is not really part of the project assignment!  It is only provided to help you test out your code.  In fact, when we test your LetterMaker class ourselves, we will be using a completely different driver class.  Feel free to write your own driver class to test your LetterMaker class a different way, if you want to.

Getting Started

Begin the project by "checking out" the project called "Spring10Proj2".  In order to help you get started, we have included a LetterMaker class with a skeleton version of the "drawLetter" method, which is the method you must complete.  Remember that you must have set up your repository in order to check out and submit projects.  Information on how to do this can be found in the Managing Projects section of the Eclipse tutorial. Follow the instructions given there to check out project Spring10Proj2. This screenshot shows the exact information that you will need to enter while creating your CVS repository connection. After checking out the project, make sure you switch back to the Java perspective, then you will see the above files in the "Package Explorer" window, and you will be able to start modifying them.

If you write the project from scratch, without checking out the "Spring10Proj2" files from your CVS repository, you will not be able to submit your work.



          You must write well structured code.  This means that you use write methods to contain small, semantically clear units of code.  You will not get full credit if you write your entire drawLetter method without splitting the code up into at least some smaller methods.

         You must use loops to make your code with any stroke thickness and any grid size.  In particular, it is not sufficient (nor practical) to hard code solutions for particular stroke thickness or grid sizes.

         You must comment your code including at least:

         A comment block a the top that has at least your name, your section number and the date you wrote (or started writing) the code

         A short description before each method telling what that method does

         A short description within a method of any code section whose function is not very clear by looking at it



Submit your project from Eclipse by right-clicking the project folder and selecting "submit".  You may submit as many times as you want -- we only grade the submission that scores the highest on the automated tests.  After you have submitted your project, you should visit the submit server.  There you can obtain limited feedback about how well your project is performing.  The number of times you can run our tests on your project (before the due date) is limited.  The earlier you begin working on the project, the more opportunities you will have to see how your project performs on our tests before the due date!



There are three public tests.  The first public test draws a "C" in green on a grid of size 11. The second public test draws a "C" in red on a grid of size 27. The third public test requests an "X" in green on a size 11 grid -- this produces the error code on a grid of size 11. (All three of these are shown above.) There will be one release test for each type of output (random in size and color), and they will be run in the following order: error, 'C', 'U', 'H', 'O', 'I', 'N', 'Z'.  For that reason, it is strongly recommended that you write code to process the letters one-by-one in that order.  (Start with error, then do 'C', then once that is finished and working properly, then work on 'U', etc.) Notice that the points on the submit server do not add up to 100.  The remainder of the points needed to make the project worth 100 points will be graded by the teaching assistant who is looking for good coding style and readability.

Web Accessibility