Input and Output: Printing Messages For The User & Reading Information From The Keyboard |
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: Input / Output: Tracing An Example
The basic steps for getting (and using) user input were both explained in lecture, and are listed below. For this exercise, you need to get the normal, detail-oriented, laborious-to-fill-out, Program Trace Table (NOT the Output Trace Table) and fill it out for the Basic_Keyboard_IO.java file. Because this program relies on user input, you should trace it three times, feeding the program input as indicated below (You may assume that the user presses the 'Start' button as soon as the program appears)
Program Run |
Input Provided |
#1 |
2<enter key> |
#2 |
-1<enter key> |
#3 |
YourFirstName<enter key> |
Part 2: Input / Output: Finding & Fixing Errors
For this exercise, you should download the ICE_05_IO.java file, and find and fix all the errors in it (both compile-time/syntax AND logical/intent errors). You should record all the errors you find in a Program Debug Table, and be ready to explain the errors to the rest of the class.
The purpose of the program is to allow the user to repeatedly tell the program what they want the robot to do. If the user types in something that's not a number the program should politely point that out, then ask them to type in their choice again. If the user types in a number that isn't valid (for example 0 or 10) then program should politely point out that only 1, 2, or 3 are valid choices.
Hint: You may want to consult the blue/yellow boxes listed below to help you remember all the different steps that are required for doing I/O.
Part 3: Input / Output: Writing Code
As per the lecture, the basic steps for getting (and using) user input are listed below. The steps are broadly divided up into stuff you need to do once (in the blue box) and stuff that you need to do once per time that you want to ask the user for input (in the yellow box).
For this exercise, you should obtain a copy of the Starting_Template.java program, rename it Basic_I_O, and modify it so that you present the user with a brief menu of commands to give to the robot. The user should be able to tell the robot to turn left, or to move, or pick something up, or put it down. You should then use the "if" statement to figure out which command the robot should do. A brief outline is provided in Figure 2 (below the boxes).
The following should be done once per
file:
|
All of the following should be done once
per command/service:
|
The steps listed below need to be repeated once EACH time that you want
to ask the user for input:
a.
Note that we use the hasNextInt method here, and
the nextInt method in the next step.
|
Figure 1: How To Get User Input
Figure 1: The Outline Of The Program For This Exercise
Part 5: Input / Output: Writing Code That Repeatedly Asks The User For Input
Take the program that you wrote in the prior exercise, and use Fileà Save As to create another copy that you can keep working on (obviously, you'll need to adjust things so that everything still compiles & runs, etc, etc). Now that you've got that separate copy, build on what you previously did by setting things up so that the program will REPEATEDLY ask the user what to do. In order to make this useful, you should print out an additional menu option which allows the user to quit, make sure that your while loop runs while the user's choice is not that number, and then double-check that the nextLine command is properly clearing out any extra input (you should have been doing this all along, but leaving it out is a common error that sometimes doesn't affect the program – until now J )
Part 6: Tracing Code: I/O
The three objectives for this exercise are
1. to familiarize you with the System.out.println statement
2. to review some of the topics that you'll be seeing on the exam,
3. and to start moving you away from the ultra-detailed program execution traces and towards an equivalent-yet-less-meticulous approach to tracing.
Download the file the ICE_07_Output.java file, and use the more informal OUTPUT Tracing Table to trace what the program does. Once you’ve done that, you should compile and run the program, and check that your trace is correct. For any part of the trace that you didn’t do correctly, you must do a normal, detailed, program execution trace for just that part of the program that you got wrong.
Part 7: (Optional) Writing Code: I/O: Output
Download the file the Starting_Template.java file, and use a loop to move the robot forward 10 intersections. At each intersection, the program should print out the current location (in street, avenue format), using the getAvenue() and getStreet() commands that are built into the robot.
Part 8: Finding and Fixing Errors; Tracing Code: I/O
Download the file named Fix_Keyboard_IO.java. Based on the lecture, find and fix any and all errors in the file.
Part 9: Writing Code: I/O: Input and Output
Download the file the Starting_Template.java file, and then print out the following menu of choices:
You can have the robot do the following:
Please type the number of your choice, then press the 'Enter' key
After you've printed out the above menu, then get the user's input. Make sure that you check that the user has typed in a number! (And don't forget to use the nextLine() command to clear any remaining input!)
Once you've got that number, you should use it to make the robot do whatever the user chose, using a series of if/else statements.