The Speed Of Sound

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 8765 Views - Last Post: 13 February 2012 - 12:03 PM Rate Topic: -----

#1 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

The Speed Of Sound

Posted 30 September 2011 - 10:37 AM

Hello, I am trying to write a program that should display a menu allowing the user to select air, water, or steel. Once the user has made a selection, he or she should be asked to enter the distance a sound wave will travel in the selected medium. The program will then display the amount of time it will take. Check that the user had selected on of the the available choices from the menu.

Design a class that stores in a distance field, in feet, traveled by a sound wave. The class should have the appropriate accessor and mutator methods for this field. In addition, the class should have the following methods:

getSpeedInAir. This method should return the number of seconds it would take a sound wave to travel, in air, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in air is:

Time= distance/1100

getSpeedInWater. This method should return the number of seconds it would take a sound wave to travel, in water, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in water is:

Time= distance/4900

getSpeedInSteel.This method should return the number of seconds it would take a sound wave to travel, in steel, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in steel is:

Time= distance/16400

this is the code i wrote for the program

import java.util.Scanner;

/**
	Lab 4
	This is Lab Assignment 4 located on page 258 problem number 9.
	The following program will ask the user to enter "air", "water", or "steel", and the distance that a sound wave will in that perticular medium. 
	After that the program will display the amount of time it will take.
	This program is designed by "Divy Tolia"
*/

public class DTSpeedOfSound
	{// Begin class
	    public static void main(String[ ] args)
	      {//Begin Main Method
	      
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 + ".");
       }
       
    } //End Main Method
   
} // End class



but i am getting this error message

Enter air, water, or steel: Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at DTSpeedOfSound.main(DTSpeedOfSound.java:23)

Tool completed with exit code 1


Please help me i am not good at programming at all i am trying to get better

This post has been edited by CodeMasterNinja: 30 September 2011 - 10:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: The Speed Of Sound

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: The Speed Of Sound

Posted 30 September 2011 - 10:44 AM

Please remember to use :code:

It compiles for me and I copy pasted your code.
Was This Post Helpful? 0
  • +
  • -

#3 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: The Speed Of Sound

Posted 30 September 2011 - 10:45 AM

View PostFuzzyness, on 30 September 2011 - 10:44 AM, said:

Please remember to use :code:

It compiles for me and I copy pasted your code.


okay i did it that way now can you please help me
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: The Speed Of Sound

Posted 30 September 2011 - 10:48 AM

As I said it compiles for me with no errors. have you changed something before you posted?
Was This Post Helpful? 0
  • +
  • -

#5 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: The Speed Of Sound

Posted 30 September 2011 - 10:49 AM

View PostFuzzyness, on 30 September 2011 - 10:48 AM, said:

As I said it compiles for me with no errors. have you changed something before you posted?


no i did not change anything but it gives me the error when i try to run it i can compile it but i cant just run it

This post has been edited by CodeMasterNinja: 30 September 2011 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: The Speed Of Sound

Posted 30 September 2011 - 10:51 AM

I tested your code and it works fine to me. What program and version(for IDE and JDK) you are using? It seems the compiler do not see the nextLine() method in Scanner class in your computer!..... like it has been corrupted or so!
Was This Post Helpful? 0
  • +
  • -

#7 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: The Speed Of Sound

Posted 30 September 2011 - 10:54 AM

View Postsmohd, on 30 September 2011 - 10:51 AM, said:

I tested your code and it works fine to me. What program and version(for IDE and JDK) you are using? It seems the compiler do not see the nextLine() method in Scanner class in your computer!..... like it has been corrupted or so!


i am using textpad and the i am using jdk 1.7
Was This Post Helpful? 0
  • +
  • -

#8 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: The Speed Of Sound

Posted 30 September 2011 - 11:02 AM

is my code not correctly typed up or is there anything else i need to type or do something ? please help me
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: The Speed Of Sound

Posted 30 September 2011 - 11:03 AM

Can you test this for me please? Change this line input = keyboard.nextLine(); to input = "air"; I need to see if nextDouble is also unavailable which will prove my guess that the Scanner.java file is missing some of methods and may be corrupted by viruses..... then post back what it says.
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: The Speed Of Sound

Posted 30 September 2011 - 11:04 AM

Your code works fine, not that textpad wont work well,I suggest you consider getting a JDE, like JCreator or Eclipse. Just easier to manage them wordpad. Yes it compiles fine.

This post has been edited by Fuzzyness: 30 September 2011 - 11:04 AM

Was This Post Helpful? 0
  • +
  • -

#11 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: The Speed Of Sound

Posted 30 September 2011 - 11:17 AM

View PostFuzzyness, on 30 September 2011 - 11:04 AM, said:

Your code works fine, not that textpad wont work well,I suggest you consider getting a JDE, like JCreator or Eclipse. Just easier to manage them wordpad. Yes it compiles fine.


does it run correctly for you?
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: The Speed Of Sound

Posted 30 September 2011 - 11:21 AM

yes it runs fine for me, select any option air steel or water and give a distance and it will output a value. So it runs and compiles correctly.
Was This Post Helpful? 0
  • +
  • -

#13 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: The Speed Of Sound

Posted 03 October 2011 - 08:03 AM

How do i add units for the distance traveled and how do i add seconds for the time. heres the code.

import java.util.Scanner;

/**
	Lab 4
	This is Lab Assignment 4 located on page 258 problem number 9.
	The following program will ask the user to enter "air", "water", or "steel", and the distance that a sound wave will in that perticular medium.
	After that the program will display the amount of time it will take.
	This program is designed by "Divy Tolia"
*/

public class DTSpeedOfSound
	{// Begin class
	    public static void main(String[ ] args)
	      {//Begin main Method

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

//End the program.
	  System.exit(0);
       }

    } //End main Method

} // End class




Help me out
Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: The Speed Of Sound

Posted 03 October 2011 - 08:31 AM

Add the units you want when asking for distance
"Enter Distance in Feet: "
For seconds just add it onto your print statement:
"The total time traveled is " + time + " seconds."
Was This Post Helpful? 0
  • +
  • -

#15 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: The Speed Of Sound

Posted 03 October 2011 - 08:47 AM

well i am still not getting it my output is
Enter air, water, or steel: air
Enter distance: 200
The total time traveled is 0.18181818181818182.

and my new code is

import java.util.Scanner;

/**
	Lab 4
	This is Lab Assignment 4 located on page 258 problem number 9.
	The following program will ask the user to enter "air", "water", or "steel", and the distance that a sound wave will in that perticular medium.
	After that the program will display the amount of time it will take.
	This program is designed by "Divy Tolia"
*/

public class DTSpeedOfSound
	{// Begin class
	    public static void main(String[ ] args)
	      {//Begin main Method

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

}

else if (input.equals("steel"))

{

time = (distance / 16400);

System.out.println("The total time traveled is " + time + ".");

//End the program.
	  System.exit(0);
       }

    } //End main Method

} // End class


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3