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();
}
}
}
[tex]The speed of sound depends on the material the sound is passing through. Below is the approximate sp[/tex] [tex]The speed of sound depends on the material the sound is passing through. Below is the approximate sp[/tex]
The volume of a sound depends on the amplitude of the sound waves. The amplitude of a sound wave determines its volume (a larger amplitude means a louder sound, a smaller amplitude means a softer sound). The vibration of a source sets the amplitude of a wave. More energetic vibration corresponds to a larger amplitude. The molecules move more vigorously.
The volume of a sound is also determined by the sensitivity of the ear (human ear is more sensitive to some frequencies than to others). It depends on both the amplitude of a sound wave and whether its frequency lies in a region where the ear is more/less sensitive.
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.
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();
}
}
}
[tex]The speed of sound depends on the material the sound is passing through. Below is the approximate sp[/tex]
[tex]The speed of sound depends on the material the sound is passing through. Below is the approximate sp[/tex]
The volume of a sound depends on the amplitude of the sound waves. The amplitude of a sound wave determines its volume (a larger amplitude means a louder sound, a smaller amplitude means a softer sound). The vibration of a source sets the amplitude of a wave. More energetic vibration corresponds to a larger amplitude. The molecules move more vigorously.
The volume of a sound is also determined by the sensitivity of the ear (human ear is more sensitive to some frequencies than to others). It depends on both the amplitude of a sound wave and whether its frequency lies in a region where the ear is more/less sensitive.
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.