Question

Can anyone help me draw the run time stack from the comment HERE for the following code? I attempted this, but was told it was incorrect, and my professor refused to explain this.... If anyone can give me an example, it will be easy to use that as a comparison for another piece of code. Specifically I believe I am not formatting the stack correctly.

My attempt:

array a initialized with 3 values
integer x = 4;
call method b, passes in array a.
integer i = 14;
array x[0] = 4;
call method e, passes in i - 2 = 12;
calls method f 12 - 4 = 8;
int j = 2;
8 * 2 = 16;
println "16";
println "4 + 0"

public class RuntimeStack
{
  public static void main (String [] args)
  {
  int[] a = new int[3];
  int x = 4;
  b(a);
  System.out.println(a[0] + “ “ + a[1] + “ “ + x);
}
public static void e(int a)
{
f(a-4);
// HERE! }
  public static void b(int [] x)
  {
    int i = 14;
    x[0] = 4;
    e(i-2);
}
  public static void f(int i)
  {
int j = 2;
    System.out.println("i * j = " + (i * j));
  }
} // end class RuntimeStack

Thank you very much!

Was it helpful?

Solution

My guess would be he wants a diagram drawn with the values in the stack in the order they would have been placed on the stack (and possibly removed). if you have a method calling a 2nd method passing in parameters each time they may want the list of parms and the return information for the call etc. that would be my best guess of what they want.

OTHER TIPS

Continuing from where you left off:

call method e, passes in 12  
call method f, passes in 8  
integer j = 2  
print "i * j = " + 16  
print 4 + " " + 0 + " " + 4
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top