You are to create an application that converts English units of measurements to
ID: 3543724 • Letter: Y
Question
You are to create an application that converts English units of measurements to metric units. The application will display a menu similar to the example dialogue whereby the user will enter a value and then select the conversion. The application will then ask if there is another conversion. The application should include separate methods for doing each of the conversions.
Formulas:
Centimeters = inches*2.54
Centimeters=feet*30
Meters=yards*0.91
Kilometers=miles*1.6
Example:
Enter a number to convert: 10
Convert:
1. Inches to Centimeters
2. Feet to Centimeters
3. Yards to Meters
4. Miles to Kilometers
Enter your choice: 1
10 inches equals 25.4 centimeters.
Another conversion (Y/N)? N
Explanation / Answer
Code
import java.util.Scanner;
public class Convert {
public static void main(String[] args) {
while(true){
System.out.print("Enter a number to convert:");
Scanner scanner = new Scanner(System.in);
double value = scanner.nextDouble();
System.out.println(" Convert :");
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");
System.out.print(" Enter your choice: ");
int choice = scanner.nextInt();
switch(choice){
case 1: double centimeters = value * 2.54;
System.out.println(value+" inches equals "+centimeters+" centimeters");
System.out.print("Another conversion (Y/N)? ");
String anotherConversion = scanner.next();
if (anotherConversion.equalsIgnoreCase("y")){
break;
}
else{
System.exit(1);
}
case 2: double centi = value * 30;
System.out.println(value+" feet equals "+centi+" centimeters");
System.out.print("Another conversion (Y/N)? ");
anotherConversion = scanner.next();
if (anotherConversion.equalsIgnoreCase("y")){
break;
}
else{
System.exit(1);
}
case 3 : double meter = value * 0.91;
System.out.println(value+" yards equals "+meter+" meters");
System.out.print("Another conversion (Y/N)? ");
anotherConversion = scanner.next();
if (anotherConversion.equalsIgnoreCase("y")){
break;
}
else{
System.exit(1);
}
case 4 : double kilometer = value * 1.6;
System.out.println(value+" miles equals "+kilometer+" kilo meters");
System.out.print("Another conversion (Y/N)? ");
anotherConversion = scanner.next();
if (anotherConversion.equalsIgnoreCase("y")){
break;
}
else{
System.exit(1);
}
default : System.out.println("Invalid input");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.