Anatomy of a Class FRQ Practice

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

Question 1 of 5

A seed pot stores planting depth in centimeters.

(a) Write a public class SeedPot with a private int depth, a public constructor SeedPot(int d) that stores d in depth, and a public method getDepth that returns depth. (b) Explain why the class header uses public. (c) Explain why depth is designated private.

Question 2 of 5

Complete the missing soilMoisture values. Then explain why the two Planter objects do not stay equal after water is called on only one of them.

public class Planter {
    private int soilMoisture;
    public Planter(int start) {
        soilMoisture = start;
    }
    public void water(int extra) {
        soilMoisture += extra;
    }
    public int getSoilMoisture() {
        return soilMoisture;
    }
}
Step Statement left soilMoisture right soilMoisture
1 Planter left = new Planter(5); 5
2 Planter right = new Planter(5); 5
3 left.water(2);
4 right.water(0);

Question 3 of 5

A client has SeedPot pot = new SeedPot(8); and the class below.

public class SeedPot {
    private int depth;
    public SeedPot(int d) {
        depth = d;
    }
    public int getDepth() {
        return depth;
    }
    private void flatten() {
        depth = 0;
    }
    public void empty() {
        flatten();
    }
}

(a) State whether each of these four statements compiles: pot.depth = 3; int d = pot.getDepth(); pot.flatten(); pot.empty(); (b) There is no public setter, so pot.depth = 3 cannot be rewritten. Replace pot.flatten() with the public call that still zeros the pot. (c) Explain why flatten cannot be called from the client even though empty can.

Question 4 of 5

A hose should increase drips by 2 when the public method soak is called. The increment itself must not be callable from client code.

public class Hose {
    private int drips;
    public Hose() {
        drips = 1;
    }
    private void tick() {
        drips += 2;
    }
    public void soak() {
        tick();
    }
    public int getDrips() {
        return drips;
    }
}

(a) Hand-trace Hose h = new Hose(); h.soak(); and state the value returned by h.getDrips(). (b) Identify which methods a client may call. (c) Explain why tick is private and soak is public.

Question 5 of 5

A student writes Clip with a public field so a client can change the mark. The specification did not ask for a public attribute.

public class Clip {
    public String mark;
    public Clip() {
        mark = "ok";
    }
}

(a) Rewrite Clip so mark is encapsulated, adding any public methods the client needs to read the mark. (b) Give one reason the original public field violates usual class anatomy. (c) State whether the Clip class header should remain public, and why.