In: Computer Science
The following table shows the approximate speed of sound in air, water, and steel:
Medium Speed
Air 1,100 feet per second
Water 4,900 feet per second
Steel 16,400 feet per second
In Java, write a program that asks 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 / 1,100
You can calculate the amount of time it takes sound to travel in water with the following formula:
Time = Distance / 4,900
You can calculate the amount of time it takes sound to travel in steel with the following formula:
Time = Distance / 16,400
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] s)
{
String medium;
double distance;
double time;
Scanner choice = new Scanner(System.in);
System.out.println("Enter one of the following: air, water, or
steel: ");
medium = choice.next(); // reading input i.e. air, water or
steel
//check for air water and steel
if (medium.equalsIgnoreCase("air") ||
medium.equalsIgnoreCase("water") ||
medium.equalsIgnoreCase("steel")){
System.out.println("Enter the distance the sound wave will travel: ");
distance = choice.nextDouble(); // read distance value if it is air water or steel
switch (medium)
{
//if medium is air
case "air":
time = distance/1100;
System.out.print("It will take " + time + " seconds.");
break;
//if medium is water
case "water":
time = distance/4900;
System.out.print("It will take " + time + " seconds.");
break;
//if medium is steel
case "steel":
time = distance/16400;
System.out.print("It will take " + time + " seconds.");
break;
}
}
else{
System.out.print("Sorry, you must enter air, water, or steel.");
}
}
}