The following table shows the approximate speed of sound in air, water and steel
ID: 3764639 • 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 (Java lang, Must print out the table from the problem and format using printf to 4 decimal places)
Explanation / Answer
Below is the java code. I guess the table to be displayed is the Medium-speed table so i am displaying that table at start. Hope i understood your question well
import java.util.*;
import java.io.*;
public class SoundSpeed{
public static void main(String[] args) {
try
{
int medium, dist;
int[] speeds={1100, 4900,16400};
String medium_desc[]={"Air", "Water","Steel"};
System.out.printf("Speed Table in different Mediums ");
System.out.printf("Medium Speed ");
for(int i=0; i<3;i++)
{
System.out.printf("%-10s - %10d ",medium_desc[i],speeds[i]);
}
Scanner scanner = new Scanner(System.in);
System.out.println("Please select the Medium: 1 for Air 2 for Water 3 for Steel");
medium = scanner.nextInt();
if(medium!=1 && medium!=2 && medium!=3 )
{
System.out.println("Invalid Medium ");
return;
}
System.out.println("You Selected "+medium_desc[medium-1]);
System.out.println("Please enter the distance");
dist = scanner.nextInt();
float time = (float)dist/speeds[medium-1];
System.out.printf("Time = %.4f units ",time);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.