Java Reference
In-Depth Information
2 Analysis and Design (continued)
3. Validate the design. Have a classmate or your instructor look over your
storyboard and make suggestions before you proceed.
4. Implement the design. Translate the design into code for the program.
5. Test the solution. Test the program, finding and correcting errors (debug-
ging) until it is error-free.
6. Document the solution. Print a copy of the source code.
7. Hand in all documents to your instructor.
3 Converting from Sample Data to User Input
In order to practice writing interactive programs that require user input, you
decide to convert a console application that includes sample data to one that has
user prompts and accepts input values. Perform the following steps.
1. Start TextPad. Open the file Money.java from the Chapter03 folder of the
Data Disk. The Money.java file contains program code for a program that
converts any number of coins into dollars and cents. Change the lines of
code that assign sample data into lines of code that prompt the user and
store the answers.
2. With the Money.java source code displayed in the TextPad window, insert
your name and date in the block comment at the beginning.
3. Use TextPad's Replace dialog box to change each occurrence of the text,
Money, to the text, Coins.
4. Save the file on the Data Disk, using Coins as the file name.
5. Add four additional variable declarations for string inputs by typing:
String strQuarters;
String strDimes;
String strNickels;
String strPennies;
at the appropriate place in the program code.
6. Delete the Assigning Values section of code. Replace it with the following
code:
System.out.println("Enter the number of quarters.");
strQuarters = dataIn.readLine();
System.out.println("Enter the number of dimes.");
strDimes = dataIn.readLine();
System.out.println("Enter the number of nickels.");
strNickels = dataIn.readLine();
System.out.println("Enter the number of pennies.");
strPennies = dataIn.readLine();
7. Delete the Calculations section of code. Replace the statements that multiply
the number of coins by their face value with lines of code that parse the
input values and assign the values to variables by typing the following:
Search WWH ::




Custom Search