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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Falling Distance

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by rosiems95; April 3rd, 2014 at 04:15 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    rosiems95 (April 3rd, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default 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. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #6
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default 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?

Similar Threads

  1. Falling Ball and Catching with Bar Game Problem Help
    By zille in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 21st, 2014, 06:41 AM
  2. Swing timer, falling object
    By ICEPower in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2013, 09:50 AM
  3. falling objects
    By viper_pranish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2013, 12:08 PM
  4. testing is player is jumping or not jumping. falling or not falling.
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2012, 06:07 PM
  5. Heads up! Satellite falling...
    By Sean4u in forum Totally Off Topic
    Replies: 18
    Last Post: September 23rd, 2011, 03:19 AM