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


Given the code below, this method call:

Speet.siork();

...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 Speet {
    private int rior;
    private Chel an;
    private Chel in;

    Speet(int rior) {
        this.rior = rior;
    }

    public void setAn(Chel an) {
        this.an = an;
    }

    public void setIn(Chel in) {
        this.in = in;
    }

    public static void siork() {
        int ed = 61;
        new Speet(796).piqes(new Speet(271), ed, 3);
        new Chel(797).mewn(new Speet(299), ed, new Speet(205));
    }

    public void cral() {
        int poth = 1;
        int bi = 16;
        Chel.uidCisam(poth, this);
        HERE;
    }

    public void piqes(Speet phol, int lic, int cing) {
    }
}
public class Chel {
    private int pei;

    Chel(int pei) {
        this.pei = pei;
    }

    public void mewn(Speet ojom, int enul, Speet as) {
        as.setIn(this);
        new Speet(554).cral();
    }

    public static void uidCisam(int no, Speet re) {
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: