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


Given the code below, this method call:

Trui.nadni();

...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 Trui {
    private int olLia;
    private Racen eee;
    private Racen diid;

    Trui(int olLia) {
        this.olLia = olLia;
    }

    public void setEee(Racen eee) {
        this.eee = eee;
    }

    public void setDiid(Racen diid) {
        this.diid = diid;
    }

    public void chelue() {
        int ar = 86;
        int grir = 57;
        Trui.liowon(grir);
        this.spal(ar);
    }

    public static void sirth(Trui ang, Racen di) {
        int ci = 38;
        Racen esi = new Racen(727);
        di.cucNanse(esi, ang);
    }

    public void fokChount(Racen cer, Racen er) {
        Trui il = new Trui(568);
        cer.sted(new Trui(589));
        il.setEee(cer);
        this.trindo(new Racen(811), cer);
    }

    public void spal(int esm) {
        int bre = 98;
        Trui.oian(bre);
        HERE;
    }

    public void trindo(Racen chiu, Racen vo) {
        vo.setSce(this);
        new Trui(720).chelue();
    }

    public static void oian(int pa) {
    }

    public static void nadni() {
        Racen o = new Racen(829);
        Trui.sirth(new Trui(123), o);
        new Trui(690).fokChount(o, new Racen(816));
    }

    public static void liowon(int viut) {
        int suar = 67;
    }
}
public class Racen {
    private int heLo;
    private Trui sce;
    private Trui bu;

    Racen(int heLo) {
        this.heLo = heLo;
    }

    public void setSce(Trui sce) {
        this.sce = sce;
    }

    public void setBu(Trui bu) {
        this.bu = bu;
    }

    public void cucNanse(Racen ac, Trui sul) {
        Trui pe = new Trui(123);
    }

    public void sted(Trui oru) {
        Racen be = new Racen(911);
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: