Complete the following program to implement the user interface of the preceding exercise. For simplicity, only the units cm, m, and in are supported. Hint: The value of factor1 or factor2 should be the conversion factor from the selected unit to cm. Ex: If the selected unit is in, factor1 is 2.54 because 1 in = 2.54 cm. import java.util.Scanner; public class UnitConverter{   public static void main(String[] args)   {      Scanner in = new Scanner(System.in);      boolean done = false;      double factor1 = 0;      double factor2 = 0;      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"))         {            /* Your code goes here */         }         else if (command.equals("m"))         {            /* Your code goes here */         }         else if (command.equals("again"))         {            getSecond = false;         }         else if (command.equals("quit"))         {            done = true;            getSecond = false;         }         else         {            System.out.println("Sorry, unknown unit.\n");            getSecond = false;         }          if (getSecond) // If 'from' unit is valid, get 'to' unit         {            unit2 = in.next();            System.out.println("To unit: " + unit2);            if (unit2.equals("in"))            {               factor2 = 2.54; // Convert from cm            }            else if /* Your code goes here */            else if /* Your code goes here */            else /* Your code goes here */         }                  if (/* Your code goes here */)         {            double value = in.nextDouble();            double result = value * factor1 / factor2;             System.out.println(value + " " + unit1 + " = " + result + " " + unit2 + "\n");         }      }   }}

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Complete the following program to implement the user interface of the preceding exercise. For simplicity, only the units cmm, and in are supported.

Hint: The value of factor1 or factor2 should be the conversion factor from the selected unit to cm. Ex: If the selected unit is in, factor1 is 2.54 because 1 in = 2.54 cm.

import java.util.Scanner;

public class UnitConverter
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      boolean done = false;
      double factor1 = 0;
      double factor2 = 0;
      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"))
         {
            /* Your code goes here */
         }
         else if (command.equals("m"))
         {
            /* Your code goes here */
         }
         else if (command.equals("again"))
         {
            getSecond = false;
         }
         else if (command.equals("quit"))
         {
            done = true;
            getSecond = false;
         }
         else
         {
            System.out.println("Sorry, unknown unit.\n");
            getSecond = false;
         }

         if (getSecond) // If 'from' unit is valid, get 'to' unit
         {
            unit2 = in.next();
            System.out.println("To unit: " + unit2);
            if (unit2.equals("in"))
            {
               factor2 = 2.54; // Convert from cm
            }
            else if /* Your code goes here */
            else if /* Your code goes here */
            else /* Your code goes here */
         }
         
         if (/* Your code goes here */)
         {
            double value = in.nextDouble();
            double result = value * factor1 / factor2; 
            System.out.println(value + " " + unit1 + " = " + result + " " + unit2 + "\n");
         }
      }
   }
}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education