ICS 201 Introduction to Computing II

ICS 201 Introduction to Computing II

ICS 201 – Introduction to Computing II

First Semester 2007/2008 (071)

Homework # 3

All Sections

Due on Friday November 09, 2007 – 10:00 p.m.

Part I: Self-Test Exercises

  1. Write a recursive method definition for the following method:

/**

Precondition: n >= 1

Returns the sum of the squares of the numbers 1 through n.

*/

public static int squares(int n)

For example, squares(3) returns 14 because 12 + 22 + 32 is 14.

  1. What is the output of the following program?

public class Exercise12 {

public static void main(String[] args) {

System.out.println(mystery(3));

}

public static int mystery(int n) {

if (n <= 1)

return 1;

else

return ( mystery(n − 1) + n );

}

}

  1. What is the output of the following program? What well-known mathematical method is rose?

public class Exercise13 {

public static void main(String[] args) {

System.out.println(rose(4));

}

public static int rose(int n) {

if (n <= 0)

return 1;

else

return ( rose(n − 1) * n );

}

}

  1. Redefine the method power (Display 11.3 – Page 667 – Chapter 11) so that it also works for negative exponents. To do this, you also have to change the type of the value returned to double. The method heading for the redefined version of power is as follows:

/**

Precondition: If n < 0, then x is not 0.

Returns x to the power n.

*/

public static double power(int x, int n)

Hint: x −n is equal to 1/( x n).

Part II: Programming Problems

Programming Problem 1:

Write a recursive method to print each element of an array of double elements with the following signature:

/**

* printArr() prints each element of the array array starting at the index first. To print * the array named myArray, you would call this method with:

* printArray(myArr, 0);

*/

public void printArray (double[] array, int first)

Place the method in a test class that has a main method to test the reverseString() method.

Programming Problem 2:

Write a recursive method that returns the reverse of the input String with the following signature:

/**

* The method signature needs to be modified to correctly handle the input String input.

*/

public String reverseString(String input, …)

Place the method in a test class that has a main method to test the reverseString() method.

Programming Problem 3:

Write a recursive method that will concatenate the elements of an array of String into a single String delimited by blanks with the following signature:

/**

* The method signature needs to be modified to correctly handle the input String input.

*/

public String myConcat(String input, ….)

Place the method in a test class that has a main method to test the myConcat () method.

Programming Problem 4:

Write a recursive method that is passed a single int parameter, N ≥ 0, and prints all the odd numbers between 1 and N with the following signature:

public void printOdds (int N)

Place the method in a test class that has a main method to test the printOdds () method.

Programming Problem 5:

Write a recursive method that is passed a single int parameter, N ≥ 0, prints

the multiples of 10 between 0 and N with the following signature:

public void printTens (int N)

Place the method in a test class that has a main method to test the printTens () method.

Programming Problem 6:

Write a recursive method to convert a String representing a binary number to its decimal equivalent. For example, binTodecimal("101011") should return the int value 43.