Programming Assignment #21B

advertisement
Programming Assignment #21BName: MetricConversions
Write a program that will convert from the system of weights and measure commonly
used in the United States to the metric system and vice versa.
1.
2.
3.
4.
Inches to Centimeters
Feet to Centimeters
Yards to Meters
Miles to Kilometers
5.
6.
7.
8.
Centimeters to Inches
Centimeters to Feet
Meters to Yards
Kilometers to Miles
Formulas to convert English units to metric units:
inches * 2.54 = centimeters
feet * 30 = centimeters
yards * 0.91 = meters
miles * 1.6 = kilometers
The program should start off by asking the user for a number to convert. Once they enter
a number, a menu should pop up with the options listed above. The program should have
a total of 9 methods besides the main(). One for each option above plus one for the
output. You need to make sure that the user enters a number between 1 & 8. You also
must include your personalized header and footer. Finally, the user must have the option
to repeat.
**The following page consists of a template that you must use to complete the program.
In other words, you must get you program to work with what I have already developed.
(Note: You can copy and paste the information into your application)
import TerminalIO.KeyboardReader;
public class MetricConversions
{
public static void main(String [] args)
{
//Instantiate KeyboardReader and MyHeader
KeyboardReader reader = new KeyboardReader();
//Declaration of variables
final int num = 21;
final String name = "MetricConversion";
double number, converted;
int choice;
char repeat;
String label1, label2;
//Calls your personalized header method
do
{
//number Input
number = reader.readDouble("\nPlease enter a number that you would like to convert: ");
//Menu
System.out.println("\nConvert:");
System.out.println("1. Inches to Centimeters
System.out.println("2. Feet to Centimeters
System.out.println("3. Yards to Meters
System.out.println("4. Miles to Kilometers
5. Centimeters to Inches");
6. Centimeters to Feet");
7. Meters to Yards");
8. Kilometers to Miles");
//choice Input
choice = reader.readInt("\nPlease enter a choice: ");
//Checks to make sure that 1-8 was entered
while (choice <1 || choice >8)
{
choice = reader.readInt("Invalid choice. Please choose again.");
}
//Case statement to see which method to call
switch(choice)
{
case 1:
//Method Call
label1 = "inches";
label2 = "centimeters";
break;
case 2:
//Method Call
label1 = "feet";
label2 = "centimeters";
break;
case 3:
//Method Call
label1 = "yards";
label2 = "meters";
break;
case 4:
//Method Call
label1 = "miles";
label2 = "kilometers";
break;
case 5:
//Method Call
label1 = "centimeters";
label2 = "inches";
break;
case 6:
//Method Call
label1 = "centimeters";
label2 = "feet";
break;
case 7:
//Method Call
label1 = "meters";
label2 = "yards";
break;
default:
//Method Call
label1 = "kilometers";
label2 = "miles";
break;
}
output(converted, number, label1, label2);
repeat = reader.readChar("Would you like to repeat the program: ");
}while (repeat == 'Y' || repeat == 'y');
//Call your personalized footer method
}
Example Output
*
* *
* * * **
* * * ***
* * * *
* *
** *
* **
* *
****
*
* **
****
*
*
*
****
****
**
**
****
****
****
* *
* *
Please enter a number that you would like to convert: 10
Convert:
1. Inches to Centimeters
2. Feet to Centimeters
3. Yards to Meters
4. Miles to Kilometers
5. Centimeters to Inches
6. Centimeters to Feet
7. Meters to Yards
8. Kilometers to Miles
Please enter a choice: 1
10.0 inches equals 25.4 centimeters
Would you like to repeat the program: y
Please enter a number that you would like to convert: 40
Convert:
1. Inches to Centimeters
2. Feet to Centimeters
3. Yards to Meters
4. Miles to Kilometers
5. Centimeters to Inches
6. Centimeters to Feet
7. Meters to Yards
8. Kilometers to Miles
Please enter a choice: 4
40.0 miles equals 64.0 kilometers
Would you like to repeat the program: y
Please enter a number that you would like to convert: 99
Convert:
1. Inches to Centimeters
2. Feet to Centimeters
3. Yards to Meters
4. Miles to Kilometers
5. Centimeters to Inches
6. Centimeters to Feet
7. Meters to Yards
8. Kilometers to Miles
Please enter a choice: 8
99.0 kilometers equals 61.875 miles
Would you like to repeat the program: n
This is my ending.
Press any key to continue...
Download