Unit 7 Lesson 5, (4) check for understanding

Unit 7, Lesson 5, bubble 4

public class Product {
    private String name;
    private int quantity;

    public Product(String name, int quantity) {
        this.name = name;
        this.quantity = quantity;
    }

    public int getQuantity() {
        return quantity;
    }

    public void decreaseQuantity(int amount) {
        quantity -= amount;
    }
}

public class LimitedProduct extends Product {
    private int limit;

    public LimitedProduct(String name, int quantity, int limit) {
        super(name, quantity);
        this.limit = limit;
    }

    public void decreaseQuantity(int amount) {
        if (quantity - amount >= limit) {
            quantity -= amount;
        }
    }
}

Which of the following best describes the output of the code segment when a Product object and a LimitedProduct object are created with the same quantity and limit values?

The key states the answer is :
C. The LimitedProduct object will have a lower quantity than the Product object.

Wouldn’t this be the opposite? Wouldn’t the Product object continue to decrease, and the LimitedProduct object be higher due to the If statement in decreaseQuantity method that wouldn’t do anything if the condition wasn’t met.

All help is appreciated. Thank you.

@svinson - Thank you for posting to the forum. I traced the code and believe the answer is incorrect. I tried several inputs and the if statement would process only if all the values were zero or amount had a value of zero.
I will report this to the curriculum team.
Sylvia

2 Likes