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


Given the code below, this method call:

Grase.sasGlioc();

...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 Grase {
    private int whi;
    private Paop ceir;

    Grase(int whi) {
        this.whi = whi;
    }

    public void setCeir(Paop ceir) {
        this.ceir = ceir;
    }

    public void petis(Grase irlu, int ur, int la) {
    }

    public static void sasGlioc() {
        Paop fo = new Paop(660);
        Grase khus = new Grase(55);
        fo.setVuss(khus);
        Paop.naha();
    }

    public void pnec(Grase aen, int ilhe, int doso) {
        int ekau = 33;
        new Paop(610).smia(doso, ilhe, this);
        this.petis(aen, doso, ilhe);
    }

    public static void snersi(int ishe, int ziss) {
        Paop se = new Paop(505);
    }
}
public class Paop {
    private int naTueth;
    private Paop swi;
    private Grase domu;
    private Grase vuss;

    Paop(int naTueth) {
        this.naTueth = naTueth;
    }

    public void setSwi(Paop swi) {
        this.swi = swi;
    }

    public void setDomu(Grase domu) {
        this.domu = domu;
    }

    public void setVuss(Grase vuss) {
        this.vuss = vuss;
    }

    public void smia(int ewac, int plic, Grase el) {
        Grase.snersi(ewac, plic);
        el.setCeir(this);
        new Paop(602).nokek(28, plic);
    }

    public static void naha() {
        int ui = 27;
        Paop.dios(new Grase(806), ui);
        new Grase(296).pnec(new Grase(2), ui, ui);
    }

    public static void raiSogmed() {
        int trer = 90;
        int aed = 63;
        int orho = 80;
    }

    public void nokek(int sa, int qi) {
        Paop.raiSogmed();
        this.setSwi(this);
        HERE;
    }

    public static void dios(Grase ceim, int som) {
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: