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 12 of 12

Thread: Java Program: Falling Distance Problem

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Program: Falling Distance Problem

    Here's the explanation of the problem:

    PROBLEM:
    When an object is falling because of gravity, the following formula can be used to determine the distance the object falls in a specific time period:

    d = 1/2gt2

    The variables in the formula are as follows:
    -"d" is the distance in meters
    -"g" is 9.8
    -"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 that 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.
    OTHER STIPULATIONS (Made by Professor):
    -Instead of 9.8, make g = 9.80665
    -Make "g" a named constant, and declare it locally inside fallingDistance.
    And this is the sample Display Output we're suppose to have:

    OUTPUT:

    output2.png

    But, I got this..............

    output.png

    Lastly, here's my Java program that definitely needs some tweaking:
    import java.text.DecimalFormat;
     
    public class fallingDistance
    {
      public static void main(String[] args)
      {
        int fallingTime = 0;
        String name1 = "Time", name2 = "Distance";
        String name3 = "(seconds)", name4 = "(meters)";
     
        DecimalFormat num = new DecimalFormat("#,###.00");
     
        System.out.println("Falling Distance\n");
        System.out.printf("%s %15s\n", name1, name2);
        System.out.printf("%s %10s\n", name3, name4);
        System.out.println();
     
        for(int i = 1; i <= 10; i++)
        {
           fallingTime++;
           if(fallingTime > 1)
           {
             System.out.println(fallingTime + "           " + num.format(fallingDistance(i)));
           }
        }
      }
     
      public static double fallingDistance(double fallingTime)
      {
        double g = 9.80665, a = 0.5;
        double d = a * g * Math.pow(fallingTime,2);
        return d;
      }
    }

    I'll fix the spacing, making g constant, and all the little stuff. Just need help with the following:

    NEED TO:
    1) include the 1 and 4.90. It's not showing up for some reason, and don't know exactly how to put it in there.
    2) Get the decimal points lined up.
    PLEASE HELP ME! Thanks!


  2. #2
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: Java Program: Falling Distance Problem

    One line of your code in your for loop skips the first result and that is: fallingTime++; I'll suggest you use: System.out.printf() to format the distance output, if thats more easier.
    Who holds the KEY to all knowledge?

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program: Falling Distance Problem

    Quote Originally Posted by Mugambbo View Post
    One line of your code in your for loop skips the first result and that is: fallingTime++; I'll suggest you use: System.out.printf() to format the distance output, if thats more easier.
    All I had to do was include "=" to if(fallingTime > 1).

    Here's my updated Java code:

    public class fallingDistance
    {
      public static void main(String[] args)
      {
        int fallingTime = 0;
        double results;
        String name1 = "Time", name2 = "Distance";
        String name3 = "(seconds)", name4 = "(meters)";
        results = distance(fallingTime);
     
        System.out.println("Falling Distance\n");
        System.out.printf("%s %15s\n", name1, name2);
        System.out.printf("%s %10s\n", name3, name4);
        System.out.println();
     
        for(int i = 1; i <= 10; i++)
        {
           fallingTime++;
           if(fallingTime >= 1)
           {
             System.out.println(" " + fallingTime + "           " + results);
           }
        }
      }
     
      public static double distance(double fallingNum)
      {
        final double g = 9.80665;
        double a = 0.5, d;
        d = a * g * Math.pow(fallingNum,2);
        return d;
      }
    }

    Now, I'm getting this since I'm trying to use the Printf Method:

    javaDistanceFalling.png

    When I should be getting this as my output:

    output2.png

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program: Falling Distance Problem

    If you want the value of results to change every time it is printed, the code needs to assign it a new value.
    Otherwise its value won't change and the loop will print the same value every time.
    Move the assignment statement inside the loop so result is changed every time the loop goes around.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program: Falling Distance Problem

    Quote Originally Posted by Norm View Post
    If you want the value of results to change every time it is printed, the code needs to assign it a new value.
    Otherwise its value won't change and the loop will print the same value every time.
    Move the assignment statement inside the loop so result is changed every time the loop goes around.
    I was able to get the value of seconds to change (e.g. 1, 2, 3, 4, 5...10). How do I get the value to change for meters is the question I'm asking.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program: Falling Distance Problem

    Okay, I'm getting somewhere now. Here's what I have

    javaDistanceFalling.png

    And just need to get it to look like this:

    output2.png

    I know the whole printf method to align everything, but I can't seem to get this part of the code to work:
    for(int i = 1; i <= 10; i++)
        {
           fallingTime++;
           if(fallingTime >= 1)
           {
             System.out.println(fallingTime + "          " + distance(i));
             //  System.out.printf("%2.0f %20.2f\n", fallingTime, distance(i));
           }
        }

    The printf method you see is not letting me compile correctly. Any advice/input?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program: Falling Distance Problem

    not letting me compile correctly
    Please copy the full text of the compiler's error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program: Falling Distance Problem

    Quote Originally Posted by Norm View Post
    Please copy the full text of the compiler's error message and paste it here.
    compilerError.jpg

    It prints out the string variables (which is fine), but then it gives me these compiler errors. Mind you, that's when I use the second line of code (which I currently have as a comment).
    Last edited by javaBoi; May 4th, 2014 at 06:23 PM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program: Falling Distance Problem

    Can you copy and paste the message? Not post an image.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program: Falling Distance Problem

    Quote Originally Posted by Norm View Post
    Can you copy and paste the message? Not post an image.
    Falling Distance

    Time Distance
    (seconds) (meters)

    Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
    at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:4045)
    at java.util.Formatter$FormatSpecifier.printFloat(For matter.java:2761)
    at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2708)
    at java.util.Formatter.format(Formatter.java:2488)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at fallingDistance.main(fallingDistance.java:22)

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program: Falling Distance Problem

    java.util.IllegalFormatConversionException: f != java.lang.Integer
    That is not a compiler error. It happens when the code is executed.
    Did you look up the error message?
    Also read the API doc for the format control characters. The error message says that the letter f is NOT for integers.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2014
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program: Falling Distance Problem

    Quote Originally Posted by Norm View Post
    That is not a compiler error. It happens when the code is executed.
    Did you look up the error message?
    Also read the API doc for the format control characters. The error message says that the letter f is NOT for integers.
    Don't have time to look up for these errors, but--anyways--I actually figured it out. Thanks, I suppose.

Similar Threads

  1. Java Program - Distance Traveled (Formatting and Decimal Place)
    By javaBoi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 12th, 2014, 09:01 AM
  2. Falling Distance
    By rosiems95 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 3rd, 2014, 06:36 PM
  3. Distance Traveled problem help
    By rosiems95 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2014, 02:52 PM
  4. 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
  5. Distance traveled problem
    By Steph2752 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 25th, 2013, 10:43 AM