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

Write a complete program that tests the equals() method at the end of the chapter. Prompt the user for the two strings to be tested. Make equals() a static method in the class that holds main().

Another implementation: define a class called myString that contains a reference to a String and the method equals(). Your main() will create two myString objects and use their equals() methods.

Click here to go back to the main menu.


Exercise 6

Change the recursive definition of equals so that the case of letters does not matter. Translate this new definition into Java and test it.

Click here to go back to the main menu.


Exercise 7

Change the recursive definition of equals so that vowels are ignored. Now "kangaroo" equals "kongeroo", both of them equals "kaangaro", and also equal "kngr".

The easy way to program this would be to first strip out all vowels and then compare what is left. But for this exercise, try to write a recursive definition that does the comparison on the unaltered strings. This may be somewhat difficult. Translate your definition into Java and test it.

Click here to go back to the main menu.


Exercise 8

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