
ITK 179, Fall 2008 Programming Assignment 6, 40 points
Due: November 6, Thursday Midnight
In this assignment, you are asked to implement six recursive functions in six programs described
in the following.
1. fibonacci.java
This program prints the nth fibonacci numbers fn. For example, java fibonacci 6 should
print 8 since f6= 8.
Note: The fibonacci sequence is defined as: f0= 0, f1= 1,and fn=fn−1+fn−2for n≥2.
2. reverse.java
This program prints the input string in the reverse order. For example, java reverse god
should print dog.
3. palindrome.java
This program returns yes or no to tell if the input string is a palindrome. For example,
java palindrome car should print no and java palindrome racecar should print yes .
4. combination.java
The command java combination m n prints all possible combinations of choosing mchar-
acters from ndistinct characters. Let the maximum valid number for nis 26 and let
the ndistinct characters be the first ncharacters in abcdef. . .xyz. For example, java
combination 3 5 should print abc abd abe acd ace ade bcd bce bde cde.
5. hanoi.java
The program prints the sequence of move n(1st argument) disks from the source tower (2nd
argument) to the target tower (3nd argument) using the intermediate tower (4th argument)
in accordance with the restrictions of Hanoi Tower as described in the class. For example
java hanoi 3 a b c should print a->b, a->c, b->c, a->b, c->a, c->b, a->b.
6. gcd.java
The program prints the greatest common divisor of two input integers. For example java
gcd 12 18 should print out 6 because gcd(12,18) = 6.
Note: To write a recursive function for this, use the following mathematical fact:
gcd(a, ax +b) = g cd(a, b)
where a, x, and bare all integers.
Note 1: For this assignment, you don’t have to WebSubmit your programs. Instead, you have
to implement your programs on your Unix account by the deadline. Put all programs
for this assignment in directory ITK179/ASG6 under your home directory. Also, you don’t have
to submit a written report. However, you have to put necessary comments in your programs
including your names.
Note 2: You can name your functions upon your choice in the programs but your have to name
your programs as required. Each program should be syntax-error free and I will use javac to
compile your program into byte-code and run it under the Unix command line. For example, I will
compile your fibonacci.java into fibonacci.class and then I will execute java fibonacci 6
to see the result printed on the screen.
Due: November 6, Thursday Midnight c
Chung-Chih Li P-1/1