Western Oregon University Page 1 of 4 CS-161 Lab #3: Assessors, Mutators, and Conditionals General At the top of the every document that you create (word processing files or source files) include: /** * Description of the class or document. * * @author YOUR NAME * @version CS161 Lab #, mm/dd/yyyy (replace with the last edit date) */ Submit in this lab via Moodle using the “Lab #” link. Your assignment must be uploaded by midnight of the due date. Moodle will automatically close the link at that time. It is strongly highly recommend you do not wait until the very last minute to submit your work. Concepts The purpose of this lab is to expand your experience with writing Java source code. In this lab we will again implement some of the basic elements of class definitions: fields and methods. We will expand our knowledge of Java statement syntax and introduce the conditional control structure (if statements). We will also implement Accessor and Mutator methods for a field as part of this lab. Additionally, this lab continues your exploration of the BlueJ environment, BlueJ’s editor, and gives you more experience with the object workbench. Background Read the remainder of chapter 2 in your text, and work through several of the exercises in the chapter to practice the concepts. Use the online tutorials (refer to the class web site) to get any further clarifications that you may need after reading the chapter material and doing some of the practice exercises. Be sure to focus on the sections on CONDITIONAL STATEMENTS and LOCAL VARIABLES. You will need to understand these concepts in order to complete this lab. Assignment Instructions Continuing with the Picture Project (Using your stop light version; note that questions #1 and #2 are the challenge questions from Lab 2; they are now required for this lab). You might consider renaming the “House” class to “StopLight” if you have not already done so. 1. Create an ACCESSOR method and a MUTATOR method for the currentColor field. These MUST have the following signatures: public void setCurrentColor( int newColor) public int getCurrentColor( ) Write the complete methods (signature and body). Refer to the section on accessors and mutators in the text for help with these methods. Note that using the mutator to change the value of the field DOES NOT change the color of the light shown in the drawing; its only purpose is to change the value of the field. The color in the drawing will only change when draw( ) is executed, and only when you finish the modifications to draw( ) in #2 below. Please review the material from chapter #2 on accessors and mutators. These are very important methods that we will write for just about every field that we create in all Java programs. Western Oregon University Page 2 of 4 CS-161 Lab #3: Assessors, Mutators, and Conditionals 2. Modify the “draw( )” method so that it draws the light showing the correct color as indicated by the field “currentColor”. Make sure that you have initialized currentColor in the constructor. The draw( ) method will use three separate “if” statements to test the value of the currentColor field to determine how the light should be shown. A strong hint here is to use the three methods (redLight, greenLight, yellowLight) written in Lab 2 to actually change the color of the lights inside of your “if” statements. Test this by invoking the setCurrentColor method, and then invoking the draw method. In Chapter #2, you are referred to Appendix C for details on other Java relational (comparison) operators. An important operator is the equality operator “==” which tests if two primitive values are equal to each other. Your “if” statements can use this operator to test currentColor, for example: if ( currentColor == 1) … 3. Create a NEW CLASS, field, and constructor Create a new class for the project called “LightController”. Delete most of the stuff in the class that BlueJ auto generates for you. DEFINE A FIELD: This class will have one field to hold an instance of your stoplight picture (“House” or “StopLight” depending upon whether you have renamed the class or not). DEFINE A CONSTRUCTOR: In the constructor you need to create an instance of your StopLight (or House) class, and assign it to the field that you created above. Then, also in the constructor, call/invoke the “draw( )” method on your StopLight. Compile and test this new class; just by creating an instance of your new LightController class in BlueJ, it should automatically open up a canvas and draw your stoplight. 4. Add a “nextLight( )” method to your LightController class The normal behavior for a stop light in the US is to cycle from Red to Green to Yellow to Red (and continues with this pattern). Create a new method called “nextLight()” that has the following signature: public void nextLight( ) This method will cycle the light from its current state (defined by the value in the field “currentColor” from Lab #2) to the appropriate next color in the normal cycle. This will require you to test against the currentColor state value to determine the next color to set the light to. However, since that field is a private field in an external class, you MUST use the accessor method that you wrote to test the current value of the field. You can use a method directly in an if statement that would look something like the following: If ( stoplight.getCurrentColor( ) == 1) { //Code to change light to the next color using the public methods } else if … Western Oregon University Page 3 of 4 CS-161 Lab #3: Assessors, Mutators, and Conditionals You are to use the methods you wrote in Lab #2 to change the light and the currentColor field state to the next color in the cycle (the redLight(), yellowLight(), greenLight() methods). This method makes only one change to the currentColor and the actual light each time that it is called. A slightly more complicated use of if-else statements is to nest them in a style known as cascading “if/else if’s” in the following form: if ( booleanStatement ) { … //your code } else if ( booleanStatement ) { … //your code } else if ( booleanStatement ) { … //your code } else { … //your code to handle the error condition } For the nextLight logic some form of nested if statements or compound boolean expressions are required (think about why this is so). The cascading form of if-then-else- if’s is one method that works well for this problem. Additionally this cascaded form is more “robust” than simply using 3 separate if statements as we can now trap for error situations where currentColor has some value other than 1, 2, or 3. The final “else” traps these error conditions (if the currentColor is somehow 0 or some other invalid value); a common behavior would be to set the light to “red”. This would then put the system back into a valid state and further calls to nextLight( ) would work properly. There are several very important concepts demonstrated in this class: 1) Use of “accessors” and “mutators” to get at the state values of external objects 2) Separation of logic behavior from display behavior. The StopLight class just draws a stoplight and has methods to set the light color. The LightController has the logic that controls the light to produce the application behavior that we want from the light, but does not directly have any display (I/O) code. This is a very important design concept that you should always try to use. OPTIONAL CHALLENGE QUESTIONS (not for extra credit, just to extend your knowledge): For this challenge exercise you need to research 2 things: how to do a loop and how to temporarily pause your program (look in the “canvas” class code). The challenge is to write a method called “simulateLight” that will loop many times and in the loop calls your nextLight( ) method, then pauses for a short amount of time. This should then look much like what a stop light would look like while it is operating on a street intersection. Also, a hint on using BlueJ: You can kill an executing Java program by simply clicking on the twirling candy-cane bar in the BlueJ main window. Turn in your entire BlueJ Project that you created/modified. Starting with Lab #3, it is required that you run the code formatter (BlueJ Auto-layout) over ALL of the classes before you Zip and submit your lab. Any code you add or modify should also be commented when appropriate (look at the example code in the book and study the comment style). Western Oregon University Page 4 of 4 CS-161 Lab #3: Assessors, Mutators, and Conditionals 1) ZIP the entire project folder, DO NOT delete any of the files. You will upload the single ZIP compressed file. Please ONLY submit Zip formats. Other compression formats require the grader to spend more time opening various tools to handle other formats, so only ZIP will be accepted. 2) Submit any required document as a separate file. 3) Log in to Moodle. 4) Click the Lab # link in the week # activity block in the class Moodle site, then browse to your document and upload the document you created. 5) If you need to submit an updated version, click on the submit link and you will see the file you previously uploaded, click the edit these files button. On the right of the file name you will see this icon . Click on the icon, delete your previous submission, and then upload your updated version.