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


Given the code below, this method call:

Lel.roosm();

...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 Lel {
    private int ska;
    private Paent te;
    private Paent uss;

    Lel(int ska) {
        this.ska = ska;
    }

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

    public void setUss(Paent uss) {
        this.uss = uss;
    }

    public static void roosm() {
        Paent.tarhom(new Lel(794), new Paent(746));
        Lel.agiss(new Paent(554), new Paent(178));
        new Paent(544).awntre();
    }

    public void cuer() {
        Lel smac = new Lel(474);
        new Paent(745).wiin();
        HERE;
    }

    public static void agiss(Paent ine, Paent sint) {
        sint.setOn(ine);
        Paent.ucpram(ine, sint);
    }
}
public class Paent {
    private int upio;
    private Paent on;

    Paent(int upio) {
        this.upio = upio;
    }

    public void setOn(Paent on) {
        this.on = on;
    }

    public static void ucpram(Paent pran, Paent uin) {
        Lel iis = new Lel(502);
        uin.setOn(pran);
        iis.cuer();
    }

    public void wiin() {
        int stri = 90;
        int pust = 58;
    }

    public static void tarhom(Lel os, Paent pauc) {
    }

    public void awntre() {
        int fe = 56;
        Lel oc = new Lel(147);
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: