Java Reference
In-Depth Information
FIGURE 4-68
1. Start TextPad. Save the program as a Java source code file with the file name,
MyType.
2. Enter general documentation comments, including the name of this lab,
your name, the date, and the file name.
3. Import the java.io.* package and the javax.swing.JOptionPane package.
4. Type the class header and an opening brace to begin the class.
5. Type public static void main(String[] args) and an opening brace
to begin the main() method header.
6. Declare the following variables using the code:
String strChoice, strTryString, strTryInt, strTryDouble;
int choice, tryInt;
double tryDouble;
boolean done = false;
7. Begin a while(!done) loop to repeat as long as the user does not click the
Cancel button.
8. Inside a try statement, enter code to display an input box with three choices,
as shown in Figure 4-68.
9. Type choice = Integer.parseInt(strChoice); on the next line to
parse the value for the choice entered by the user.
10. Create a switch statement to test for each of the three choices. Type
switch(choice) as the header and then press the ENTER key. Type an
opening brace.
11. Enter a case statement for each of the three choices, using pages 254 through
257 as a guide for coding the switch, case, and break statements.
• Case 1: If the user enters a 1, display a message that informs users they are
correct, as any input can be saved as a String. Enter the break statement.
• Case 2: If the user enters a 2, parse the value into tryInt. Display a message
that informs users they are correct. Enter the break statement.
(continued)
Search WWH ::




Custom Search