Question

In: Computer Science

The following table shows the approximate speed of sound in air,water, and steel:MediumSpeed...

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 + ".");
}
}
  
}

Solutions

Expert Solution

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");
       }
   }

}


Related Solutions

The following table shows the approximate speed of sound in air, water, and steel:
The following table shows the approximate speed of sound in air, water, and steel:Medium SpeedAir 1,100 feet per secondWater 4,900 feet per secondSteel 16,400 feet per secondIn Java, 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 oftime it will take.You can calculate the amount of time it takes sound to travel in air with the following...
The speed of sound in water is _____ than the speed of sound in air because _____.
The speed of sound in water is _____ than the speed of sound in air because _____. (a) Faster; water is much harder to compress (b) Faster; water is much more dense (c) Slower; water is much easier to compress (d) Slower; water is much less dense (e) Equal; the two fluids are at the same pressure
The following table shows the approximate number of males of Hispanic origin employed in the United...
The following table shows the approximate number of males of Hispanic origin employed in the United States in a certain year, broken down by age group. Age 15-24.9 25-54.9 55-64.9 Employment (thousands) 29,000 17,000 3,700 (a) Use the rounded midpoints of the given measurement classes to compute the expected value and the standard deviation of the age X of a male Hispanic worker in the United States. (Round all probabilities and intermediate calculations to two decimal places.) expected value yrs...
In this experiment, we have claimed to measure the speed of sound in air. To do...
In this experiment, we have claimed to measure the speed of sound in air. To do this, we have used an audible frequency sweep. Why can we claim that the speed of sound will be the same over the bandwidth of the sweep? Is this assertion justified based on your results from experiment 2? Hint: Consider how sound (longitudinal pressure waves) propagate in a medium. (5 pts)
The following table shows the approximate numbers of school goers in the United States (residents who...
The following table shows the approximate numbers of school goers in the United States (residents who attended some educational institution) in 1998, broken down by age group. Age 3−6.9 7−12.9 13−16.9 17−22.9 23−26.9 27−42.9 Population (millions) 8 22 15 13 3 5 Use the rounded midpoints of the given measurement classes to compute the probability P distribution of the age X of a school goer. (Round probabilities to four decimal places.) Age 5 10 15 20 25 35 P(X =...
Ultrasound probe emitting sound with wave length of 70 mm in air (sound speed 343 m/s)...
Ultrasound probe emitting sound with wave length of 70 mm in air (sound speed 343 m/s) is used for probing a muscle tissue (sound speed 1500 m/s). What are the smallest features in air and the muscle tissue that can be seen with this device?
The following table shows the approximate value of exports and imports for the United States from 1983 through 1987.
Imports, exports, and the trade balance The following table shows the approximate value of exports and imports for the United States from 1983 through 1987. Complete the table by calculating the surplus or deficit both in absolute (dollar) terms and as a percentage of GOP. If necessary, round your answers to the nearest hundredth. Between 1984 and 1985, the _______  _______ In dollar terms and _______ as a percentage of GOR.
The following table shows the approximate value of exports and imports for the United States from 1983 through 1987.
Imports, exports, and the trade balance The following table shows the approximate value of exports and imports for the United States from 1983 through 1987. Complete the table by calculating the surplus or deficit both in absolute (dollar) terms and as a percentage of GDP. If necessary, round your answers to the nearest hundredth. Source: "Income, Expenditures, Poverty, & Wealth: Gross Domestic Product (GDP)," United States Census Bureau, United States Department of Commerce, last modified September 2011, accessed June 10, 2013, https://www.census.gov/library/publications/2011/compendia/statab/131ed/income-expenditures-poverty-wealth.html. Between 1984...
a) Kinetic theory. The speed of sound in the air is 330 m/s under standard conditions...
a) Kinetic theory. The speed of sound in the air is 330 m/s under standard conditions of temperature and pressure (273 K and 1 atm). Since the size of a molecule is much smaller than the average distance between the molecules, this number provides an estimate of the order of magnitude of the molecular media velocity. Consider a cubic meter of air and concentrate it on a N2 molecule that travels in the x direction at 330 m / s....
Calculate the wavelengths in air at 20°C for sounds in the maximum range of human hearing. The speed of sound in air is 343 m/s.
Calculate the wavelengths in air at 20°C for sounds in the maximum range of human hearing. The speed of sound in air is 343 m/s.Part A Calculate the wavelength of a 20-Hz wave.Part B Calculate the wavelength of a 20,000-Hz wave. Part C What is the wavelength of a 19-MHz ultrasonic wave? 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT