CoSc 200 Laboratory #2
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.
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.
The image above should be a working version of the simple laundry sorter. You should experiment with this demonstration version after you finish reading part I to make sure you understand exactly what we have in mind.
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 some graph 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!)
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 Locations. 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.
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.
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).
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 generate integers between 1 and 3 (say, standing for white,
dark, and colored), use
generator = new RandomIntGenerator(1,3);
If n is the value of generator.nextValue(), make the
color of the item be Color.white if n is 1,
Color.black if n is 2, and Color.red if
n is 3.
Now define criteria for determining where each color should go. The simplest criterion is based on the sum of the three color components. If the sum of the component numbers is less than 230, decide it is dark, if it is greater than 600, decide it is white. Otherwise it is colored. After you get the program working you might want to experiment with other criteria.
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 be dropping the onMouseClick method in favor of using the three methods:
The image above will be a working version of the simple laundry sorter. You should experiment with this demonstration version to make sure you understand exactly what we have in mind.
As with last week's lab, your grade will have components based on style and on correctness. Please note that each week we will likely be adding new style guidelines that we will be looking for in your programs.
Grading Point Allocations Value
Feature Style, Design, and Efficiency (10 pts total) 3 Includes informative comments. In particular, there is a description of the program, the author's name, a date, and an acknowledgement statement at the top of the file, and above each method is a description, in general terms, of what that method does. 2 Uses meaningful names and follows naming conventions. 2 Uses constants when appropriate and follows naming conventions.. 2 Uses good and consistent formatting. 1 Generates new objects only when necessary. Correctness (10 pts) 2 Generates a new color swatch only if previous one placed correctly. 2 Swatch displayed in the correct initial position; returns to original location if incorrectly sorted. 2 Updates # correct and # incorrect appropriately. 2 Drags swatch properly (-1 pt if use clicking instead of dragging) 2 Includes an appropriate added feature, e.g., a warning displayed when the user starts to drag the clothing item outside the laundry item or the size of the clothing item changes randomly
Make sure your work is ready to submit. Return to JCreator and double check that you included your name, the date, and an acknowledgement statement in a comment at the start of your ".java file", your ".java" file compiles successfully, and the program executes correctly.
To submit your work.
Submission Deadline: 3:00pm on Tuesday, January 17. Note that once you submit, it is impossible to submit a revision.
Resubmission and Late Submission Deadline: 3:00pm on Tuesday, January 24. A 5 point penalty will be assessed for a resubmission or late submission. These should be submitted to Lab02Late.