Billy Scranner Billy Scranner - 4 years ago 70
Java Question

Using additional methods correctly

I'm having trouble with additional "public static double" methods. The results I get are; for the first output, 1-10 in ascending order. The two other outputs I get are ten 0.0 results. My issue is not formatting, nor do I have any error messages.

Here is what I currently have:

public static void main(String[] args) {
System.out.println("Sec\tMeters\tFeet ");

for (int t=1; t<=10; t+=1) {
System.out.print(t);

System.out.print("\t" + distanceFell(t));

double conv = ((1/2) * 9.8 * ((t)*(t)));
System.out.println("\t" + metersToFeet(conv));
}

}

public static double distanceFell(int t)
{
double conv = ((1/2) * 9.8 * ((t)*(t)));
return (conv);
}

public static double metersToFeet (double conv)
{
double convFeet = (conv / 0.3048);
return (convFeet);
}
}

Answer Source

Because you are starting your multiplication with 1/2, this gives a result of 0. Integer division keeps the whole segment of the division and throws out the remainder.

Thus you get 0 * 9.8 * t * t, which is, as you've noticed, 0.0.

Rather than 1/2, you should try 0.5. This will give you the floating-point maths that you're after.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download