Answer:
The program of this question can be given as:
Program:
//import pacakge for user input.
import java.util.Scanner;
//define class
public class SpeedofSound
{
public static void main(String a[]) //define main function
{
//define variable.
String medium;
double distance,time=0;
//creating Scanner class object for input from user.
Scanner s=new Scanner(System.in);
//print message.
System.out.printf("Enter medium(air,water or steel) : ");
medium=s.nextLine(); //taking input.
//print message.
System.out.printf("Enter the distance that the sound will travel : ");
distance=s.nextDouble(); //taking input.
switch(medium) //checking condtion between range.
{
case "air":
time=distance/1100; //apply formula
break;
case "water":
time=distance/4900; //apply formula
break;
case "steel":
time=distance/16400; //apply formula
break;
default:
System.out.printf("Sorry, you must enter air,water or steel"); //error for invalid input of medium
System.exit(0);
}
System.out.printf("It take "+time+" seconds"); //print final answer.
}
}
Output:
Enter medium(air,water or steel) : air
Enter the distance that the sound will travel : 200
It take 0.18181818181818182 seconds
Explanation:
In this program first, we import packages for user input. Then we declare the class in the class we declare all the variables and then we create the scanner class object. It is used for taking input from the user. Then we use the switch statement It is used for condition. It works between the ranges. In the switch statement, we apply all the formula that is given in the question. and at the last, we print the output using printf function in java.