Java Reference
In-Depth Information
2 Using switch and try Statements to Validate User Input
(continued)
• Case 3: If the user enters a 3, parse the value into tryDouble. Display a
message that informs users they are correct. Enter the break statement.
• Case 4: Set done equal to true. Enter code to display a closing message.
Enter the break statement.
• Case default: throw a new NumberFormatException.
12. Close the switch statement with a closing brace.
13. Create a catch statement by typing catch(NumberFormatException e)
and then an opening brace.
14. Display an appropriate message directing the user to try again and then close
the catch statement with a closing brace.
15. Close the try statement, the while statement, and the main() method with
closing braces.
16. Save the file on the Data Disk using the file name MyType.java.
17. Compile the program. If necessary, fix any errors in the TextPad window,
save, and then recompile.
18. Run the program. Enter various values for each menu choice. Check your
answers.
19. Print a copy of the source code for your instructor.
20. As an extra credit assignment, add choices for long, byte, and boolean data
types.
3 Writing User-Defined Methods
A small proprietary school that offers distance-learning courses would like an
application that calculates total tuition and fees for their students. Users will
input the number of hours; the program then will calculate the total cost. For
full-time students taking greater than 15 hours of courses, the cost per credit
hour is $44.50. For part-time students taking 15 hours or fewer, the cost per
credit hour is $50.00.
1. Start TextPad. Save the file on your Data Disk with the file name
Tuition.java.
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 java.text.DecimalFormat package.
4. Type the class header and opening brace for the public class, Tuition.
5. Enter the main() method header.
6. Declare an integer variable named hours. Declare double variables named
fees, rate, and tuition.
7. Enter the following method calls and then close the main() method with a
closing brace.
displayWelcome();
hours = getHours();
Search WWH ::




Custom Search