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

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

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