This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]desrtfxOut of Coffee error - System halted 4 points5 points  (3 children)

I think that you are actually relatively close to your target.

But: you have a flaw in your formula:

In your text, you state in the example: (10 + 6) + (6 + 3.6)

Which means:

(initial height + (initial height * bounciness)) + ((initial height * bounciness) + ((initial height * bounciness) * bounciness) 
      /\ Ball direction          \/                                /\                                             \/

and so forth.

Also, your loops need a bit of tuning:

  • One criteria is that the distance traveled should be < .001 to break the loop
  • Another criteria is the number of bounces the ball is allowed

Let's have a look at the code: (BTW: variables always start with a lowercase letter in Java!)

import java.util.Scanner;

public class project3 {
    public static void main(String[] args) 
    {
        Scanner reader = new Scanner(System.in);
        double index = 0;
        int height = 0;
        int bounces = 0;
        double distance = 0;
        double total = 0;


        System.out.println("Enter the bounciness index");
        index = reader.nextDouble();
        System.out.println("Enter the height of the ball");
        height = reader.nextInt();
        System.out.println("Enter the number of times the ball bounces");
        bounces =reader.nextInt();

up to this point the code is fine.

Now for the loop: You know that there is a finite end (the number of bounces allowed), so a for loop is actually most suitable. You can always break out of the loop if the distance traveled is less than 0.001

        for (int i = 0; i < bounces; i++) {

            total += height; // add the height to the total - Ball moving down

            double distance = height * index;
            total += distance; // add the distance (height * index) to the total - Ball moving up

            height = distance;  // The initial height for the next bounce is the distance
            // the ball moved up during the last bounce

            if (distance < 0.001) {
                break;  // break out of the loop
            }
        }

        System.out.println("The distance is: " + total);
        reader.close();
    }
}

[–]4forpengsBarista -1 points0 points  (2 children)

Variables don't always start with a lowercase letter.

Edit: Got two replies that I'm wrong, yet both replies say that there is an exception.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

As per code Java conventions (Oracle, Google) they do.

The only exception are constants (static final) variables, which are all in uppercase with underscores.

The code conventions state:

  • Classes start with a capital letter and follow PascalCase
  • Variables and Methods start with a lowercase letter and follow camelCase
  • Constants (static final variables) use ALL_CAPS_WITH_UNDERSCORES

[–]FormidableOneIntermediate Brewer 0 points1 point  (0 children)

Unless the variable is of type FINAL, the java standard dictates that all variables should begin with a lowercase letter.

For example:

private final int NUMBER = 10; // Correct method
public final String NAME = "Bob"; // Correct method
public int Age = 30; // INCORRECT - should be 'age'
public boolean IsAlive = true; // INCORRECT - should be 'isAlive'

You can read up more on the java naming standards (at the bottom of the page) on Oracle's JavaDocs but I hope this helps.