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


Given the code below, this method call:

Uaght.stiRetsa();

...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 Uaght {
    private int asung;

    Uaght(int asung) {
        this.asung = asung;
    }

    public void oulpa(int pi, int odca, Uaght ebre) {
    }

    public void spamal(Uaght jia, Uaght seng, int tini) {
    }

    public static void pahil(Uaght psed, int ci, int dri) {
        new Artoo(632).seace(new Artoo(197), ci, psed);
        new Uaght(140).oulpa(dri, ci, psed);
    }

    public static void stiRetsa() {
        Uaght ibic = new Uaght(143);
        int be = 9;
        int ir = 90;
        Uaght.pahil(ibic, ir, be);
        new Uaght(159).spamal(ibic, new Uaght(549), be);
    }
}
public class Artoo {
    private int wel;
    private Artoo me;
    private Artoo sted;

    Artoo(int wel) {
        this.wel = wel;
    }

    public void setMe(Artoo me) {
        this.me = me;
    }

    public void setSted(Artoo sted) {
        this.sted = sted;
    }

    public void seace(Artoo onri, int tre, Uaght foad) {
        this.setSted(onri);
        HERE;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: