# Falling Distance

• April 3rd, 2014, 04:04 PM
rosiems95
Falling Distance
When an object is falling because of gravity, the following formula can be used to determine the distance the object falls in a specified time period:
d = 1/2 gt^2
The variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the amount of time, in seconds, that the object has been falling.
Write a method named FallingDistance that accepts an object's falling time (in seconds) as an argument. The method should return the distance, in meters, that the object has fallen during the time interval. Demonstrate the method by calling it in a loop that passes the values 1 through 10 as arguments, and displays the return value.
Code Java:

```import java.util.Scanner; import java.text.DecimalFormat;   public class FallingDistance { public static void main(String[] args) { DecimalFormat num = new DecimalFormat("#,###.00"); Scanner keyboard = new Scanner(System.in);   double fallingTime;   System.out.print("Enter the falling time (in seconds): "); fallingTime = keyboard.nextDouble();   for(int i = 1; i <=10; i++) System.out.println(num.format(fallingDistance(i)) + " meters"); } public static double fallingDistance(double fallingTime) { double g = 9.8, a = 0.5; double d = a * g * Math.pow(fallingTime,2); return d; } }```

My program runs, but no matter what falling time I enter, I get the same numbers. What am I doing wrong?
• April 3rd, 2014, 04:11 PM
Norm
Re: Falling Distance
Add some println() statements to the fallingDistance() method to see what the value of the fallingTime arg it receives is and the value of d that it returns.
The print out should help you see what the code is doing.
• April 3rd, 2014, 04:12 PM
jashburn
Re: Falling Distance
Focusing on
Code :

``` fallingTime = keyboard.nextDouble();   for(int i = 1; i <=10; i++) System.out.println(num.format(fallingDistance(i)) + " meters"); } public static double fallingDistance(double fallingTime) {```

fallingTime captures the user's input, and it would seem that the intention is to pass it to the fallingDistance method. However you are instead passing in the value of 'i' from the "for" statement...
• April 3rd, 2014, 04:36 PM
rosiems95
Re: Falling Distance
I tried both what Norm and jashburn suggested, and they both worked. The only thing is that with jashburn's suggestion it displays the output 10 times. Should it be doing that?
Also, does this part of the question mean it should only accept numbers 1-10: Demonstrate the method by calling it in a loop that passes the values 1 through 10 as arguments, and displays the return value
• April 3rd, 2014, 05:01 PM
GregBrannon
Re: Falling Distance
I believe jashburn was pointing out an error in your code rather than suggesting a possible solution. Reread that post and respond appropriately.
• April 3rd, 2014, 06:36 PM
jashburn
Re: Falling Distance
GregBrannon is exactly right. I was pointing out what is obvious in the code that explains the reason the program output is unaffected by your input. It looked rather strange to me at that time why you would want to loop through the call 10 times, but now that the assignment text has been included in the original post, I can see the reason for it.

Quote:

Write a method named FallingDistance that accepts an object's falling time (in seconds) as an argument. The method should return the distance, in meters, that the object has fallen during the time interval. Demonstrate the method by calling it in a loop that passes the values 1 through 10 as arguments, and displays the return value.
It is the demonstration part that calls for the loop. Notice that it doesn't ask for the ability to accept user input. It only asks for the display of the falling distance at falling times of 1 sec to 10 sec, inclusive. So, you should be able to answer your question: "does this part of the question mean it should only accept numbers 1-10." What do you think?