CS 112 - Introduction to Computer Science II
Homework #2


Due: Monday 9/11 at the beginning of class

1.  Magic Squares: Exercise P8.19.  Call your test program MagicSquareTester.java.  Prompt the user for a positive odd n and print the message "Parameter n must be a positive odd number." if the user does not enter a valid n. 

2.  Towers of Hanoi:  See the description of Horstmann programming exercise P18.13.  We will implement this slightly differently.  Complete the implementation the given starter code recursively such that your program prints out solution instructions for the given number of disks.  Example output:

$ java Hanoi 1
Move a disk from peg 1 to peg 3.

$ java Hanoi 2
Move a disk from peg 1 to peg 2.
Move a disk from peg 1 to peg 3.
Move a disk from peg 2 to peg 3.

$ java Hanoi 3
Move a disk from peg 1 to peg 3.
Move a disk from peg 1 to peg 2.
Move a disk from peg 3 to peg 2.
Move a disk from peg 1 to peg 3.
Move a disk from peg 2 to peg 1.
Move a disk from peg 2 to peg 3.
Move a disk from peg 1 to peg 3.

An Towers of Hanoi applet is available at http://www.mazeworks.com/hanoi/.