Write a program that asks the user to enter a distance in meters. The program will then pre-sent the following menu of selections: Convert to kilometers Convert to inches Convert to feet Quit the program

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter7: Using Methods
Section: Chapter Questions
Problem 1CP
icon
Related questions
Question

Please provide Java source code for following project with proper comments.

Write a program that asks the user to enter a distance in meters. The program will then pre-sent the following menu of selections:

  1. Convert to kilometers

  2. Convert to inches

  3. Convert to feet

  4. Quit the program

The program will convert the distance to kilometers, inches, or feet, depending on the user’s selection. Here are the specific requirements:

  • Write a void method named showKilometers, which accepts the number of meters as an argument. The method should display the argument converted to kilometers. Convert the meters to kilometers using the following formula:

    kilometers = meters * 0.001
  • Write a void method named showInches, which accepts the number of meters as an argument. The method should display the argument converted to inches. Convert the meters to inches using the following formula:

    inches = meters * 39.37
  • Write a void method named showFeet, which accepts the number of meters as an argument. The method should display the argument converted to feet. Convert the meters to feet using the following formula:

    feet = meters * 3.281
  • Write a void method named menu that displays the menu of selections. This method should not accept any arguments.

  • The program should continue to display the menu until the user enters 4 to quit the program.

  • The program should not accept negative numbers for the distance in meters.

  • If the user selects an invalid choice from the menu, the program should display an error message.

Here is an example session with the program, using console input. The user’s input is shown in bold.

Enter a distance in meters: 500 [Enter]

1. Convert to kilometers

2. Convert to inches

3. Convert to feet

4. Quit the program Enter your choice: 1 [Enter]

500 meters is 0.5 kilometers.

1. Convert to kilometers

2. Convert to inches

3. Convert to feet

4. Quit the program

Enter your choice: 3 [Enter]

500 meters is 1640.5 feet.

1. Convert to kilometers

2. Convert to inches

3. Convert to feet

4. Quit the program

Enter your choice: 4 [Enter]

Bye!

Expert Solution
Java Program

import java.util.*;
import java.lang.*;
public class Main {
    
    static double showKilometers(double distance){
        return distance*0.001;
    }
    
    static double showInches(double distance){
        return distance*39.97;
    }
    
    static double showFeet(double distance){
        return distance*3.281;
    }
    
    static void menu(){
        System.out.println("1. Convert to kilometers");
        System.out.println("2. Convert to inches");
        System.out.println("3. Convert to feet");
        System.out.println("4. Quit the program");
        System.out.println("Enter your choice : ");
    }
    
 public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
  System.out.println("Enter a distance is meters: ");
  double num = scan.nextDouble();
  while(true){
      menu();
      int ch = scan.nextInt();
      switch(ch){
          case 1: System.out.println(num+" meters is " + showKilometers(num) + " kilometers.");
                  break;
          case 2: System.out.println(num+" meters is " + showInches(num) + " inches.");
                  break;
          case 3: System.out.println(num+" meters is " + showFeet(num) + " feet.");
                  break;
          case 4: System.out.println("Bye!");
                  System.exit(0);
          default: System.out.println("Enter a valid choice");
      }
  }
 }
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Graphical User Interface
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT