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. Create an application that keeps track of the items that a wizard can...

    Need help debugging. Create an application that keeps track of the items that a wizard can carry. console application which has no errors: import java.util.Scanner; public class Console {        private static Scanner sc = new Scanner(System.in);     public static String getString(String prompt) {         System.out.print(prompt);         String s = sc.nextLine();         return s;     }     public static int getInt(String prompt) {         int i = 0;         boolean isValid = false;         while (!isValid) {             System.out.print(prompt);...

  • 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();...

  • Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is...

    Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is the completed program. Pseudocode: DISPLAY Login information PROMPT for username/password            output "Enter Username"            input userName          output "Enter password: "         input password //If successful, display information pertaining to the specific user, as well as prompt for logout    IF User type 'quit'      Exit Program    VALIDATE User Credentials   IF NOT Validated      Check number of Invalid Attempts         IF user attempts equal three THEN            DISPLAY error message...

  • 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 to make it look cleaner but still give the...

    Can you help me rearrange my code to make it look cleaner 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, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...

  • 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....

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