5.8. Conversion program - Java

preview_player
Показать описание
-----------------------------------------------------------------------------------------------------
Starting out with Java: From control structures through objects
Chapter 5
Programming Challenges

8. Conversion program

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

Gaddis, Tony (2015-05-29). Starting Out with Java: From Control Structures through Objects (Page 314 and 315). Pearson Education. Kindle Edition.

Thanks 😊
Рекомендации по теме
Комментарии
Автор

this would really help but the video is blurred out

williambarajas
Автор

If I type a negative number for my distance to begin, I receive the message “please enter a positive number for distance multiple times and I’m not sure why

TchnoBee
Автор

Love your videos, they really help me get started on figuring out good approaches for my assignments! THANK YOU!

For anyone wanting to know though, to copy the textbook assignment more exactly change all of your "println" to just "print" if you want your user input to appear on the same line as the text prompting your input, instead of showing user input on its own separate line.

In addition, you may opt to delete lines 27 & 28 and simply change line 58 to a basic print error message line. Lines 27 & 28 are unnecessary as the text placed on 28 is not actually attainable based on user input. Doing a single print message on 58 will achieve the same results.

As one final adjustment to display, for example, "500 meters" instead of "500.00 meters" go to lines 12, 18, and 24 and change %.2f meters to %.0f meters. This will make it so no decimals appear for meters, as shown in the assignment.

crystalpeterson
Автор

Thanks for your videos. It's really helping me a lot!

xychromosome
Автор

I don’t know if it’s just me but some of your videos are actually blurry and I can’t see them

duhvighost
Автор

When I execute my code, I'll input 1, 2, or 3, and it will just give me an infinite loop of the result until I press control c. Is it an issue with my while loop? Do you know what my issue is?

nicholasfrisch
Автор

How do you 0 value in decimal point .0 after 500 ?

turkmenmusics
Автор

Hi Atta,

I found your video well structured and helpful. I did run into one problem persistently, even after reviewing the video.

I tend to not get a result logged after I make an input for both number of meters and user choice.

I've requested to connect with you on LinkedIn if that is a better method of contact for both this video, as well as professional growth.

dastinkartoum-prater
Автор

your screen is not showing ooo
l need that solution tonight for my exam

banabasjannam
Автор

Another great lesson! I notice that the translation has several interesting errors, also it tends to obscure your code. I find your speaking voice very understandable. Did you add the translator because viewers requested it? If so, I think it would be worth checking it through before posting. My vote is to remove it because the code is what I want to see. You are so generous for offering this to us. Thanks so much.

patriciavotadini
Автор

hi, can you please assist in my Computer science homework? I need help with a UML diagram for this assignment. Please get back to me ASAP. Thank you!

vineypatel
Автор

Hi  Kakra - thank you so much for your fantastic tutorials, they have really helped me in learning Python. I have tried to write modularized versions of two Python programs you have written and wondered if you would accept a couple of questions and some code for me to share with you? Is there an email address I can use to email you. Thanks! Will (from Scotland)

williamtimothy
Автор

Are you available to help with some coding homework

shadybabyjo
welcome to shbcf.ru