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


Given the code below, this method call:

Grie.iead();

...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 Grie {
    private int itcri;
    private Oprid lal;
    private Grie ve;
    private Oprid eunt;

    Grie(int itcri) {
        this.itcri = itcri;
    }

    public void setLal(Oprid lal) {
        this.lal = lal;
    }

    public void setVe(Grie ve) {
        this.ve = ve;
    }

    public void setEunt(Oprid eunt) {
        this.eunt = eunt;
    }

    public static void teme() {
        Grie me = new Grie(271);
        Grie adki = new Grie(126);
        Grie ri = new Grie(447);
    }

    public static void abem() {
        Oprid mi = new Oprid(509);
        Oprid scla = new Oprid(791);
        Oprid pla = new Oprid(0);
        int oa = 4;
    }

    public static void menrur(Oprid gi, int ina) {
        Grie e = new Grie(257);
        gi.hacIanca(gi);
    }

    public static void iead() {
        int ad = 29;
        new Oprid(479).tror(new Grie(266), ad);
        Grie.menrur(new Oprid(931), ad);
    }

    public void scaa(Oprid idcu, int pe) {
        HERE;
    }
}
public class Oprid {
    private int erksa;
    private Oprid lusm;

    Oprid(int erksa) {
        this.erksa = erksa;
    }

    public void setLusm(Oprid lusm) {
        this.lusm = lusm;
    }

    public void teoHass(int orm, int iis) {
        Oprid er = new Oprid(678);
        Oprid ep = new Oprid(182);
        ep.setLusm(er);
        Oprid.pniho(orm, ep, iis);
        ep.chosm(new Grie(931), this, iis);
    }

    public void hacIanca(Oprid ra) {
    }

    public void tror(Grie ii, int maip) {
        int ac = 12;
        ii.setLal(this);
        this.teoHass(82, maip);
        Grie.teme();
    }

    public void chosm(Grie nel, Oprid onma, int la) {
    }

    public static void pniho(int ospi, Oprid ang, int isac) {
        int gop = 86;
        int numu = 8;
        ang.setLusm(ang);
        new Grie(483).scaa(ang, isac);
        Grie.abem();
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: