Question: Assignment: Towers of Hanoi Methods: The Puzzle: The Towers of Hanoi puzzle has three posts and some number n of disks. Each disk has a



Assignment: Towers of Hanoi Methods: The Puzzle: The Towers of Hanoi puzzle has three posts and some number n of disks. Each disk has a hole in o getNumDisk ( ) : method to return the total number of Disks in the Rod. the middle, so that it can be put on a post, and the disks are of different sizes. O getDisk ( ) : method to return the topmost disk from the rod. o removeDisk ( ) : method to remove and return the topmost disk from the rod. Initially, the disks are all stacked on the leftmost post, with larger disks closer to the bottom and smaller disks addDisk () : method that takes in a Disk parameter and to adds it on top of the rod. closer to the top. For example, if there are three disks, the puzzle starts out looking like this. o toString () : method to return the contents of the Rod object in the following order: Example of what the toString () method should return: 3 2 As mentioned earlier the version of TOH we are modelling consists of 3 rods, and a number of disks, all of different sizes, which can slide onto any of these rods. The puzzle again starts with the disks in a neat stack in descending order of size on one rod, the largest disk (by size) positioned at the top, thus making an inverted pyramid. The goal of this puzzle is to find the optimal solution for moving all disks from "Rod A" to another The object is move all of the disks from the leftmost post to the rightmost post. So, when the puzzle is solved, it obeying the following rules: looks like this. Only one disk may be moved at a time. . Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod A smaller disk (in size) cannot be placed on top of a larger (in size) disk. Example : Rod A: 2 1 But there are some rules that must be followed. Rod B: 3 1. Only one disk can be held in your hand at a time. The other disks must be on posts. Rod C: 2. A disk can only be put down by placing it on one of the posts. For example, you can't put a disk on the Note: Disk 2 from Rod A cannot be moved to Rod B but it can move to Rod C table beside the puzzle. 3. No disk can ever be put on top of a smaller disk. Finally, implement the Hanoi object that models the actions in the TOH problem; this class for example lets As part of the assignment we will implement a different version of the Towers of Hanoi (TOH) problem by - 1) you move a disk from one Rod to another. The Hanoi object is defined as follows: using multiple objects & recursion and 2) our version of TOH have larger disks on top and smaller disks at the Fields (encapsulated): The Hanoi object consist of 3 Rod objects. bottom of the rod (i.e. decreasing order). Constructor: takes one parameter - an integer n and instantiates one Rod with n disks and rest 2 with 0 disks. DETAILS: Methods: (Note: You can define any additional methods you deem necessary) Our starting point is the implementation of the Disk object to model an Object-Orient version of the Towers of o printStatus () : method to print the contents of the game status after every move (see sample output Hanoi. The Disk object is defined as follows: for the format). Fields (encapsulated): An integer representing the size (width) of the Disk o move ( ) : method that take two Rod parameters and moves disk from one rod to another by following Constructor: takes one parameter - an integer size to create an instance of the Disk above mentioned rules. This is a helper method, so make it private. Methods: o recursiveTOH ( ) : recursive method to solve TOH problem. Make sure to include (display) the game getSize () : method to return the size of the disk. (Note: Our Disk object is immutable). status of every move in this method. o The Disk object is comparable in nature. Implement the compareTo () method to compare two disks A tester (client) program called TOHtester . java is provided to test the TOH problem for Rods with different based on their size. Disk sizes. Your program should be able to successfully run all test cases. The sample output (output.txt) is attached with the assignment for your reference. Next we implement the Rod object to hold zero or more disk objects (and keeps their order). The Rod object is How to submit your assignment: defined as follows: Fields (encapsulated): A List (ArrayList or LinkedList) to store the disks. Zip and upload the following files to Programming Assignment 3 submission link: Constructor: takes one parameter - an integer n and instantiates the Rod with n disks of sizes n to 1 (see the 1. Source Code Files: Disk . java, Rod. java, and Hanoi . java toString () method), with the largest disk being at the top of your list. 2. Word document with class diagram for each object (there is no inheritance between these objects). Hint: fix top at one end of the list. Example, disk n can reside at index n-I and smallest disk is at index 0.public class TOHtester { public static void main (String[ ] args) { // Test 1: Create an instance of the Hanoi problem for n = 1 Hanoi h1 = new Hanoi (1) ; // Make a recursive call System. out. println ( "TEST 1: where n = 1") ; System. out. println( "Initial State:") ; hl . printStatus () ; // Display Initial State h1 . recursiveTOH ( ) ; System. out. println( "Final State:") ; h1. printStatus () ; // Display Final State / / Test 2: Create an instance of the Hanoi problem for n = 4 Hanoi h2 = new Hanoi (4) ; // Make a recursive call System. out. println ( "TEST 2: where n = 4") ; System. out. println( "Initial State:") ; h2.printStatus( ) ; // Display Initial State h2 . recursiveTOH ( ) ; System. out. println ( "Final State:") ; h2.printStatus ( ); // Display Final State // Test 3: Create an instance of the Hanoi problem for n = 7 Hanoi h3 = new Hanoi (7) ; // Make a recursive call System. out. println ( "TEST 3: where n = 7"); System. out. println ( "Initial State:") ; h3. printStatus ( ); // Display Initial State h3 . recursiveTOH ( ) ; System. out . println( "Final State:") ; h3.printStatus () ; // Display Final StateTEST 1: where n = 1 Initial State: Rod A: 1 Rod B: Rod C: Final State: Rod A: Rod B: Rod C: 1 TEST 2: where n = 4 Initial State: Rod A:4 3 2 Rod B: Rod C: Rod A: 3 2 1 Rod B: 4 Rod C: Rod A: 2 H Rod B: 4 Rod C: 3 Rod A: 2 1 Rod B: Rod C:4 Rod A: 1 Rod B: 2 Rod C:4 3 Rod A: 4 Rod B: 2 Rod C:3 Rod A:4 Rod B: 3 Rod C: Rod A: 1 Rod B:4 3 2 Rod C: Rod A: Rod B: 4 3 2 Rod C: 1 Rod A: Rod B: 3 HN Rod C:4 Rod A: 3 Rod B: 2 Rod C:4 Rod A:4 Rod B: 2 Rod C: 1 Rod A:4 W Rod B: Rod C:2 H Rod A: 3 Rod B: 4 Rod C:2 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
