1

I have created this program where an array of bubble objects will be created in the constructor, and then the bubbles will float accross the canvas and once the bubbles touch each other they will disappear and reveal the words "POP!". My method called noneLeft() should return true if all the bubbles have been popped and then another method called redisplayAll() will be called and the bubbles will reset and redisplay again. However, I am not sure what to write for my if statement to return true once the last bubble has been popped. How do I write down if the last bubble in the array has been popped, then return true. Would I have to use bubbles.length?

public Mover(double width, double height, int numberOfBubbles) {
    canvasWidth = width;
    canvasHeight = height;
    bubbles = new Bubble[numberOfBubbles];

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

        bubbles[i] = new Bubble();
        bubbles[i].showBubble(width, height);

    }

    count = 0;
}



public boolean noneLeft() {


   if (bubbles[].isPopped() == true){

       return true;

   }

   return false;    

}
1
  • 2
    return Arrays.stream(bubbles).allMatch(x -> x.isPopped()); Commented Nov 11, 2017 at 17:33

1 Answer 1

2

The code should be

public boolean noneLeft() {
    for (Bubble b : bubbles) {
        if (!b.isPopped()) {
            return false;
        }
    }
    return true;
}

Iterate over the bubbles and as soon as you find one that has not popped return false since at least one bubble is left.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.