Question: Complete the following program to implement the user interface of the preceding exercise. For simplicity, only the uimport java.util.Scanner; public class UnitConverter { public static

Complete the following program to implement the user interface of the preceding exercise. For simplicity, only the uimport java.util.Scanner;
public class UnitConverter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
boolean done = false;
double factor1=1;
double factor2=1;
String unit1="";
String unit2="";
while (!done)
{
boolean getSecond = true;
String command = in.next();
System.out.println("From unit (in, cm, m, again, quit): "+ command);
if (command.equals("in"))
{
factor1=2.54; // Convert to cm
unit1= command;
}
else if (command.equals("cm"))
{
factor1=1; // Already in cm
unit1= command;
}
else if (command.equals("m"))
{
factor1=100; // Convert to cm
unit1= command;
}
else if (command.equals("again"))
{
getSecond = false;
}
else if (command.equals("quit"))
{
done = true;
getSecond = false;
}
else
{
System.out.println("Sorry, unknown unit.
");
getSecond = false;
}
if (getSecond)
{
System.out.print("To unit: ");
unit2= in.next();
if (unit2.equals("in"))
{
System.out.println(unit2);
factor2=2.54; // Convert from cm
}
else if (unit2.equals("cm"))
{
System.out.println(unit2);
factor2=1; // Already in cm
}
else if (unit2.equals("m"))
{
System.out.println(unit2);
factor2=0.01; // Convert from cm
}
else
{
System.out.println("Sorry, unknown unit.
");
getSecond = false;
}
}
if (getSecond)
{
double value = in.nextDouble();
double result = value * factor1/ factor2;
System.out.println(value +""+ unit1+"="+ result +""+ unit2+"
");
}
}
}
}nits cm, m, and in are supported.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!