Date: Wed, 18 Sep 2002 18:05:02 -0500 (CDT) From: Mark Jacobson To: 810-061-01@uni.edu, 810-061-02@uni.edu Subject: Wed 09/18 lecture summary... Laundry tips for beginners... Hi 061 students, If this is not readable in your GUI email program for some reason, be sure to look at this email note from our class web page at: http://www.cns.uni.edu/~jacobson/061 dirtyItem *********** ** ** *********** *********** ************ ************ ** Darks ** ** Colors ** ** Whites ** *********** ************ ************ darksPile colorsPile whitesPile 1 2 3 The above diagram shows that I plan to use the randomly generated colorChoice intEGER int value as follows: --- If the colorChoice was == to 1 then the dirtyItem will have its corresponding FilledRect object set to Color.black else if colorChoice was == to 2 then dirtyItem will look RED, i.e. Color.red after you make it or its corresponding FilledRect object be RED, be Color.red... else // it must have been == 3, cause that is all that is left... dirtyItem will look WHITE, i.e. Color.white will be what dirtyItem object had its .setColor() method set it to. When do you need to generate a new random number 1, 2 or 3 by using the nextValue() method of the generator object, i.e. use the generator that you created in the begin() startup method by doing this statement: generator = new RandomIntGenerator(1, 3); // Create the random // generator that only // does 3 values: 1, 2 and 3 Do you need to store the generator.nextValue() result in an int variable? Yes, yes, yes. YES, you need an int variable!!!!! --- Does that int variable need to be a. a private instance variable for the entire class, or -- b. can it be a local variable, local just to that onMouseClick() method? Answer: It depends on what strategy you are using to recognize when the user of your program has clicked on the right laundry pile, for the current dirty clothing item. The correct answer is a, it needs to be a PIV (private instance variable), if you want to REMEMBER the color value to use it in processing the user's mouse clicks. --------------- AND OR NOT aside comments you can ignore ----------- --------------- cause you do not need to use &&, || or ! ----------- --------------- --- for this program. ----------- The symbol for AND logical operation in Java is && and is exactly the same as the AND you are studying in 810:080 discrete structures. The symbol for OR logical operation, or the syntax for OR in Java, is the || (two vertical bars or two pipe symbols in a row - see your keyboard key with the backslash \ on it, just above the Enter key on most every PC keyboards. AND is && in Java OR is || in Java You do NOT need to use NOT, at least NOT yet, but the symbol for NOT is ! in Java, i.e. one exclamation mark symbol. You most certainly do NOT have to use && or || in this assignment either. --- We will cover those and see examples later on. --------------- end of the IGNORE this if you want to .. ----------- --------------- AND OR NOT aside comments you can ignore ----------- -------------------------------------------------------------------- Do you need to store the generator.nextValue() result in an int variable? Yes, yes, yes. YES, you need an int variable!!!!! --- Does that int variable need to be a. a private instance variable for the entire class, or -- b. can it be a local variable, local just to that onMouseClick() method? Answer: It depends on what strategy you are using to recognize when the user of your program has clicked on the right laundry pile, for the current dirty clothing item. If you do NOT use the approach suggested in the handout, then I would recommend making your colorChoice int variable be a PIV (private instance variable). Its a good approach. So a. would the the RIGHT answer, if you do NOT do the handout's suggested approach. --------------------------------------------------------------------------- Handout's suggested approach - copied and pasted from the online versino at http://www.cs.uni.edu/~fienup/cs061f02/labs/lab3/lab3.htm which is linked to from http://www.cns.uni.edu/~jacobson/061 --------------------------------------------------------------------------- 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). --------------------------------------------------------------------------- End of your handout Identifying the Correct Basket hints from Fienup... --------------------------------------------------------------------------- So if you do NOT use the handout's suggested approach, then the following approach that relies upon the PIV (private instance variable) could be used. Remember the colorChoice (oh ya, its a PIV, so its remembered) that you set to 3 (for WHITE in the begin() method), or that the generator.nextValue() integer 1, 2 or 3 was stored in when the user was correct and dropped the last dirty clothing item into the RIGHT PILE for its color. if ( darksPile.contains( point ) AND colorChoice == 1 ) { YOU DID IT!!!! Choice was right, the dirty item is in the right pile. That BLACK shirt or BLACK slacks or BLACK sock or BLACK dress is a nice piece of clothing, by the way.... } else if ( colorsPile.contains( point ) AND the colorChoice was 2 ) { You just dropped a red colored textile item into the colors pile. Way to go! You are becoming a laundry sorting expert. What do you see in the dirtyItem on deck box now? Remember, it could happen that there are two of the same color in a row sometimes! It could be another RED, but probably 2/3 of the time or 67% of the time it would likely be a BLACK or a WHITE next, right? } else if ( colorsPile.contains( point ) AND the remembered PIV variable colorChoice contains int value 3 ) { Way to go. You clicked in the whitesPile FramedRect, and the dirtyItem was WHITE too. ---------- See that 3rd box, the one on the far right, shown below. It contained your mouse click. The dirtyItem box above all of them was showing WHITE, so hopefully that dirty white shirt or that dirty white T-Shirt item or that dirty white slacks, shorts, or dress will come out looking real fine. We are so glad you did not put the white item in with colors or darks, for sure! } *********** ************ ************ ** Darks ** ** Colors ** ** Whites ** *********** ************ ************ darksPile colorsPile whitesPile 1 2 3 dirtyItem *********** ** ** *********** *********** ************ ************ ** Darks ** ** Colors ** ** Whites ** *********** ************ ************ darksPile colorsPile whitesPile 1 2 3 Does that int variable need to be a. a private instance variable for the entire class, or -- b. can it be a local variable, local just to that onMouseClick() method? If you choose to use the HINT and suggestion on your handout of the Identifying the Correct Basket section -- (only two paragraphs) ------------------------------ --- then your int variable could certainly be a LOCAL variable and it would not need to be a PIV. b. can it be a local variable, local just to that onMouseClick() method? YES IT CAN! --- Why can it be a local variable, instead of a PIV? Cause you do NOT need it in the onMouseCilck() method! Matter of fact, the onMouseClick() method only needs to use the .contains() method ONCE. You do NOT have to use .contains() THREE times in your code! --- ----------- ----- You have an PIV private instance variable of type - - - FramedRect called correctBasket. ------------- Here are the THREE possible situations that would occur if you use this approach, and the PIV correctBasket to refer to the correctBasket for the given color B for Black, R for Red, and W for White for indicating the current color of the dirtyItem. 1. Dirty item is BLACK, B dirtyItem *********** ** B ** *********** correctBasket *********** ************ ************ ** Darks ** ** Colors ** ** Whites ** *********** ************ ************ darksPile colorsPile whitesPile 1 2 3 2. Dirty item is RED, R dirtyItem *********** ** R ** *********** correctBasket *********** ************ ************ ** Darks ** ** Colors ** ** Whites ** *********** ************ ************ darksPile colorsPile whitesPile 1 2 3 3. Dirty item is WHITE, W dirtyItem *********** ** W ** *********** correctBasket *********** ************ ************ ** Darks ** ** Colors ** ** Whites ** *********** ************ ************ darksPile colorsPile whitesPile 1 2 3 Whew! This should be enough hints to get you through part A, with help from the two graduate student TAs and myself. Rewrite some of these hints and reproduce these diagrams yourself on a sheet of paper, if you don't fully understand them. That will help you toward your EUREKA or your AHA experience where you do understand more of what you need to do and have clearer ideas of things to try. Biction. Almost time for UNI VB (VolleyBall, not Visual Basic), so I am out of here! :-) - - - - Mark