Answer:
The Java Program for the given problem is as below. Directly copy the code and run it on your machine.
Explanation:
Refer the Screenshots attached for the output.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TheSpeedOfSound {
public static void main(String[] s)
{
String medium;
double distance;
double time;
try{
BufferedReader choice = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter one of the following: air, water, or steel: ");
medium = choice.readLine(); // 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 = Double.parseDouble(choice.readLine()); // 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.");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}