I need help fixing this code. (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;          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;             unit1 = command;          }          else if (command.equals("m"))          {             factor1 = 100;             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.\n");             getSecond = false;          }          if (getSecond)          {             System.out.print("To unit: ");             unit2 = in.next();             if (unit2.equals("in"))             {                factor2 = 2.54; // Convert from cm             }             else if(unit2. equals(" cm")){                factor2 = 1;          }                    else if(unit2.equals("m")){             factor2 = 100;          }          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 + "\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

I need help fixing this code. (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;
         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;
            unit1 = command;
         }
         else if (command.equals("m"))
         {
            factor1 = 100;
            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.\n");
            getSecond = false;
         }

         if (getSecond)
         {
            System.out.print("To unit: ");
            unit2 = in.next();
            if (unit2.equals("in"))
            {
               factor2 = 2.54; // Convert from cm
            }
            else if(unit2. equals(" cm")){
               factor2 = 1;
         }
         
         else if(unit2.equals("m")){
            factor2 = 100;
         }
         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 + "\n");
         }
      }
   }
}

The image shows a comparison between sets of expected and actual outputs in a unit conversion task.

### Comparison 1:

**Output differs. See highlights below.**

- **Input**: 
  ```
  cm in 10 quit
  ```

- **Your output**:
  ```
  From unit (in, cm, m, again, quit): cm
  To unit: 10.0 cm = 3.937007874015748 in
  From unit (in, cm, m, again, quit): quit
  ```

- **Expected output**:
  ```
  From unit (in, cm, m, again, quit): cm
  To unit: in↵
  10.0 cm = 3.937007874015748 in
  From unit (in, cm, m, again, quit): quit
  ```

### Comparison 2:

**Output differs. See highlights below.**

- **Input**:
  ```
  cm in 10 again cm in 20 quit
  ```

- **Your output**:
  ```
  From unit (in, cm, m, again, quit): cm
  To unit: 10.0 cm = 3.937007874015748 in
  From unit (in, cm, m, again, quit): again
  From unit (in, cm, m, again, quit): cm
  To unit: 20.0 cm = 7.874015748031496 in
  From unit (in, cm, m, again, quit): quit
  ```

- **Expected output**:
  *The expected output is not provided explicitly, but it can be inferred from the description of the task that it involves similar unit conversion processes.*

**Explanations for Discrepancies:**

In both comparisons, the discrepancy appears to arise from minor formatting differences in the output text, such as the inclusion of a return character in the expected output.

This analysis highlights the importance of precise formatting in automated text outputs, especially in educational or computational tools.
Transcribed Image Text:The image shows a comparison between sets of expected and actual outputs in a unit conversion task. ### Comparison 1: **Output differs. See highlights below.** - **Input**: ``` cm in 10 quit ``` - **Your output**: ``` From unit (in, cm, m, again, quit): cm To unit: 10.0 cm = 3.937007874015748 in From unit (in, cm, m, again, quit): quit ``` - **Expected output**: ``` From unit (in, cm, m, again, quit): cm To unit: in↵ 10.0 cm = 3.937007874015748 in From unit (in, cm, m, again, quit): quit ``` ### Comparison 2: **Output differs. See highlights below.** - **Input**: ``` cm in 10 again cm in 20 quit ``` - **Your output**: ``` From unit (in, cm, m, again, quit): cm To unit: 10.0 cm = 3.937007874015748 in From unit (in, cm, m, again, quit): again From unit (in, cm, m, again, quit): cm To unit: 20.0 cm = 7.874015748031496 in From unit (in, cm, m, again, quit): quit ``` - **Expected output**: *The expected output is not provided explicitly, but it can be inferred from the description of the task that it involves similar unit conversion processes.* **Explanations for Discrepancies:** In both comparisons, the discrepancy appears to arise from minor formatting differences in the output text, such as the inclusion of a return character in the expected output. This analysis highlights the importance of precise formatting in automated text outputs, especially in educational or computational tools.
## Analysis of Output Differences in Unit Conversion Program

In this educational resource, we are examining the differences in outputs for a unit conversion program. The program is designed to convert units such as inches (in), centimeters (cm), meters (m), and feet (ft). Below are two instances where the output differed from the expected result.

### Case 3: Output Comparison

**Input:**
```
cm ft cm m 100 quit
```

**Your Output:**
```
From unit (in, cm, m, again, quit): cm
To unit: Sorry, unknown unit.
From unit (in, cm, m, again, quit): cm
To unit: 100.0 cm = 1.0 m
From unit (in, cm, m, again, quit): quit
```

**Expected Output:**
```
From unit (in, cm, m, again, quit): cm
To unit: ft
Sorry, unknown unit.
From unit (in, cm, m, again, quit): cm
To unit: m
100.0 cm = 1.0 m
From unit (in, cm, m, again, quit): quit
```

**Explanation:**
The expected output shows that the program should first receive an attempt to convert from centimeters to feet, resulting in a 'Sorry, unknown unit' message since 'ft' was not recognized by the program initially. Then, the correct conversion from centimeters to meters occurs.

### Case 4: Output Comparison

**Input:**
```
ft m cm 10 quit
```

**Your Output:**
```
From unit (in, cm, m, again, quit): ft
To unit: Sorry, unknown unit.
```

### Key Observations:
- The input specifies a sequence of conversions ending in 'quit'.
- The program needs to handle unknown units gracefully and proceed to valid conversions.
- The expected output handles the conversion attempts and correctly identifies valid unit conversions.

These cases illustrate the importance of clear error handling and proper recognition of supported units in programming calculators or converters. Understanding and addressing these discrepancies ensures a program functions correctly and efficiently provides the intended service.
Transcribed Image Text:## Analysis of Output Differences in Unit Conversion Program In this educational resource, we are examining the differences in outputs for a unit conversion program. The program is designed to convert units such as inches (in), centimeters (cm), meters (m), and feet (ft). Below are two instances where the output differed from the expected result. ### Case 3: Output Comparison **Input:** ``` cm ft cm m 100 quit ``` **Your Output:** ``` From unit (in, cm, m, again, quit): cm To unit: Sorry, unknown unit. From unit (in, cm, m, again, quit): cm To unit: 100.0 cm = 1.0 m From unit (in, cm, m, again, quit): quit ``` **Expected Output:** ``` From unit (in, cm, m, again, quit): cm To unit: ft Sorry, unknown unit. From unit (in, cm, m, again, quit): cm To unit: m 100.0 cm = 1.0 m From unit (in, cm, m, again, quit): quit ``` **Explanation:** The expected output shows that the program should first receive an attempt to convert from centimeters to feet, resulting in a 'Sorry, unknown unit' message since 'ft' was not recognized by the program initially. Then, the correct conversion from centimeters to meters occurs. ### Case 4: Output Comparison **Input:** ``` ft m cm 10 quit ``` **Your Output:** ``` From unit (in, cm, m, again, quit): ft To unit: Sorry, unknown unit. ``` ### Key Observations: - The input specifies a sequence of conversions ending in 'quit'. - The program needs to handle unknown units gracefully and proceed to valid conversions. - The expected output handles the conversion attempts and correctly identifies valid unit conversions. These cases illustrate the importance of clear error handling and proper recognition of supported units in programming calculators or converters. Understanding and addressing these discrepancies ensures a program functions correctly and efficiently provides the intended service.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 7 images

Blurred answer
Knowledge Booster
Adjacency Matrix
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