Question: Hello I was assigned a Java problem about Towers of Hanoi and could really use some help. This is the class that needs implemented public

Hello I was assigned a Java problem about Towers of Hanoi and could really use some help. This is the class that needs implemented

public class TowerOfHanoi { // TO DO: Instance Variables /* Construct the Towers of Hanoi (3 towers) with aNumDisc * on the first tower. Each tower can be identified by an * integer number (0 for the first tower, 1 for the second * tower, and 2 for the third tower). Each disc can be identified * by an integer number starting from 0 (for the smallest disc) * and (aNumDisc - 1) for the largest disc. */ public TowerOfHanoi(int aNumDiscs) { // TO DO: Constructor } /* Returns an array of integer representing the order of * discs on the tower (from bottom up). The bottom disc should * be the first element in the array and the top disc should be * the last element of the array. The size of the array MUST * be the number of discs on the tower. For example, suppose * the tower 0 contains the following discs 0,1,4,6,7,8 (from top * to bottom). This method should return the array [8,7,6,4,1,0] * (from first to last). * @param tower the integer identify the tower number. * @return an array of integer representing the order of discs. */ public int[] getArrayOfDiscs(int tower) { // TO DO } /* Gets the total number of discs in this Towers of Hanoi * @return the total number of discs in this Towers of Hanoi */ public int getNumberOfDiscs() { // TO DO } /* Gets the number of discs on a tower. * @param tower the tower identifier (0, 1, or 2) * @return the number of discs on the tower. */ public int getNumberOfDiscs(int tower) { // TO DO } /* Moves the top disc from fromTower to toTower. Note that * this operation has to follow the rule of the Tower of Hanoi * puzzle. First fromTower must have at least one disc and second * the top disc of toTower must not be smaller than the top disc * of the fromTower. * @param fromTower the source tower * @param toTower the destination tower * @return ture if successfully move the top disc from * fromTower to toTower. */ public boolean moveTopDisc(int fromTower, int toTower) { // TO DO } }

//////////////////////////////

And this is the tester class provided ///////////////////////////////

package lab6; public class TowersTester { public static void main(String[] args) { int numDiscs = 7; int point = 0; TowerOfHanoi t = new TowerOfHanoi(numDiscs); // Testing the method getNumberOfDiscs() System.out.println("Construct the Table of Hanoi puzzle with 7 disks using the following statement:"); System.out.println(" TowerOfHanoi t = new TowerOfHanoi(7);"); System.out.println("All test will be base on results of manipulating TowerOfHanoi t. "); System.out.print("Testing the method getNumberOfDiscs(): "); if(t.getNumberOfDiscs() != numDiscs) { System.out.println("FAIL"); System.out.println("The method getNumberOfDiscs() should return " + numDiscs + "."); System.out.println("But your method getNumberOfDiscs() returns " + t.getNumberOfDiscs() + ". "); } else { point++; System.out.println("PASS"); } System.out.println("Your current point is " + point + ". "); // Testing initial puzzle System.out.println("Testing initial puzzle"); int[][] tower0 = {{6,5,4,3,2,1,0},{},{}}; int[] nd0 = {7,0,0}; if(testPuzzle(t, nd0, tower0)) { point++; } System.out.println("Your current point is " + point + ". "); System.out.println("Move top disc from tower 0 to tower 1"); if(testMTD(t.moveTopDisc(0, 1), true, 0, 1)) { point++; } System.out.println("Your current point is " + point + ". "); System.out.println("Testing the current puzzle"); int[][] tower1 = {{6,5,4,3,2,1},{0},{}}; int[] nd1 = {6,1,0}; if(testPuzzle(t, nd1, tower1)) { point++; } System.out.println("Your current point is " + point + ". "); // Move System.out.println("Move top disc from tower 0 to tower 2"); if(testMTD(t.moveTopDisc(0, 2), true, 0, 2)) { point++; } System.out.println("Your current point is " + point + ". "); System.out.println("Testing the current puzzle"); int[][] tower2 = {{6,5,4,3,2},{0},{1}}; int[] nd2 = {5,1,1}; if(testPuzzle(t, nd2, tower2)) { point++; } System.out.println("Your current point is " + point + ". "); // Move System.out.println("Move top disc from tower 1 to tower 2"); if(testMTD(t.moveTopDisc(1, 2), true, 1, 2)) { point++; } System.out.println("Your current point is " + point + ". "); System.out.println("Testing the current puzzle"); int[][] tower3 = {{6,5,4,3,2},{},{1,0}}; int[] nd3 = {5,0,2}; if(testPuzzle(t, nd3, tower3)) { point++; } System.out.println("Your current point is " + point + ". "); // Move (false) System.out.println("Move top disc from tower 0 to tower 2 (MUST RETURN FALSE)"); if(testMTD(t.moveTopDisc(0, 2), false, 0, 2)) { point++; } System.out.println("Your current point is " + point + ". "); System.out.println("Testing the current puzzle (MUST REMAIN UNCHANGED FROM PREVIOUS TEST)"); if(testPuzzle(t, nd3, tower3)) { point++; } System.out.println("Your current point is " + point + ". "); } private static boolean testMTD(boolean aResult, boolean eResult, int fromTower, int toTower) { System.out.print("Test return value of the method moveTopDisc(): "); if(aResult != eResult) { System.out.println("FAIL"); System.out.println("t.moveTopDisc(" + fromTower + "," + toTower + ") should return " + eResult + "."); System.out.println("But your method moveTopDisc(" + fromTower + "," + toTower + ") return " + aResult + "."); } else { System.out.println("PASS"); } return aResult == eResult; } private static boolean testPuzzle(TowerOfHanoi t, int[] numDiscs, int[][] tower) { boolean result = true; for(int i = 0; i < 3; i++) { System.out.print("Testing the method getNumberOfDiscs(" + i + "): "); if(t.getNumberOfDiscs(i) != numDiscs[i]) { System.out.println("FAIL"); System.out.println("The method getNumberOfDiscs(" + i + ") should return " + numDiscs[i] + "."); System.out.println("But your method getNumberOfDiscs(" + i + ") returns " + t.getNumberOfDiscs(i) + ". "); result = false; } else { System.out.println("PASS"); } // Testing the method getArrayOfDiscs(0) System.out.print("Testing the method getArrayOfDiscs(" + i + "): "); if(!arrayToString(tower[i]).equals(arrayToString(t.getArrayOfDiscs(i)))) { System.out.println("FAIL"); System.out.println("The method getArrayOfDiscs(" + i + ") should return the array " + arrayToString(tower[i]) + "."); System.out.println("But your method getArrayOfDiscs(" + i + ") returns the array " + arrayToString(t.getArrayOfDiscs(i)) + ". "); result = false; } else { System.out.println("PASS"); } } return result; } private static String arrayToString(int[] array) { String result = "["; if(array.length > 0) { for(int i = 0; i < array.length - 1; i++) { result = result + array[i] + ","; } result = result + array[array.length - 1]; } return result + "]"; } }

////////// Thanks!!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!