created 01/01/03


Chapter 73 Programming Exercises


Exercise 1

Write a program that implements this definition of cube numbers:

cube(1) = 1
cube(N) = cube(N-1) + 3(square(N)) - 3N + 1

Implement the square() method using this definition (also given in the exercises for chapter 71):

square(1) = 1
square(N) = square(N-1) + 2N - 1

Make a complete program similar to PyramidTester.java given in the chapter.

Click here to go back to the main menu.


Exercise 2

Consider this definition of the sum of the elements in an integer array:

sum( array, index ) = 0, if index == array.length 

sum( array, index ) = array[index] + sum( array, index+1), if index < array.length

Write a Java method that implements this definition and a program to test it. The method should look something like:

int sum ( int[] array, int index )
{
 . . .
}

The testing program will call sum( testArray, 0 ).

Click here to go back to the main menu.


Exercise 3

Improve the previous program by extending the definition of sum:

sum( array ) = sum( array, 0 )

sum( array, index ) = 0, if index == array.length 

sum( array, index ) = array[index] + sum( array, index+1), if index < array.length

To implement this, write a second method sum( int[] array) that overloads the method of exercise 1. The testing program will call sum( testArray ).

Click here to go back to the main menu.


Exercise 4

Write your own recursive definition of the maximum element in an array of integers. Then, implement your definition in Java and test it with a testing program.

Click here to go back to the main menu.


Exercise 5

A palindrome is a string that is the same when reversed. For example, "abba" is a palindrome. Here is a math-like definition:

palindrome( "" ) = true

palindrome( x  ) = true

palindrome( x+X+y ) = false, if x != y
                    = palindrome( X ), if x == y

The symbol x stands for a single character, as does y. The symbol X stands for a string of characters. The symbol + stands for concatenation.

Implement palindrome() and a program that tests it.

Click here to go back to the main menu.


End of Exercises