Purpose of the Program

Purpose of Program Using void and value-returning methods.

Exercise

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

1. Convert to kilometers
2. Concert to inches
3. Convert to feet
4. Quit the program

The program will convert the distance to kilometers, inches, or feet, depending on the users 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. Concert 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. Concert 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. Concert the meters to feet using the following formula: feet = meters * 3.281
  • Write a void method named menu, that displays the menu of selection. 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 messages.

Example Input and Output

Example input and output Here is an example session with the program, using console input. The users input is show in red and bold.

Enter a distance in meters: 500
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

Enter your choice: 1
500.0 meters is 0.5 kilometers.

1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

Enter your choice: 2
500.0 meters is 19685.0 inches.

1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

Enter your choice: 5
Invalid selection.

Enter your choice: 4
Bye!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.