Making Decisions: If, If/Else Statements
Note: Please keep the programs that you create today, in case you have a dispute about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save their own, individual copy
Part 1: Code Tracing
You should download the code from the website for ICE_04_Demo_01.java, and trace the code. For in-class exercises like this one, feel free to skip to the new topic for today (in this case, skip to line 59), but don't forget to put all the Things/Walls/etc into the trace table as if you had traced through all the set-up code.
Part 2: Error Finding
You should download the code from the website for ICE_04_01_Errors..java, and find all the errors in the file. You should start with the compiler errors (obviously), and then run the program and see if it does what it's supposed to.
Often, programmers will leave comments in the code that hint at what the program should accomplish. Between the comments, and your own sharp intellect, you should be able to figure out what the program should be doing. Once you've figured out what the program is supposed to do, you should trace the code, and determine if it's actually doing what it's supposed to.
By the way, as the course progresses, fewer and fewer of the errors will be compiler errors, and more and more will be intent errors J.
Part 3: Writing Code
You should continue using the file you were working on in the previous part. For this part, create a new method, putThingSafer, which does what the flowchart in Figure 1 indicates it should. You should then modify the program so that instead of calling putThing, your code calls putThingSafer. Also, try putting in an extra 3 calls to putThingSafer, and see if your robot breaks (since it won't have 6 Things in it's backpack)
Figure 1: What putThingSafer should do
Part 4: If ... else, canPickThing, frontIsClear
Download the file named ICE_04_If_Else.java from the website. When you run it, you should see a city that looks like the one below (You may have to adjust the "Zoom" slider to see everything all at once).
When you examine the code for the program, you'll see that your job is to change chooseDirection (and ONLY chooseDirection - you are NOT allowed to change anything else), so that once the program has finished, the robot is at the other end of the pipe (at (4, 17)).
Before you begin, look closely at the city, and then answer these
Part 5: Retrieving A Thing (Optional)
Download the file I_04_If.java. Notice that the city's Walls and Things will always look like:
The robot, however, will randomly face one of the four possible directions. Your job is to write some code in main that will figure out which hallway the robot is facing, travel down that hallway, retrieve the Thing at the end, and return to the starting point (5, 6). You can do this using only four basic commands, plus the new command frontIsClear. frontIsClear will tell you "true", or "Yes" if (and only if) the robot can move 1 intersection forward without crashing into a wall. So, in the above picture,
will cause Jo to first ask "Is there no wall in front of me?", come up with the answer "Yes, there is no wall in front of me", feed that into the "if" statement, and thus take a step forward.
Once you've got the basic idea for figuring out which hallway the robot is pointing down, double-check your idea with the instructor. Then create four separate services – one for each hallway. If you can, feel free to re-use these services between hallways.