Java Reference
In-Depth Information
7 .
public static void
cheers(
int
n)
{
while
(n > 1)
{
System.out.print("Hip ");
n--;
}
System.out.println("Hurray");
}
8 .
public static void
stars(
int
n)
{
for
(
int
count = 1; count <= n; count++)
System.out.print('*');
}
9 .
public static void
backward(
int
n)
{
while
(n >= 10)
{
System.out.print(n%10);
//write last digit
n = n/10;
//discard the last digit
}
System.out.print(n);
}
10. The trace for Self-Test Exercise 4: If
n = 3
, the code to be executed is
if
(3 >= 1)
{
writeUp(2);
System.out.print(3 + " ");
}
The execution is suspended before the
System.out.println
. On the next recursion,
n = 2
; the code to be executed is
if
(2
>=
1)
{
writeUp(1);
System.out.print(2 + " ");
}
The execution is suspended before the
System.out.println
. On the next recursion,
n = 1
and the code to be executed is
if
(1 >= 1)
{
writeUp(0);
System.out.print(1 + " ");
}