Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Harnessing the Power of Objects Abstraction at Play

Jack McClelland
Jack McClelland
1,356 Points

!isEmpty() was used to determine if dispenser was full - not being empty is not the same thing as being full though?

This logic doesn't make sense to me: "if the container isn't empty, then print out 'dispenser is full'" Because the dispenser could have 3 pez in it for example, which is not empty but also not full.

Also why print this out right after filling the dispenser, when it's clear that the dispenser is full?

3 Answers

Sean M
Sean M
7,344 Points

if you're referring to this code below:

  public boolean dispense() {
        boolean wasDispensed = false;
        if (!isEmpty()) {
            pezCount--;
            wasDispensed = true;
        }
        return wasDispensed;
    }

The !isEmpty is checking if it's not empty, not if it's full. If it's not empty, it will decrement the pezCount.

Hey Jack, before we check if the dispenser is not empty we fill the dispenser with the fill() method. I think Graig just illustrated that if we do the check again we will see that the dispenser has been filled and it's no longer empty.

Right, it's a domain logic error to infer from the condition alone that the dispenser is full. If we're including previous statements and the condition to justify the assertion, then the condition serves no purpose, since the assertion follows from previous statements alone. You could call it a poor example.