Java Reference
In-Depth Information
Algorithm
drawConcentricCircles(givenNumber, givenDiameter, givenPoint)
if
(givenNumber >= 1)
{
2.
Draw a circle whose diameter is
givenDiameter
and whose center is at
givenPoint
givenDiameter = 4 * givenDiameter / 3
drawConcentricCircles(givenNumber - 1, givenDiameter, givenPoint)
}
3.
public
static
void
countUp(
int
n)
{
if
(n >= 1)
{
countUp(n - 1);
System.out.println(n);
}
// end if
}
// end countUp
4.
public
static
int
productOf(
int
n)
{
int
result = 1;
if
(n > 1)
result = n * productOf(n - 1);
return
result;
}
// end productOf
5.
public
static
void
displayArray(
int
[] array,
int
first,
int
last)
{
if
(first == last)
System.out.print(array[first] + " ");
else
{
int
mid = (first + last) / 2;
displayArray(array, first, mid - 1);
System.out.print(array[mid] + " ");
displayArray(array, mid + 1, last);
}
// end if
}
// end displayArray
6.
The order of events is as follows:
displayBackward()
displayChainBackward(firstNode)
displayChainBackward(
a reference to the second node
)
displayChainBackward(
a reference to the third node
)
displayChainBackward(null)
Print the data in the third node
Print the data in the second node
Print the data in the first node
Activation records for the calls to
displayChainBackward
appear in a stack, as follows (
dCB
is an abbreviation for
displayChainBackward
; the stack is shown top to bottom):
dCB(firstNode)
dCB(
reference to second node
) dCB(firstNode)
dCB(
reference to third node
) dCB(
reference to second node
) dCB(firstNode)
dCB(null) dCB(
reference to third node
) dCB(
reference to second node
) dCB(firstNode)
dCB(
reference to third node
) dCB(
reference to second node
) dCB(firstNode)
Print the data in the third node
dCB(
reference to second node
) dCB(firstNode)