COSC 60 - Lab 3

advertisement
COSC 60 – Lab 6 (Spring 2009)
(1) Write a program that asks the user to enter a distance in meters. The program will then
present the following menu of selections:
1. Convert to kilometers
2. convert to inches
3. convert to feet
4. quit the program
The program will convert the distance to kilometers, inches, or feet, depending on the user’s
selection. Here are the specific requirements:
 Write a method called getKilometers whose return type is double. This accepts the number
of meters as an argument. The method should then return the argument converted to
kilometers. Convert the meters to kilometers using the following formula:
kilometers = meters * 0.001
 Write a void method called showInches, which accepts the number of meters as an
argument, the method should display the argument converted to inches. Convert the
meter to inches using the following formula:
inches = meters * 39.37
 Write a void method named showFeet, which accepts the number of meters as argument.
The method should display the argument converted to feet. Convert the meter to feet
using the following formula:
feet = meters * 3.281
 Write a void method named menu that displays the menu selections. This method should
not accept any arguments.
 The program should continue to display the menu until the user enters 4 to quit the
program.
 The program should not accept negative numbers for the distance in meters.
 If the user selects an invalid choice from the menu, the program should display an error
message.
 When you output result, use DecimalFormat class to round decimal to two places.
Here is an example of the program’s input and output. User input is shown in bold.
Enter a distance in meters : 500
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice : 1
500 meters is 0.5 kilometers.
1.
2.
3.
4.
Convert to kilometers
Convert to inches
Convert to feet
Quit the program
Enter your choice : 3
500 meters is 1640.5 feet
1.
2.
3.
4.
Convert to kilometers
Convert to inches
Convert to feet
Quit the program
Enter your choice : 4
Bye
(2) (As time permits) In the last lab, you worked on the following program. Your goal for today’s
lab is to take the same program and create some useful methods in the program and call it from
the main() method. The point here is to organize your program in a more structured manner.
Write a program to read a list of exam scores (integer percentage in the range 0 to 100) and to
output the total number of grades and the number of grades in each letter-grade category (90 to
100 = A, 80 to 89 = B, 70 to 79 = C, 60 to 69 = D, and 0 to 59 = F). The end of the input is indicted by
a negative score as a sentinel value. (The negative value is used only to end the loop, so do not use
it in the calculations). A sample dialog is given below. User input is shown in bold
Please enter the grades :
87
98
50
86
85
78
73
85
72
72
70
72
66
63
-1
Total number of grades = 14
Number of A’s = 1
Number of B’s = 4
Number of C’s = 6
Number of D’s = 2
Number of F’s = 1
Note: Your programs should be neat and well documented. Follow all the java style and
documentation guidelines explained in class and in your text book.
Submission: Show (i) pseudo code, (ii) source code, and (iii) sample run of all your programs to the
TA.
Download