Calling Instance Methods FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

The Hopper class is supplied.

class Hopper {
    private int grain;
    public Hopper() {
        grain = 12;
    }
    public void pour(int n) {
        grain += n;
    }
    public int amount() {
        return grain;
    }
}

Hopper bin = new Hopper();
bin.pour(3);
bin.pour(4);

(a) Identify the receiver of each pour call. (b) Give the final amount(). (c) Explain how each call updates state.

Question 2 of 5

The Gauge class is supplied.

class Gauge {
    private int psi;
    public Gauge(int start) {
        psi = start;
    }
    public void bump(int n) {
        psi += n;
    }
    public int reading() {
        return psi;
    }
}

Gauge tank = new Gauge(8);
Gauge also = tank;
tank.bump(5);

(a) Do tank and also refer to one object or two? (b) Give both reading() results after bump. (c) Explain why.

Question 3 of 5

The Ledger class is supplied.

class Ledger {
    private int cents;
    public Ledger(int start) {
        cents = start;
    }
    public void credit(int n) {
        cents += n;
    }
    public int balance() {
        return cents;
    }
}

Ledger drawer = new Ledger(30);
int saved = drawer.balance();
drawer.credit(8);

(a) Give saved after credit. (b) Give drawer.balance() after credit. (c) Explain why those integers can differ.

Question 4 of 5

The StampPad class is supplied.

class StampPad {
    private int ink;
    public StampPad(int start) {
        ink = start;
    }
    public void stamp() {
        ink -= 2;
    }
    public int remaining() {
        return ink;
    }
}

StampPad pad = null;
pad.stamp();

(a) State what happens when stamp() is called. (b) Identify the receiver. (c) Explain how to make a legal stamp() call using a constructed StampPad.

Question 5 of 5

The Hopper class is supplied.

class Hopper {
    private int grain;
    public Hopper() {
        grain = 12;
    }
    public void pour(int n) {
        grain += n;
    }
    public int amount() {
        return grain;
    }
}

(a) Explain why int x = bin.pour(1); is invalid if pour is void. (b) Show a valid pair of statements that pour 1 then print the amount. (c) Name the receiver in your statements.