Question

How would I use switch statements to write a program that will take an input of...

How would I use switch statements to write a program that will take an input of length (such as 23.4) and its unit by choosing from a menu. The menu should users which unit they want to convert by choosing off of a menu. The program then should output the converted length. The units the program supports are in, ft, yd, mi, m, km. Also have the program print out an error if either the input units or the conversion units are invalid. It should also repeat this process until a 0 is entered for length.

Hint: it may be easier if you take the input length and convert it to one unit (inches for example) and then convert that to the desired unit.

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

This program will provide you an idea of how you can achieve the said task.

I have not included the calculation part to convert from one unit to other.

public class UnitCon {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner sc=new Scanner(System.in);
       String units[]= {"in", "ft", "yd", "mi", "m", "km"};
       System.out.println("Units provided are : in, ft, yd, mi, m, km \nProcess repeats till you enter a zero in length");
do {

System.out.println("Enter Length or size");
double length = sc.nextDouble();
           System.out.println("Enter units from which you will convert");
           String from=sc.nextLine();
           System.out.println("Enter units to which you want to convert");
           String to=sc.nextLine();
           double output_length=0;
           switch(from) {
           case "in":
               output_length=convert_in(length,to);
               break;
           case "ft":
               output_length=convert_ft(length,to);
               break;
           case "yd":
               output_length=convert_yd(length,to);
               break;
           case "mi":
               output_length=convert_mi(length,to);
               break;
           case "m":
               output_length=convert_m(length,to);
               break;
           case "km":
               output_length=convert_km(length,to);
               break;
           default: System.out.println("Wrong choice");
           }
           //Print output_length
       }while(length!=0);
   }
  
   public static double convert_in(double length, String to) {
       switch(to) {
       case "ft":           //Your code to convert
           break;
       case "yd":           //Your code to convert
           break;
       case "mi":           //Your code to convert
           break;
       case "m":           //Your code to convert
           break;
       case "km":           //Your code to convert
           break;
       default: System.out.println("Wrong choice");
       }
      
   }
   public static double convert_ft(double length, String to) {
       switch(to) {
       case "in":           //Your code to convert
           break;
       case "yd":           //Your code to convert
           break;
       case "mi":           //Your code to convert
           break;
       case "m":           //Your code to convert
           break;
       case "km":           //Your code to convert
           break;
       default: System.out.println("Wrong choice");
       }
   }
   public static double convert_yd(...)
   public static double convert_mi(...)
   public static double convert_m(...)
   public static double convert_km(...)

}

Add a comment
Know the answer?
Add Answer to:
How would I use switch statements to write a program that will take an input of...
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
  • **Please write a C++ program **I'd appreciate it if you could take a screenshot/picture and put...

    **Please write a C++ program **I'd appreciate it if you could take a screenshot/picture and put it up Thanks 2. 50 Peintsl Write a program that calculates the solution/s ofa quadratic equation. The program should prompt the user for values a, b, and c can prints the solution to the screen. Write a unit conversion program using the conversion factors of the table shown below. Your program should ask the user from which unit he/she want to convert (o al...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

  • (1) (50%) Write a C program that takes as input a fully parenthesized, arithmetic expression of...

    (1) (50%) Write a C program that takes as input a fully parenthesized, arithmetic expression of binary operators +, -,*,/, and converts the expression into a binary expression tree. Your program should take input from the command line. The entire expression should be in a character string without any space in it An input string only includes floating numbers in the format of Y.YY, that is, one digit to the left of the decimal point and two digits to the...

  • In c++ Please. Now take your Project 4 and modify it.  You’re going to add another class...

    In c++ Please. Now take your Project 4 and modify it.  You’re going to add another class for getting the users name. Inherit it. Be sure to have a print function in the base class that you can use and redefine in the derived class. You’re going to also split the project into three files.  One (.h) file for the class definitions, both of them.  The class implementation file which has the member function definitions. And the main project file where your main...

  • Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an...

    Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an email address, and reports to the user whether or not the email address is valid. For the purposes of this assignment, we will consider a valid email address to be one that contains an @ symbol The program must allow the user to input as many email addresses as desired until the user enters "q" to quit. For each email entered, the program should...

  • Project 1, Program Design 1. Write a C program replace.c that asks the user to enter...

    Project 1, Program Design 1. Write a C program replace.c that asks the user to enter a three-digit integer and then replace each digit by the sum of that digit plus 6 modulus 10. If the integer entered is less than 100 or greater than 999, output an error message and abort the program. A sample input/output: Enter a three-digit number: 928 Output: 584 2. Write a C program convert.c that displays menus for converting length and calculates the result....

  • Write a complete C++ program that will: Declare a vector of integers Use a pointer to...

    Write a complete C++ program that will: Declare a vector of integers Use a pointer to dynamically allocate an array of 10 elements Ask the user how many values they would like to have in the data structures Read in the user’s response and make sure that it is greater than 20 (error loop) Generate the number of integers that the user asked for and store them into both data structures. The integer values should be unique unordered values (no...

  • Java programming How do i change this program to scan data from file and find the...

    Java programming How do i change this program to scan data from file and find the sum of the valid entry and count vaild and invalid entries The provided data file contains entries in the form ABCDE BB That is - a value ABCDE, followed by the base BB. Process this file, convert each to decimal (base 10) and determine the sum of the decimal (base 10) values. Reject any entry that is invalid. Report the sum of the values...

  • Write a C++ program to solve: The completed program should include the following: —Program input and...

    Write a C++ program to solve: The completed program should include the following: —Program input and output —Allow the user to input the starting, stopping and increment values for the insulation thickness and the air temperature. Also, at the end of the program, provide an opportunity for the user to use the program again without having to re-execute the program (ie. Ask them if they want to use the program again). —your program must find and output the value of...

  • I am writing a Python program to serve as a fitness tracker. I would love some...

    I am writing a Python program to serve as a fitness tracker. I would love some input as to how you would code this. I've included what I want this program to accomplish, what I will keep track of, and below all of that I have posted the assignment guidelines. Any suggestions on how to make this better/ more efficient would be welcome! I'm thinking of making a weekly tracker that takes input per day, and outputs at the end...

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