/** value = 1; } else { value

/** * This implementation is derived from the Tower of Hanoi Algorithm * Source Code available at * http://www.softwareandfinance.com/Java/TowerOfHanoi_Algorithm.html * authored by Kathiresan. */import java.lang.*;import java.util.*;@SuppressWarnings(“unchecked”)class TOH { private static int moveCount = 0; private static int countA = 0; private static int countB = 0; private static int countC = 0; static private Stack A = new Stack(); static private Stack B = new Stack(); static private Stack C = new Stack(); static private void Solve2DiscsTOH(Stack source, Stack temp, Stack dest) { temp.push(source.pop()); PrintStacks(); dest.push(source.pop()); PrintStacks(); dest.push(temp.pop()); PrintStacks(); moveCount += 3; } static private int SolveTOH(int nDiscs, Stack source, Stack temp, Stack dest) { int value = 0; if(nDiscs <= 4) { if ((nDiscs % 2) == 0) { value = 1; } else { value = 2; } } else { value = 3; } switch(value){ case 1: Solve2DiscsTOH(source, temp, dest); nDiscs = nDiscs - 1; if (nDiscs == 1) { return 1; } temp.push(source.pop()); moveCount++; PrintStacks(); Solve2DiscsTOH(dest, source, temp); dest.push(source.pop()); moveCount++; PrintStacks(); SolveTOH(nDiscs, temp, source, dest); return 1; case 2: if (nDiscs == 1) { return -1; } Solve2DiscsTOH(source, dest, temp); nDiscs = nDiscs - 1; dest.push(source.pop()); moveCount++; PrintStacks(); Solve2DiscsTOH(temp, source, dest); return 1; case 3: SolveTOH(nDiscs - 2, source, temp, dest); temp.push(source.pop()); moveCount++; PrintStacks(); SolveTOH(nDiscs - 2, dest, source, temp); dest.push(source.pop()); moveCount++; PrintStacks(); SolveTOH(nDiscs - 1, temp, source, dest); } return 1; } static private void PrintStacks() { if (countA != A.size() || countB != B.size() || countC != C.size()) { int diffA = A.size() - countA; int diffB = B.size() - countB;// local variable never used// int diffC = C.size() - countC; int value = 0; if (diffA == 1 && diffB == -1) value = 0; else if (diffA == 1) value = 1; else if (diffB == 1 && diffA == -1) value = 2; else if (diffB == 1) value = 3; else if (diffA == -1) value = 4; else value = 5; switch (value) { case 0: System.out.print("Move Disc " + A.peek() + " From B To A"); break; case 1: System.out.print("Move Disc " + A.peek() + " From C To A"); break; case 2: System.out.print("Move Disc " + B.peek() + " From A To B"); break; case 3: System.out.print("Move Disc " + B.peek() + " From C To B"); break; case 4: System.out.print("Move Disc " + C.peek() + " From A To C"); break; case 5: System.out.print("Move Disc " + C.peek() + " From B To C"); } countA = A.size(); countB = B.size(); countC = C.size(); System.out.println(); } PrintStack(A); System.out.print(" , "); PrintStack(B); System.out.print(" , "); PrintStack(C); System.out.print(" , "); } private static void PrintStack(Stack s) { System.out.print(s.toString()); } // main method public static void main(String args) { try { //generating a random number from 2-9 Random rand = new Random(); int discNumber = rand.nextInt(8) + 2; for (int i = discNumber; i >= 1; i–) { A.push(i); } countA = A.size(); countB = B.size(); countC = C.size(); PrintStacks(); SolveTOH(discNumber, A, B, C); System.out.println(“Total Moves = ” + moveCount); while (C.size() > 0) { C.pop(); } } catch (Exception e) { e.printStackTrace(); } }}