89.8k views
0 votes
Complete the following program to implement the user interface of the preceding exercise. For simplicity, only the units cm, m, and in are supported.

UnitConverter.java
import 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;
System.out.print("From unit (in, cm, m, again, quit): ");
String command = in.next();
if (command.equals("in"))
{
factor1 = 2.54; // Convert to cm
unit1 = command;
}
else if (command.equals("cm"))
{
. . .
}
else if (command.equals("m"))
{
. . .
}
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"))
{
factor2 = 2.54; // Convert from cm
}
else . . .
}
if (. . .)
{
double value = in.nextDouble();
double result = value * factor1 / factor2;
System.out.println(value + " " + unit1 + " = " + result + " " + unit2);
}
}
}
}

User Mihkel
by
7.7k points

2 Answers

5 votes

Final answer:

The Java program is an implementation of a unit converter that employs dimensional analysis and conversion factors to convert measurements between inches, centimeters, and meters.

Step-by-step explanation:

The Java program provided is designed to convert measurements between different units using dimensional analysis and conversion factors. When converting units, it is essential to have conversion factors that allow for the cancellation of the original units, resulting in the desired unit. The process follows several steps to perform conversions accurately:

  1. Identify the 'given' information including the number and its unit.
  2. Determine the desired unit for the final answer.
  3. Apply conversion factors that utilize ratios, which will cancel out the original units and leave the desired unit.
  4. Multiply the top numbers and divide by the bottom numbers of these ratios to complete the conversion.

For instance, to convert inches to centimeters, you would multiply the number of inches by 2.54 (since 1 inch = 2.54 centimeters). The program will continue to ask for input until the user enters 'quit'.

User DeLac
by
7.2k points
5 votes

Final answer:

To complete the UnitConverter program, the missing conversion factors for cm and m should be added, and the logic for conversion should be checked to proceed only when necessary. Dimensional analysis is used with appropriate conversion factors to convert between units.

Step-by-step explanation:

To complete the given Java program, we need to fill in the missing conversion factors and logic for the UnitConverter to correctly convert between centimeters (cm), meters (m), and inches (in). Following the provided structure, here's what we can add to the UnitConverter Java program:

else if (command.equals("cm"))
{
factor1 = 1; // 1 cm is the base unit
unit1 = command;
}
else if (command.equals("m"))
{
factor1 = 100; // 1 m = 100 cm
unit1 = command;
}
if (unit2.equals("in"))
{
factor2 = 2.54; // Convert from cm
}
else if (unit2.equals("cm"))
{
factor2 = 1; // Base unit remains the same
}
else if (unit2.equals("m"))
{
factor2 = 100; // Convert from cm
}When the user inputs a value after selecting the units, the following condition should be corrected to check if the conversion should proceed:if (!done && getSecond)
{
double value = in.nextDouble();
double result = value * factor1 / factor2;
System.out.println(value + " " + unit1 + " = " + result + " " + unit2);
}
This ensures that the dimensional analysis uses the proper conversion factors, converting units by multiplying the given value by the factor of the original unit and dividing by the factor of the unit to convert to.

User Wesgarrison
by
7.3k points