Difference between revisions of "CIS 3020 Part 6"

From In The Wings
Jump to navigation Jump to search
Line 2: Line 2:
 
   |__TOC__
 
   |__TOC__
 
   |}
 
   |}
 +
==Ordering of Statements in Methods==
 
Consider the following two methods:
 
Consider the following two methods:
 
{|
 
{|
Line 26: Line 27:
 
   }
 
   }
 
}
 
}
 +
</pre>
 +
|}
 +
* Do they calculate the same values?
 +
* Do they do the calculations in the same way?
 +
* Do they print the same results?
 +
===Execution===
 +
{|
 +
|+ Executing with x=4:
 +
|-
 +
|
 +
<pre>
 +
Entering count1(4)
 +
4
 +
Entering count1(3)
 +
3
 +
Entering count1(2)
 +
2
 +
Entering count1(1)
 +
1
 +
Entering count1(0)
 +
0
 +
Exiting count1(0)
 +
Exiting count1(1)
 +
Exiting count1(2)
 +
Exiting count1(3)
 +
Exiting count1(4)
 +
</pre>
 +
||
 +
<pre>
 +
Entering count2(4)
 +
Entering count2(3)
 +
Entering count2(2)
 +
Entering count2(1)
 +
Entering count2(0)
 +
0
 +
Exiting count2(0)
 +
1
 +
Exiting count2(1)
 +
2
 +
Exiting count2(2)
 +
3
 +
Exiting count2(3)
 +
4
 +
Exiting count2(4)
 
</pre>
 
</pre>
 
|}
 
|}

Revision as of 10:57, 13 April 2007

Ordering of Statements in Methods

Consider the following two methods:

public void count1(int x) {
  if (X <= 0) {
    System.out.println(x);
  } else {
    System.out.println(x);
    count1(x-1);
  }
}
public void count2(int x) {
  if (X <= 0) {
    System.out.println(x);
  } else {
    count2(x-1);
    System.out.println(x);
  }
}
  • Do they calculate the same values?
  • Do they do the calculations in the same way?
  • Do they print the same results?

Execution

Executing with x=4:
Entering count1(4)
4
Entering count1(3)
3
Entering count1(2)
2
Entering count1(1)
1
Entering count1(0)
0
Exiting count1(0)
Exiting count1(1)
Exiting count1(2)
Exiting count1(3)
Exiting count1(4)
Entering count2(4)
Entering count2(3)
Entering count2(2)
Entering count2(1)
Entering count2(0)
0
Exiting count2(0)
1
Exiting count2(1)
2
Exiting count2(2)
3
Exiting count2(3)
4
Exiting count2(4)