java系列作业代写lab6

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

Leave a Reply

Your email address will not be published. Required fields are marked *