Java Reference
In-Depth Information
Final recursion,
n = 0
, and the
if
statement does nothing, ending the recursive calls:
if
(0 >= 1)
// condition false
{
// this clause is skipped
}
The
output is
3 2 1
.
12.
6
13.
The output is
24
. The method
rose
is the factorial method, usually written
n!
and defined as follows:
n!
is equal to
n
*(
n
−
1
)*(
n
−
2
)*...*
1
14.
public static double
power(
int
x,
int
n)
{
if
(n < 0 && x == 0)
{
System.out.println(
"Illegal argument to power.");
System.exit(0);
}
if
(n < 0)
return
( 1/power(x,
−
n));
else if
(n > 0)
return
( power(x, n
−
1)*x );
else
// n == 0
return
(1.0);
}
15.
public static int
squares(
int
n)
{
if
(n <= 1)
return
1;
else
return
( squares(n
−
1) + n*n );
}
Programming Projects
Many of these Programming Projects can be solved using AW's CodeMate.
To access these please go to:
www.aw-bc.com/codemate
.
1.
A savings account typically accrues savings using compound interest. If you
deposit $1,000 with a 10% interest rate per year, then after one year you have a
total of $1,100. If you leave this money in the account for another year at 10%
interest, then after two years the total will be $1,210. After three years you would
have $1,331, and so on.