-2

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!

andrsnn
  • 1,591
  • 5
  • 25
  • 43
  • Please show your efforts. – rocketboy Aug 03 '13 at 02:23
  • Yes updated. I am particularly confused as to "correctly format" a runtime stack. – andrsnn Aug 03 '13 at 02:26
  • Common guys, I understand it is a simple question (hence the downvotes) but if anyone could kindly help me in the right direction, I will be one step further toward understanding java and programming better.... – andrsnn Aug 03 '13 at 02:29
  • Presumably he means that the *format* you are using doesn't correctly represent what's on the stack, which it certainly doesn't. I would have thought just a table showing values and where they came from, but it's his question. – user207421 Aug 03 '13 at 02:41
  • @EJP how would you format it? – andrsnn Aug 03 '13 at 02:47
  • Do you mean at one point you answered a question like this? – andrsnn Aug 03 '13 at 04:10
  • I mean that I answered it *here.* Read what I wrote again: the part starting 'I would have thought ...'. Really this is ridiculous. I suggest you post the actual assignment question here. Clearly you didn't understand that either. – user207421 Aug 03 '13 at 04:39
  • Wow buddy. Sorry, clearly we all have to start somewhere. Maybe you can humble yourself, as you were once in my position, albeit with different problems. The assignment question *IS* exactly how I posted it above. Draw the runtime stack from the comment *HERE*. That is the exact question. – andrsnn Aug 03 '13 at 05:51
  • Again, as my professor told me I had the question wrong because of its format, (I also displayed the answer in a table) I asked this question here as it suggested there was a more correct and proper way to format the answer. – andrsnn Aug 03 '13 at 05:53

2 Answers2

1

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.

LhasaDad
  • 1,786
  • 1
  • 12
  • 19
0

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
negoose
  • 177
  • 1
  • 3
  • 9
  • Yes I updated it. I was not home next to the paper with my answer so I just stepped through it again. I asked this question as per my professor was assuming a format, I was assuming there was a correct way format to the stack – andrsnn Aug 03 '13 at 02:47