This is based on a Programming Challenge in your textbook. This table shows the approximate speed of sound in air, water, and steel.
Air | 1,100 feet per second |
Water | 4,900 feet per second |
Steel | 16,400 feet per second |
Write a program that does the following:
- Display a menu allowing the user to select air, water, or steel. Prompt the user and allow them to enter a choice. User entry should be a character: ‘A’ for air, ‘W’ for water, and ‘S’ for steel. Allow both uppercase and lowercase as valid entries
- If the user enters an invalid choice, print an appropriate error message and end the program.
- For a valid choice, now ask the user to enter the distance that a sound wave will travel through the selected medium. (Use type double for this entry).
- If the selected distance is negative, this is an invalid choice, so print an error message and end the program.
- For a valid distance, compute and display the amount of time it will take for the sound wave to travel the given distance, rounded to 4 decimal places.
Sample Runs
(user input is underlined, to distinguish it from output)
Sample run 1
Welcome to Sound Wave CalculatorSelect the medium the sound wave will travel through. A Air W Water S Steel> AHow far will the sound wave travel (in feet)? 3500 The sound wave will travel 3.1818 secondsGoodbye
Sample run 2
Welcome to Sound Wave CalculatorSelect the medium the sound wave will travel through. A Air W Water S Steel> wHow far will the sound wave travel (in feet)? 3500The sound wave will travel 0.7143 secondsGoodbye
Sample run 3
Welcome to Sound Wave CalculatorSelect the medium the sound wave will travel through. A Air W Water S Steel> sHow far will the sound wave travel (in feet)? 98765The sound wave will travel 6.0223 secondsGoodbye
Sample run 4
Welcome to Sound Wave CalculatorSelect the medium the sound wave will travel through. A Air W Water S Steel> zIllegal entry. Aborting program.Goodbye
Sample run 5
Welcome to Sound Wave CalculatorSelect the medium the sound wave will travel through. A Air W Water S Steel> SHow far will the sound wave travel (in feet)? -10Distance cannot be negative. Aborting program.Goodbye