Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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.
```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?

2. ## 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.

3. ## The Following User Says Thank You to Norm For This Useful Post:

rosiems95 (April 3rd, 2014)

4. ## Re: Falling Distance

Focusing on
```      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...

5. ## The Following User Says Thank You to jashburn For This Useful Post:

rosiems95 (April 3rd, 2014)

6. ## 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

7. ## 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.

8. ## The Following User Says Thank You to GregBrannon For This Useful Post:

rosiems95 (April 3rd, 2014)

9. ## 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.

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?