Section Readings: 2.3

<标题>Problem 1. (Sum of Integers) Implement the static methods sumIter() and sumRec() in

SumOfInts that take an integer argument n and return the sum S(n) = 1 + 2 + 3 + · · · + n,

computed iteratively (using a loop) and recursively. The recurrence relation for the latter

<标题>implementation is

<标题>$ java SumOfInts 100

5050 5050

Problem 2. (Exponentiation) Implement the static method power() in Power that takes

two integer arguments a and b and returns the value of a

<标题>the recurrence relation

$ java Power 3 5

243

Problem 3. (Bit Counts) Implement the static methods zeros() and ones() in Bits that

takes a bit string (ie, a string of zeros and ones) s as argument and returns the number

<标题>of zeros and ones in s, each computed recursively. Hint: First devise an appropriate

<标题>recurrence relation for the problem.

$ java Bits 1010010010011110001011111

zeros = 11 , ones = 14 , total = 25

Problem 4. (String Reversal) Implement the static method reverse() in Reverse that

<标题>takes a string s as argument