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


Given the code below, this method call:

Piosh.upser();

...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 Piosh {
    private int alCa;
    private Piosh om;
    private Cil cei;

    Piosh(int alCa) {
        this.alCa = alCa;
    }

    public void setOm(Piosh om) {
        this.om = om;
    }

    public void setCei(Cil cei) {
        this.cei = cei;
    }

    public static void upser() {
        int duol = 41;
        new Piosh(867).heca(new Piosh(802), duol);
        Piosh.erber();
    }

    public static void erber() {
        int he = 72;
        int re = 88;
        int e = 46;
    }

    public void scapa(Cil is, int io, Cil il) {
        this.rord();
        il.setSa(is);
        HERE;
    }

    public void rord() {
        int ac = 64;
        int bi = 36;
    }

    public void heca(Piosh frai, int se) {
        int eol = 40;
        this.setOm(frai);
        Cil.ofli();
        Cil.hurne(this, eol);
    }
}
public class Cil {
    private int loPe;
    private Cil sa;

    Cil(int loPe) {
        this.loPe = loPe;
    }

    public void setSa(Cil sa) {
        this.sa = sa;
    }

    public static void hurne(Piosh sna, int mo) {
        int cel = 89;
        int gle = 85;
    }

    public static void ofli() {
        int vun = 57;
        new Piosh(273).scapa(new Cil(922), vun, new Cil(980));
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: