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

New Topic/Question
Reply



MultiQuote





|