The following table shows the approximate speed of sound in air, water and steel
ID: 3638560 • Letter: T
Question
The following table shows the approximate speed of sound in air, water and steel.Medium Speed
Air 1100 speed per second
Water 4900 speed per second
Steel 16,400 speed per second
Hello, I am trying to write a program that will ask the user to enter "air", "water", or "steel", and the distance that a sound wave will travel in the medium. The program should then display the amount of time it will take.
You can calculate the amount of time it takes sound to travel in air with the following formula:
Time = Distance / 1100
You can calculate the amount of time it takes sound to travel in water with the following formula:
Time = Distance / 4900
You can calculate the amount of time it takes sound to travel in steel with the following formula:
Time = Distance / 16400
Explanation / Answer
import java.util.Scanner; public class TimeCalc { private static double airsp = 1100; private static double watersp = 4900; private static double steelsp = 16400; public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter air, water or steel:"); String type = scan.nextLine(); type = type.toLowerCase().trim(); System.out.println("Enter distance:"); Double distance = scan.nextDouble(); System.out.println("The amount of time taken is:"); if (type.matches("air")) System.out.println(distance/airsp + " seconds"); else if (type.matches("water")) System.out.println(distance/watersp +" seconds"); else if (type.matches("steel")) System.out.println(distance/steelsp +" seconds"); else System.out.println("Invalid medium entered"); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.