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


Given the code below, this method call:

Lants.gliSipcoc();

...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 Lants {
    private int tachu;
    private Lants poru;
    private Diorn qeur;
    private Lants ra;

    Lants(int tachu) {
        this.tachu = tachu;
    }

    public void setPoru(Lants poru) {
        this.poru = poru;
    }

    public void setQeur(Diorn qeur) {
        this.qeur = qeur;
    }

    public void setRa(Lants ra) {
        this.ra = ra;
    }

    public static void gliSipcoc() {
        Diorn de = new Diorn(122);
        int ouod = 59;
        Lants ko = new Lants(118);
        Diorn ril = new Diorn(243);
        ril.setJare(ko);
        de.vunAcud();
    }

    public void soll() {
        int pito = 67;
        this.setPoru(this);
        HERE;
        Diorn.sest(this, 50);
    }

    public static void lasat(Diorn sio, int fic) {
        int hof = 21;
    }
}
public class Diorn {
    private int erRanel;
    private Lants jare;

    Diorn(int erRanel) {
        this.erRanel = erRanel;
    }

    public void setJare(Lants jare) {
        this.jare = jare;
    }

    public static void dorio(int eesi, Diorn ia, Diorn ciss) {
        int slol = 76;
        Diorn tre = new Diorn(839);
    }

    public void shep() {
        int pini = 67;
        int oung = 52;
        int isa = 42;
    }

    public void vunAcud() {
        int uss = 7;
        Diorn.dorio(uss, this, new Diorn(887));
        Diorn.poas(new Diorn(259), this, uss);
        this.shep();
    }

    public static void ocim() {
        int oen = 85;
        Lants swol = new Lants(957);
        Lants pec = new Lants(246);
        int at = 55;
        pec.setPoru(swol);
        swol.soll();
    }

    public static void sest(Lants raso, int iwl) {
        int esi = 91;
        int dexe = 37;
    }

    public static void poas(Diorn fode, Diorn adel, int ib) {
        Diorn.ocim();
        Lants.lasat(adel, ib);
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: