Stack frames and objects (like the Idea Lab activity): Correct Solution


Given the code below, this method call:

Uid.putgar();

...will eventually reach the point marked HERE. Draw a diagram of the stack and the heap at that point.

(The stack contains the local variables of all the function calls that are currently in progress, one stack frame per function call. The heap contains all of the objects that currently exist.)

In your diagram:

The code:

public class Uid {
    private int utsad;
    private Phuu loic;

    Uid(int utsad) {
        this.utsad = utsad;
    }

    public void setLoic(Phuu loic) {
        this.loic = loic;
    }

    public static void sioZantro(int pa, Uid eiss, Phuu tre) {
        tre.setVi(eiss);
        HERE;
        new Uid(786).pegn(39, tre, pa);
    }

    public void aioButis(Phuu u, int e) {
        this.lioEtir(this);
        u.setRass(this);
        Uid.sioZantro(e, this, u);
    }

    public void lioEtir(Uid se) {
    }

    public static void putgar() {
        Phuu ro = new Phuu(248);
        int cin = 40;
        int li = 33;
        Phuu rai = new Phuu(446);
        Phuu.ephess(16);
    }

    public void pegn(int o, Phuu jabo, int ini) {
    }

    public void piosem(Phuu i, int si, int ja) {
    }
}
public class Phuu {
    private int juff;
    private Uid rass;
    private Uid vi;

    Phuu(int juff) {
        this.juff = juff;
    }

    public void setRass(Uid rass) {
        this.rass = rass;
    }

    public void setVi(Uid vi) {
        this.vi = vi;
    }

    public static void ephess(int whil) {
        new Uid(424).piosem(new Phuu(159), whil, whil);
        new Uid(237).aioButis(new Phuu(419), whil);
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: