5 Replies - 15393 Views - Last Post: 01 March 2009 - 05:46 PM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

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

Calculate the speed of sound

Post icon  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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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 + ".");
				}
	}
}




Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1