Question

... 5. Complete the following program to implement the user interface of the preceding exercise. For simplicity, only the uniNeed help solving this question. Dont even know where to start. Thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 inch to cm
               unit1 = command;
           }
           else if (command.equals("m")) {
               factor1 = 100; //convert meter to cm
               unit1 = command;
           }
           else if (command.equals("cm")) {
               factor1 = 1; //convert cm to cm
               unit1 = command;
           }
           else if (command.equals("again")) {
               getSecond = false;
           }
           else if (command.equals("quit")) {
               getSecond = false;
               done = true;
           }
           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 to inch from cm
               }
               else if (unit2.equals("m")) {
                   factor2 = 100; // Convert to m from cm
               }
               else if (unit2.equals("cm")) {
                   factor2 = 1; // Convert cm from cm
               }
           }
           if (!done) {
               double value = in.nextDouble();
               // We first convert the input value into cm depending upon factor1
               // That is, if unit1 was inch, we convert to cm by multiplying with 2.54
               // if it was meter, we convert to cm by multiplying by 100 etc
              
               // Once we have cm, we convert it into unit2, depending upon factor2
               // That is, if unit2 was inch, we convert to cm by dividing with 2.54
               // if it was meter, we convert to cm by dividing by 100 etc
              
               double result = (value * factor1)/factor2;
              
               System.out.println(value + " " + unit1 + " = " + result + " " + unit2);
           }
       }
   }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------

The spacing & indentation might not look proper, once I post this answer. Hence, attaching screenshot too for quick readability.

while(!done) { boolean getSecond System.out.print ( From unit (in, String command if (command.equals ( in ) ) { factor1

Add a comment
Know the answer?
Add Answer to:
Need help solving this question. Dont even know where to start. Thanks. ... 5. Complete the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • Complete the following Java class. In this class, inside the main() method, user first enters the...

    Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum(). import java.util.Scanner; import java.util.ArrayList; public class...

  • Original question: I need a program that simulates a flower pack with the traits listed below:...

    Original question: I need a program that simulates a flower pack with the traits listed below: - You must use a LinkedList for storage (not an Arrray list). - You must be able to display, add, remove, sort, filter, and analyze information from our flower pack. - The flower pack should hold flowers, weeds, fungus, and herbs. All should be a subclass of a plant class. - Each subclass shares certain qualities (ID, Name, and Color) – plant class -...

  • You will need to think about problem solving. There are several multi-step activities. Design, compile and...

    You will need to think about problem solving. There are several multi-step activities. Design, compile and run a single Java program, StringEx.java, to accomplish all of the following tasks. Add one part at a time and test before trying the next one. The program can just include a main method, or it is neater to split things into separate methods (all static void, with names like showLength, sentenceType, lastFirst1, lastFirst), and have main call all the ones you have written...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

  • COP3337 Assignment 10 This is an additional assignment from the chapter on the Java Collections Framework....

    COP3337 Assignment 10 This is an additional assignment from the chapter on the Java Collections Framework. Suppose you buy 100 shares of a stock at $12 per share, then another 100 at $10 per share, then you sell 150 shares at $15. You have to pay taxes on the gain, but exactly what is the gain? In the United States, the FIFO rule holds: You first sell all shares of the first batch for a profit of $300, then 50...

  • this needs to be in Java: use a comparator class which is passed into the sort...

    this needs to be in Java: use a comparator class which is passed into the sort method that is available on the ArrayList. import java.util.Scanner; public class Assign1{ public static void main(String[] args){ Scanner reader = new Scanner (System.in); MyDate todayDate = new MyDate(); int choice = 0; Library library = new Library(); while (choice != 6){ displayMainMenu(); if (reader.hasNextInt()){ choice = reader.nextInt(); switch(choice){ case 1: library.inputResource(reader, todayDate); break; case 2: System.out.println(library.resourcesOverDue(todayDate)); break; case 3: System.out.println(library.toString()); break; case 4: library.deleteResource(reader,...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT