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 |
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,100Time=Distance/1,100
You can calculate the amount of time it takes sound to travel in water with the following formula:
Time=Distance/4,900Time=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
This is the question Below is what I wrote but I need the program to have these certain things
The user can enter upper or lower case letters
the user needs to be told if they enter an incorrect medium
the user needs to be told if they enter an amount of time that is less than 0
display time in seconds to 2 decimal places
package speedofsound;
import java.util.Scanner;
public class SpeedofSound {
public static void main(String[] args)
{
//Variables
double distance;
double time;
String input;
Scanner keyboard = new Scanner (System.in);
//Ask user for air, water, or steel
System.out.print("Enter air, water, or steel: ");
input = keyboard.nextLine();
//Ask user for distance
System.out.print("Enter distance: ");
distance = keyboard.nextDouble();
//while (distance >0 )
//System.out.println("Please enter a number greater then 0");
//Add if statement
if (input.equals("air"))
{ time = (distance / 1100);
System.out.println("The total time traveled is " + time +
".");
}
else if (input.equals("water"))
{ time = (distance / 4900);
System.out.println("The total time traveled is " + time +
".");
}
else if (input.equals("steel"))
{ time = (distance / 16400);
System.out.println("The total time traveled is " + time +
".");
}
}
}
import java.util.Scanner;
public class SpeedofSound {
public static void main(String[] args) {
//Variables
double distance;
double time;
String input;
Scanner keyboard = new
Scanner(System.in);
//Ask user for air, water, or steel
System.out.print("Enter air, water,
or steel: ");
input = keyboard.nextLine();
//Ask user for distance
System.out.print("Enter distance:
");
distance =
keyboard.nextDouble();
//while (distance >0 )
// adding validation if user gives
distance as <0
while (distance < 0) {
System.out.println("Please enter a number greater then 0");
System.out.print("Enter distance: ");
distance =
keyboard.nextDouble();
}
//Add if statement with equalsIgnoreCase
if (input.equalsIgnoreCase("air"))
{
time = (distance
/ 1100);
System.out.printf("The total time traveled is %.2f.", time);
} else if
(input.equalsIgnoreCase("water")) {
time = (distance
/ 4900);
System.out.printf("The total time traveled is %.2f.", time);
} else if
(input.equalsIgnoreCase("steel")) {
time = (distance
/ 16400);
System.out.printf("The total time traveled is %.2f.", time);
} else {
System.out.println("Invalid medium");
}
}
}