I'm trying to work on a program challenge that takes an input of Air, Water, or Steel, and shows the speed of sound traveled in feet in Java.
My driver code looks like this
import java.util.Scanner;
public class driver
{
public static void main(String[] args)
{
String type;
int distance;
int time = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Input one of the following- Air, water, or steel.");
type = keyboard.nextLine();
System.out.print("Input distance in feet");
distance = keyboard.nextInt();
speedofsound sound = new speedofsound(distance);
if (type.equals("Air"))
time = sound.getSpeedInAir();
System.out.println(time);
if (type.equals("Water"))
time = sound.getSpeedInWater();
if (type.equals("Steel"))
time = sound.getSpeedInSteel();
System.out.println("The time it would take for" + type + "to travel" + distance + "feet is" + time);
}
}
and the Class looks like this
public class speedofsound {// class stating
private int distance;//distance integer
private int time;
public speedofsound (int d)
{
distance = d; // from driver pass over dist in ft,
}
public int getSpeedInAir()
{
return (distance / 1100);
}
public int getSpeedInWater()
{
time = distance / 4900;
return time;
}
public int getSpeedInSteel()
{
time = distance / 16400;
return time;
}
}
When I execute this in the console I get "Input one of the following- Air, water, or steel.Air Input distance in feet50 0 The time it would take forAirto travel50feet is0" No matter what I input, I keep getting 0. Whats up?
Aucun commentaire:
Enregistrer un commentaire