CSI Laboratory #3
Dirty Laundry

Due: Friday (02/14/2003) by 5 p.m. (WRT 106 office)

Objective: To gain experience using conditionals.

The Scenario. One thing many students have to figure out for the first time when they come to college is how to wash their clothes. Often by the time most students have figured it out, all their underwear is pink and T-shirts and other light-colored items are streaked with lots of interesting colors. In the hopes of helping next year's first-year students adjust more easily to college, we would like you to write a laundry sorting simulator.

The Approach. It is usually a good practice to develop programs incrementally. Start with a simplified version of the full problem. Plan, implement and test a program for this simplified version. Then, add more functionality until you have solved the full problem.

To encourage this approach, we have divided this lab into two parts. For the first, we will describe a laundry sorter with a very simple interface. You should plan, implement and test a program for this problem first. Then, in the second part of the assignment we will ask you to add additional functionality.

Demo: See the running applet for Part A Laundry Sorter application.

Demo: See the running applet for Part B Laundry Sorter application.

 

You are also encouraged to approach each of our two parts in this step-wise fashion. For example, in the first part you might begin by just writing the instructions to construct the necessary graphical objects without worrying about any of the mouse event handling. Once your program can draw the picture, you can move on to figure out how to handle events.

Getting Started

1. First you need to locate a free computer in either Wright 112 or 339. Create a new folder "lab3" on the " Student Z: " drive in the folder "cs061" which you created in lab 1.

2. Start Internet Explorer by Start | Programs | Internet | Internet Explorer and go to http://www.cs.uni.edu/~fienup/cs061f02/labs/lab3/.

3. Download each of the files "objectdraw.jar", "lab_3.gpj", and "LaundryTrainer.java" by right-clicking on the name of each file and selecting "Save Target As". Save these files to your newly created "Z:\cs061\lab3" folder.

4. Start jGrasp by double-clicking on the lab_3.gpj project file that you downloaded to your new lab3 folder. By using this project file you sets up the correct Compiler and run-time settings for the objectdraw package.

5. Click on the "Project" tab at the bottom of the upper-left-hand sub-window of jGrasp. Double click "LaundryTrainer.java" file. Enlarge the "LaundryTrainer.java" window .

Part A

The simulator should begin with three wash baskets on the screen (for our purposes they can just be rectangles or squares). One is labeled "Whites", one "Darks", and the last "Colors". An image showing the kind of display we have in mind appears below. When the simulation begins, a color-swatch square (the laundry item) will appear above the baskets. The user ("laundry trainee") should then click in the corresponding basket. If the user is correct, the program should randomly select a new color for the next item and display it on the screen. For this assignment you may just select among the three colors Color.white, Color.red, and Color.black when creating items of clothing. If the user clicks on an incorrect basket, the original item remains in position for another try.

Design of Part A.

You will need to design an extension of the WindowController class which will display the wash baskets and the item to be sorted. Begin by laying out where all the items go on paper. The picture should look more or less like the one above.

When the program begins, place all the wash baskets (with labels) on the screen. Then, add the item of clothing that is to be sorted. For simplicity you might as well always make the first item have color white. The item should actually consist of two rectangles, a FilledRect which is the appropriate color and a FramedRect which is the same size, but lays on top of the filled rectangle to form a border (otherwise it will be awfully difficult to see a white item!)

Think Constants!

When you lay out the wash baskets and item, make up constants (private static final ...) for all the relevant information. This will make it easier to change things around and also make your program much, much easier to read (presuming you give constants good names). Constant names are by convention written with all capital letters and underscores, e.g. THIS_IS_A_CONSTANT. Your constants may be (and often should be) more complex objects like Location. You can initialize constants with the results of a constructor:

private static final Location SOME_LOCN = new Location(100,200);

Remember that you may NOT have constants whose definition uses canvas (e.g., no FramedRect constants).

The width and heights of wash baskets and the item to be sorted, coordinates of the upper left corner of each of these, etc., are all good candidates for constants.

Identifying the Correct Basket

