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

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 in java to implement the user interface of the preceding exercise. For simplicity, only the units cm, m, and in are supported.

 

This is a Java program designed to convert units of measurement. Below is the detailed transcription of the code and an explanation of its functionality.

### UnitConverter.java

```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")) {
                // Add conversion logic here for cm
            } else if (command.equals("m")) {
                // Add conversion logic here for 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 {
                    // Add conversion logic here for other units
                }

                if (/* condition to check valid conversion */) { // Replace with actual condition
                    double value = in.nextDouble();
                    double result = value * factor1 / factor2;
                    System.out.println(value + " " + unit1 + " = " + result + " " + unit2);
                }
            }
        }
    }
}
```

### Explanation:

1. **Imports and Class Declaration**
   ```java
   import java.util.Scanner;

   public class UnitConverter {
   ```
   - The program begins by importing the `Scanner` class for reading user inputs.
   - The `UnitConverter` class is declared as public.

2. **Main Method**
   ```java
   public static void main(String[] args) {
   ```
   - The `main` method is the entry point of the program.

3
Transcribed Image Text:This is a Java program designed to convert units of measurement. Below is the detailed transcription of the code and an explanation of its functionality. ### UnitConverter.java ```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")) { // Add conversion logic here for cm } else if (command.equals("m")) { // Add conversion logic here for 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 { // Add conversion logic here for other units } if (/* condition to check valid conversion */) { // Replace with actual condition double value = in.nextDouble(); double result = value * factor1 / factor2; System.out.println(value + " " + unit1 + " = " + result + " " + unit2); } } } } } ``` ### Explanation: 1. **Imports and Class Declaration** ```java import java.util.Scanner; public class UnitConverter { ``` - The program begins by importing the `Scanner` class for reading user inputs. - The `UnitConverter` class is declared as public. 2. **Main Method** ```java public static void main(String[] args) { ``` - The `main` method is the entry point of the program. 3
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Top down approach design
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
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