Project #2

CMSC 131

 Due:   Wednesday, Oct. 10, 2012 at 11:00PM

Object-Oriented Programming I

Type of project: Closed

Fall 2012


Flags of the World

 

Objective

This project will give you some practice writing loops and methods, and also introduces the notion of having multiple classes working together in a single project.

 

Overview

The project consists of several classes working together -- most of them are provided for you. Only one class, the one named FlagMaker, will be implemented by you.  The others do not need to be modified by you in any way.  The final product will draw flags of different countries and at different sizes as selected by the input from the console.

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;

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 provided colors and these are all that are needed for the flags:  BLACK, BLUE, GREEN, ORANGE, RED, WHITE, YELLOW.  In real life some flags are made from different variations of these colors (for example one flag might have a dark green and another might have a light green - for ease on this project we will use the provided Color.GREEN for both shades of green).

MyGrid Class

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

import GridTools.MyGrid;

A MyGrid object is a window that you can draw on.  In the center of the window there is a rectangular grid (twice as wide as it is high).  You can color in each region of the grid with any color you want.  Below is an example of an "empty" MyGrid object of size 8 (that means 8 high and 16 wide).

Creating a DrawingGrid Object

A MyGrid object is created in the ExampleDriver provided for you in your CVS repository; if you need to create a new MyGrid object in another driver, you could use a statement like this one:

MyGrid aGrid = new MyGrid(8);  //aGrid will refer to a 8 by 16 drawing grid

This statement will create a MyGrid object named aGrid where each of the 128 squares are already colored white.

Make sure you DO NOT create any MyGrid objects in the methods of the FlagMaker class. These methods will be passed an already created MyGrid object, and that is the only grid they should be drawing in!!

Coloring the Squares on the Grid

Once you have a MyGrid 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 MyGrid class called setColor.  The setColor method of the MyGrid 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.  Also note that rows go across (side to side) and columns go up and down.  For example, below is a picture of what you would see if you executed the code fragment that follows.

MyGrid aGrid = new MyGrid(6);  // creates the 6 by 12 MyGrid object

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

aGrid.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.  This will be a very useful fact when you look for similarities between the flags so you can reuse code you have already written.

 

Determining the size of the MyGrid object

There are two other methods of the MyGrid class that you will need.  Suppose you have a variable called "aGrid" that refers to an existing MyGrid, and you want to know how big it is.  You can use the methods "getHt" and "getWd" to find out.  The signature for these method appears below:

public int getHt()    // the return value is the height of the grid

public int getWd()    // the return value is the width of the grid

For example, if the variable "aGrid" refers to an existing 7 by 14 MyGrid object, then the following expressions would be equal to the integer 7 and 14 respectively:

aGrid.getHt() //returns the value 7

aGrid.getWd() //returns the value 14

WARNING: Be careful - DO NOT USE getWidth() or getHeight() - these are defined but will not give you the numbers you are looking for.

FlagMaker Class

OK, this is where YOU come in!  We have provided a skeleton for this class that you must complete.  The drawFlag method must be in the FlagMaker class with the signature given. (A signature is what is described by the first line of a method definition - name, parameters, etc.)  You may find that writing additional methods can help to eliminate duplicative code.  Since we will not be creating any FlagMaker objects, any methods you choose to write in this class should be declared using the keyword "static".  Since these other methods will only be called from another method in the one class you are writing, it can also use the keyword "private" as shown for the drawExample flag method given.

The drawFlag method that you must write must have the following signature:

public static void drawFlag(MyGrid grid, int countryCode)

Remember that the two parameters (grid and countryCode) are provided to your method by whoever called it.  Those parameters contain the information that your method needs in order to do its job.

The parameter "grid" will refer to an existing MyGrid object that has already been created by someone else.  Your code should not create a MyGrid object anywhere within the methods of the FlagMaker class, it is already provided!  Your method will draw on the grid that is passed in via this parameter.  You may assume that the grid starts off empty (all white squares).

The parameter "countryCode" will be an integer.  The number should correspond to one of the country flags listed in the menu of the ExampleDriver provided for you in your CVS repository (also corresponding to the numbers of The Flag Information Pages).  If the value passed is not one of these values, your program should paint "the error flag" into the grid passed.  The error flag is described in the linked file along with all of the country flags. If the grid passed as the first parameter is not of a valid height for a flag corresponding to the country code passed as the second parameter, the error flag should be painted into the grid passed as the first parameter.  If the height of the grid passed is valid for the flag indicated by the countryCode passed, the flag of that country will be painted filling the grid and matching the description in the linked file. Note: You will need to use the getHt and getWd methods on the grid passed to know its size.

The style for the flags must conform exactly to the examples and descriptions using the basic colors of the Color class (even though some are not  exactly the accurate shade for the flag chosen). 

The size of the grid can be determined by calling the "getHt" and "getWd" methods of the MyGrid class.  The size of the grid dictates the size of the flag you are supposed to draw (the flag must fill up the entire grid, as in the examples given.)  If you are using the driver provided, the driver will get the input from the user, create the grid of the correct size, and pass that grid along with the country code to the drawFlag method.

 

Examples and descriptions of flags

For examples and further descriptions of the flags click here. These are the exact style that you must use for drawing your flags. The linked file shows two examples for each flag so that you can see what it would look like in two different sizes.   Your flag must conform exactly to what is shown shown and described in the linked file when the size is valid for that country's flag.  The flag must take up the entire MyGrid in the size it is passed. Your drawFlag method must be capable of drawing flags of any size that is appropriate for that country's flag (as defined in the linked file), and it must draw an error flag when appropriate. 

Some forms of bad input are handled by the ExampleDriver which then passes a grid of the correct size and a country code outside of those listed to the drawFlag method. For example: If the grid size given is not appropriate for the flag selected, an error flag needs to be drawn.  The smallest flag size allowed will be 4 high by 8 wide; if a size smaller than 4 is requested, the example driver creates a MyGrid object of size 4x8 in which you will display the error flag.  The maximum size for any flag is one with a height of 30; if a size larger than 30 is requested, an the example driver creates a MyGrid object of size 4x8 in which you must display the error flag.  Since these limits are taken care of in the exampleDriver, you do not have to check it again in your code (you can read the exampleDriver for more explanation) - the example driver simply passes an invalid country code so you know that the error flag should be displayed.


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 FlagMaker class ourselves, we will be using a completely different driver class.  Feel free to write your own driver class to test your FlagMaker class a different way, if you want to.

 

Getting Started

Begin the project by "checking out" the project called "Fall2012Proj2".  In order to help you get started, we have included a FlagMaker class with a skeleton version of the "drawFlag" method which you must complete.  It also contains another method which is just an example flag - you can just delete this method when you don't need it anymore.  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 Eclipse tutorial. Follow the instructions given there to check out project Fall2012Proj2.  Refer to Project 0 if you are still having difficulty with the CVS and Eclipse setup on your machine. After checking out the project, when you switch back to the Java perspective, 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 "Fall2012Proj2" files from your CVS repository, you will not be able to submit your work.

 

Requirements

 

Suggestions

 

Submission

Submit your project from Eclipse by right-clicking the project folder and selecting "submit project".  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!

 

Tests

There are several public tests and several release tests for this project. The first of the public tests makes sure that you can draw an error flag correctly. If you are running the ExampleDriver, you should see this error flag when you request any country's flag but in a very large size. The next six public tests are the first three flags shown in the flag description document in each of the two sizes pictured (smaller size then larger size). The release tests each test multiple sizes for each of the flags. These tests appear in the same order as the flag descriptions appear in the linked flag description file.

Web Accessibility