Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions