Programming and Problem Solving with Java

(やまだぃちぅ) #1

656


4.Modify the factorialmethod discussed in this chapter to print its parameter
and returned value indented two spaces for each level of call to the method.
The call factorial( 3 ) should produce the following output on System.out:
3 2 1 0 1 1 2 6

5.Write a recursive value-returning method that sums the integers from 1
throughn.
6.Rewrite the following method so that it is recursive.
public static voidprintSqRoots(intn)
{
inti;
for(i = n; i > 0 ; i--)
outFile.println(i + " "+ Math.sqrt((double)i));
}
7.Theprintmethod discussed in this chapter prints the contents of an array from
first element to last. Write a recursive method that prints from last element to
first.
8.Write an isTheremethod that takes an array as a parameter and performs a re-
cursive linear search.
9.Rewrite the powermethod using another base case.
10.Rewrite the powermethod using the following formula.
if n == 0, return 1
if n == 1, return x
if nis even, return power(x*x, n/2)
else return x*power(x, n–1)
Free download pdf