# Calculate the speed of sound

Page 1 of 1

## 5 Replies - 30683 Views - Last Post: 01 March 2009 - 05:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=89903&amp;s=fc46f0d7d7dae4af4f6a146cd29ed9b4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 villalandron

Reputation: 0
• Posts: 153
• Joined: 24-September 08

# Calculate the speed of sound

Posted 01 March 2009 - 04:48 PM

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

The problem is that I am having a lot of problems creating this. I thought I would do it by utilizing switch commands but I am still a noob. I am getting some error messages. Here is the code:
```/* code

import java.util.Scanner;

/*
PROGRAM: TheSpeedofSound.java
Written by Julio C. Villa
The following table shows the approximate speed of sound in air, water, and steel:

Medium Speed
Air 1100 feet per second
Water 4900 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 / 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
*/

public class TheSpeedofSound
{
public static void main(String[ ] args)
{

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter air, water, or steel: ");
String air;
String water;
String steel;
air = keyboard.nextLine();
water = keyboard.nextLine();
steel = keyboard.nextLine();

System.out.print("Enter distance: ");
int distance;
distance = keyboard.nextInt();

switch (air || water || steel)
{
case air:
int time;
time = (distance / 1100)
System.out.println("The total time traveled is " + time + ".");

case water:
time = (distance / 4900)
System.out.println("The total time traveled is " + time + ".");

case steel:
time = (distance / 16400)
System.out.println("The total time traveled is " + time + ".");
}
}
}

```

Any help in this will be appreciated. Thank you!

Jay

This post has been edited by macosxnerd101: 06 April 2011 - 06:32 PM
Reason for edit:: A little late, but added code tags.

Is This A Good Question/Topic? 0

## Replies To: Calculate the speed of sound

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Calculate the speed of sound

Posted 01 March 2009 - 04:53 PM

dude this is your 38th post, by now you should have learned to post your code between code tags

### #3 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: Calculate the speed of sound

Posted 01 March 2009 - 04:54 PM

Switch will only work with ints or chars, better to use if with strings.

Quote

String air;
String water;
String steel;
air = keyboard.nextLine();
water = keyboard.nextLine();
steel = keyboard.nextLine();

Second, you are reading in an input 3 times. You should only read it in once and store it in a string, then in your ifs you can check like: if(input.equals("air")) { where input is the inputted value from the user.

### #4 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Calculate the speed of sound

Posted 01 March 2009 - 04:55 PM

btw you cant use a switch with strings, and also you're using a weird syntax for it, so you'd be better off with if else statements

### #5 villalandron

Reputation: 0
• Posts: 153
• Joined: 24-September 08

## Re: Calculate the speed of sound

Posted 01 March 2009 - 05:27 PM

No I get 7 more errors. Here is what I have.

```import java.util.Scanner;

/*
PROGRAM: TheSpeedofSound.java
Written by Julio C. Villa
The following table shows the approximate speed of sound in air, water, and steel:

Medium Speed
Air 1100 feet per second
Water 4900 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 / 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
*/

public class TheSpeedofSound
{
public static void main(String[ ] args)
{

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter air, water, or steel: ");
String air;
String water;
String steel;
air = keyboard.nextLine();
water = keyboard.nextLine();
steel = keyboard.nextLine();

System.out.print("Enter distance: ");
int distance;
distance = keyboard.nextInt();

if (input.equals("air"))
{
int time;
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 + ".");
}
}
}

```

### #6 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Calculate the speed of sound

Posted 01 March 2009 - 05:46 PM

```import java.util.Scanner;

/*
PROGRAM: TheSpeedofSound.java
Written by Julio C. Villa
The following table shows the approximate speed of sound in air, water, and steel:

Medium Speed
Air 1100 feet per second
Water 4900 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 / 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
*/

public class SpeedOfSound
{
public static void main(String[ ] args)
{

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter air, water, or steel: ");
String input;
input = keyboard.nextLine();

System.out.print("Enter distance: ");
double distance;
distance = keyboard.nextDouble();
double time;
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 + ".");
}
}
}

```

here, this should do it

This post has been edited by mostyfriedman: 01 March 2009 - 05:48 PM