Once you have done the layout and figured out how to generate new items, all you have to do is to write the code for the method onMouseClick. Because you may be generating the item in one method (begin) and checking to see if the user clicked in the appropriate basket in a different method (the onMouseClick method), you will need to associate some information with an instance variable that will enable onMouseClick to determine which is the correct basket. An appropriate way to do this is to use an instance variable of type FramedRect called correctBasket.

When you generate a new item (in either begin or onMouseClick), you will associate this variable with the rectangle/basket in which an item of its color should be placed. That way when the user clicks on a basket, onMouseClick can simply check to see if the rectangle currently associated with the instance variable contains the point where the mouse was clicked. Then, onMouseClick will either select a new color for the item (if the user was correct) or wait until the user clicks again (if incorrect).

A Warning!

One odd feature of the simple interface that may bother you a bit is a result of the fact that the program selects laundry items randomly. Because the selection is truly random it sometimes picks the same color twice in a row. When this happens and you click on the correct basket for the first item you will get the feeling that the program ignored you. Even though it has actually displayed a new item, the new item looks just like the old one, so you may think nothing changed. Don't let this trick you into thinking that your version of the program (or ours) isn't working correctly. The more advanced interface in part B includes counters in the display that eliminate this problem.

Recycling

Because your program only uses one laundry item at a time, you might as well just recycle it - reusing the same rectangle for each laundry item. Simply change its color rather than creating a new rectangle. In general it is a good strategy to reuse objects rather than creating new ones when possible, as this generally uses a lot less time and does not clutter memory.

Generating random numbers

We have provided a class in the objectdraw package which helps to generate random numbers. This will be used to determine the next color for the item.

Suppose you wish to generate random integers in the range from m to n (where m <= n). Let generator be an instance variable declared to be of type RandomIntGenerator. Create a new random number generator from class RandomIntGenerator, and assign it to generator:

generator = new RandomIntGenerator(m,n);

Now every time you want a new random integer in that range, simply invoke the method

generator.nextValue()

which will return a randomly chosen integer between m and n (inclusive). Thus to construct a RandomIntGenerator that generates integers between 1 and 3 (say, standing for white, dark, and colored), use

generator = new RandomIntGenerator(1,3);

If the value of generator.nextValue() is 1, then make the color of the next laundry item (the swatch) be Color.white. If the value of generator.nextValue() is 2, then make the color of the next laundry item (the swatch) be Color.black . If the value of generator.nextValue() is 3, then make the color of the next laundry item (the swatch) be Color.red.

Part B

Once you get the basic (part A) version working, we would like you to jazz it up a bit. Print the LaundryTrainer.java and "Save As" the file under the name "LaundryTrainerB.java

Here are the extensions we would like you to make to the LaundryTrainerB.java file:

  1. Add labels (Text items) at the bottom of the picture showing the number of correct and incorrect placements. This makes it clearer when the student succeeds in placing the item correctly. They should read something like "correct = nn", "incorrect = mm". The value in the first Text item will be formed by concatenating the string "correct =" with an integer instance variable which keeps track of the number of correct answers. The other is similar.
  2. Students should drag the items to the correct laundry basket rather than just clicking on the basket. Recall from the example in class that you will need an instance variable to label the last mouse position before the drag so you can determine how far to drag the item.

We will let you figure out most of the details of how to add the features for the more advanced versions. One piece of advice is that for the second enhancement you will stop using the onMouseClick method in favor of using the three methods:

Final remarks

Be sure to do the basic version of the lab before attempting the more advanced features. Just work on adding one feature to your program at a time.  Test each new feature thoroughly before going on to work on the next.  Divide the project into many small subprojects and focus on conquering each separate subproblem one at a time.  Divide and conquer. 

Turning in your program

The submission procedure is basically the same as last week. In the same large envelope as last week:

 

Hard-copies, output, and the floppy disk should be turned into the instructor inside the large vanilla envelope. Labs are due by 5 p.m. on Tuesday February 11, 2003.  If you do not turn it in at the beginning of class, you may turn it in at Wright 106, my office.  Slide it under my office door if I am not there when you turn it in.

Good luck and have fun!