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


Given the code below, this method call:

Twer.huscim();

...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 Twer {
    private int ruo;
    private Twer eni;
    private Nuro oism;
    private Nuro tida;

    Twer(int ruo) {
        this.ruo = ruo;
    }

    public void setEni(Twer eni) {
        this.eni = eni;
    }

    public void setOism(Nuro oism) {
        this.oism = oism;
    }

    public void setTida(Nuro tida) {
        this.tida = tida;
    }

    public static void huscim() {
        int amo = 95;
        new Twer(613).emaPous(new Nuro(159), amo);
    }

    public static void stri() {
        int whu = 16;
        int scri = 20;
        int me = 34;
        HERE;
    }

    public void emaPous(Nuro ofti, int tiss) {
        int psu = 26;
        Nuro.semor(this, tiss);
        ofti.setHi(this);
        new Nuro(980).ieme(this);
    }

    public static void paurm(Twer toos) {
        int cac = 70;
        Twer ceo = new Twer(665);
        Nuro ot = new Nuro(148);
        ot.treEpaph(toos, 14, cac);
        ot.setHi(toos);
        Twer.stri();
        ceo.raucil();
    }

    public void raucil() {
        int imed = 36;
        int ro = 54;
        int hido = 79;
        int biu = 63;
    }

    public void bugrap(Twer wock, Nuro sli) {
        int to = 77;
    }
}
public class Nuro {
    private int whaen;
    private Twer hi;

    Nuro(int whaen) {
        this.whaen = whaen;
    }

    public void setHi(Twer hi) {
        this.hi = hi;
    }

    public void ieme(Twer ac) {
        this.setHi(ac);
        Twer.paurm(new Twer(342));
        ac.bugrap(ac, this);
    }

    public static void semor(Twer ia, int gi) {
    }

    public void treEpaph(Twer ufa, int sca, int mioe) {
        int no = 37;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: