/* In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide on to any 
tower The puzzle starts with disks sorted in ascending order of size from top to bottom (e g , each disk sits on top of an 
even larger one) You have the following constraints:
(A) Only one disk can be moved at a time
(B) A disk is slid off the top of one rod onto the next rod
(C) A disk can only be placed on top of a larger disk
Write a program to move the disks from the first rod to the last using Stacks
*/

@@ -1,8 +1,52 @@
+package edu.java;%0A%0Aimport java.util.Stack;%0A%0A
 /* In th
@@ -574,8 +574,1189 @@
 acks%0A*/%0A
+%0Apublic class Hanoi %7B%0A%0A%09/**%0A%09 * @param args%0A%09 */%0A%09public static void main(String%5B%5D args) %7B%0A%09%09int n = 5;%0A%09%09Tower%5B%5D towers = new Tower%5Bn%5D;%0A%09%09for (int i = 0; i %3C 3; i++) towers%5Bi%5D = new Tower(i); %0A%09%09for (int i = n - 1; i %3E= 0; i--) towers%5B0%5D.add(i); %0A%09%09towers%5B0%5D.moveDisks(n, towers%5B2%5D, towers%5B1%5D);%0A%0A%09%7D%0A%0A%7D%0A%0Aclass Tower %7B%0A%09private Stack%3CInteger%3E disks;%0A%09private int index;%0A%09public Tower(int i )%7B%0A%09%09this.disks = new Stack%3CInteger%3E();%0A%09%09this.index = i;%0A%09%7D%0A%09%0A%09public int index()%7B%0A%09%09return index;%0A%09%7D%0A%09%0A%09public void add(int d)%7B%0A%09%09if (!disks.isEmpty() && disks.peek()%3Cd)%7B%0A%09%09%09System.out.println(%22Cannot put on top of it%22);%0A%09%09%7D else %7B%0A%09%09%09disks.push(d);%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void moveTopTo(Tower t)%7B%0A%09%09int top = disks.pop();%0A%09%09t.add(top);%0A%09%09System.out.println(%22Move%22+ top + %22from %22+this.index()+ %22to%22+ t.index());%0A%09%7D%0A%09%0A%09public void print() %7B%0A%09%09System.out.println(%22tower print+ %22+this.index());%0A%09%09for (int i = disks.size() - 1; i %3E= 0; i--) %7B%0A%09%09%09System.out.println(%22 %22 + disks.get(i)); %7D%0A%09%7D%0A%09%0A%09public void moveDisks(int n, Tower destination, Tower buffer) %7B%0A%09%09if (n %3E 0) %7B%0A%09%09%09moveDisks(n - 1, buffer, destination); %0A%09%09%09moveTopTo(destination);%0A%09%09%09buffer.moveDisks(n - 1, destination, this);%0A%09%09%7D%0A%09%7D%0A%7D%0A

Update Hanoi.java