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


Given the code below, this method call:

Diian.ciade();

...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 Diian {
    private int nesm;
    private Grici te;
    private Diian tene;

    Diian(int nesm) {
        this.nesm = nesm;
    }

    public void setTe(Grici te) {
        this.te = te;
    }

    public void setTene(Diian tene) {
        this.tene = tene;
    }

    public void engsol(Grici tu) {
        Grici tro = new Grici(960);
        Diian dra = new Diian(92);
        new Grici(440).segpe();
        tro.setDovi(dra);
        Diian.eisTrem();
    }

    public static void eisTrem() {
        Grici reud = new Grici(538);
        Grici splu = new Grici(581);
        reud.umspes();
        Grici.benirt(reud);
    }

    public static void ciade() {
        Diian cupa = new Diian(822);
        Grici.sqenku(cupa);
        cupa.setTene(cupa);
        cupa.engsol(new Grici(742));
        Grici.chiwl(cupa, cupa);
    }

    public void esird(Grici fean, Grici dass, Grici cet) {
        Diian clae = new Diian(455);
    }

    public void locbir() {
        int phue = 89;
        int fliu = 2;
        int ei = 1;
        int relt = 47;
    }
}
public class Grici {
    private int feIac;
    private Diian rol;
    private Diian dovi;

    Grici(int feIac) {
        this.feIac = feIac;
    }

    public void setRol(Diian rol) {
        this.rol = rol;
    }

    public void setDovi(Diian dovi) {
        this.dovi = dovi;
    }

    public static void chiwl(Diian fu, Diian shic) {
        int ce = 8;
        int ic = 65;
        shic.locbir();
    }

    public void segpe() {
        Diian so = new Diian(711);
    }

    public static void ladad(int dri) {
        int meul = 28;
        HERE;
    }

    public static void sqenku(Diian aidu) {
        Grici irua = new Grici(714);
        Grici iwho = new Grici(63);
        Diian prid = new Diian(259);
        Grici tair = new Grici(754);
        prid.esird(new Grici(408), tair, iwho);
    }

    public void umspes() {
        int sepi = 94;
        int tist = 99;
    }

    public static void benirt(Grici stol) {
        int bont = 18;
        int gunt = 6;
        Grici.ladad(gunt);
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: