Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Tower of Hanoi (Modified from Wiki) The Tower of Hanoi is a mathematical game or puzle. It consists of three poles and a nmberof disks

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Tower of Hanoi (Modified from Wiki) The Tower of Hanoi is a mathematical game or puzle. It consists of three poles and a nmberof disks of different sizes, which can slide onto any pole. The puzle starts with the disks in a neat stack in ascending order of size on pole mmber 1 (leftmost pole), the smallest at the top, thus making a conical shape The objoctive of the puxzle is to move the entire stack to another pole, obeying the following simple rules: 1. Only one disk can be moved at a tie 2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty pole 3. No larger dik may be placed on top of a aler disk. With 3 disks, the puzzle can be solved in7oves. The minimal number of moves required to solve a Tower of Hanoi puzzl is 2-1, where n is te number of disks. The following is an example of a Tower of Hanoi puzzle: The final goal of this project is for you to create a Tower of Hanoi puxxe game with any number of disks so that a user can play on his/her coputr. No worry, it is going to be a text mode puxale Not a graphic one, The main goal is for you to practice Object-Oriented Programing. We are going to look at a Tower of Hanoi puzzle as a physical object and try to write our program using it as a guideline. For now, let's look at the Tower of Hanoi puzzle shown earlier and see what physical objects are in the picture Disks: There are a mber of disks with different sies. Poles: There are three poles. . The Puxzle: The whole puzzle (Tower of Hanoi) tself that consists of a umber of disks and three poles So, we are going to implement our program base on the above observation. Note that we are going to simulate the situation where your classes may be used by other programmers, In other words, you need to protect your class in case of any unusual situations. These situations will be discussed in each class. Part I: The Disk Class (10 Points) A most important characteristice of a disk itz Recall that in a Tower of Hanoi puxxle, there are a number of disks but none of them have the same size. Because of this, the size should be a part of its constructor to constrct a disk with a given size The other noticeable characteristic of a disk is that, once you construct a disk, you cannot change its size. You eed to destroy it or sand it (which is the same as reconstruct it). So, the Diak class will be what we usually call an immutable class. A use cannot change the data inside the object of type Disk once it has been constructed. However, users can still read data out of it. So, from the above observations, the Diak clas should have the following: Constructors: - public Disk(int aDiakSize, char aDiskChar, char aPoleChar): This costructor allows a user to construct a disk with any size greater than . The aDiskChar ad aPoleChar parameters will be used to generate the String representation of an object of type Disk. This i be discussed later -public Disk(int aDiskSize): This constructor constructs a disk with a give size and use the asterisk character ) as the disk character and use the vertical bar character (1) as the pole character -Unusual Situations: A disk cannot have size 0 or less. However, since your Disk class may be used by another programmer, you have to protect your clas. There is nothing to prevent a programmer to construct a disk with a negative number or zeo for the disk size as shown below: Disk aDisk neu Disk(-4, '''1D Note that a constructor cannot retur a value. So, th is o way for your class to signal to propranmer that he/she construct a Disk object with an invalid argurnent So, for this project, if a programmer construct a disk with a size 0 or ls, you simply construct a disk with size I .Accessor Methods: -public int getsize) returns the size of the disk in integer -public String toString) returns the string presentation of a disk. The String rep resentation of a disk of size is a String starting with n aDiskChar, folwed by the aPoleCha, and followed by another aDiskChar Forexample, the follwing code Hlippet Disk aDisk new Disk (51 x', 'M'); System.out print (aDisk); should produce output as follows: For another example, the following code snippet Diak aDisk new Disk (3) System.out print (aDisk) should produce output as follows: Instance Variables: Obviously, you need a variable of type int to store the size of the disk. Having a string representation of the disk ready to be returned when a user call the toString method is also a good idea. So, y are going to have at least two instance variables: private int diskSize; private String diskString: Note that the diskString should be constructed in the constructor. You can also have more instance variables if you want to A test program (DiskTeater.class)provided. Simply compile your Disk.java and make sure that DiskTester.class is located in the same directory as your Disk.class. Then simply run java DiskTester Part II: The Pole Class (20 Points) The main characteristic of a pole is that it has an ability to hold a mber of disks. A disk can be placed to a pole and can be removed from a pole. Unfortunately, a pole has its limitation. Once you construct a pole, it has finite width and height. In other words, it can only hold a finite number of disks which cannot be chaned. Some disks may be too big to be placed into a pole as l. A pole can also tell you how many disks are there currently in the pole. From this observation, the Pole class should have the following: Constructors: -public Pole(int aMaxNumberOfDisk, int aMaxDiskSize, char aPoleChar): constructs a pole where the maximum mimber of disks i aMaxNumberOfDisk, the maximum disk size that it can hold is aMaxDiskSize, and the pole character is aPoleChar -public Pole(int aMaxNumberOfDiak, int aMaxDiakSize): constructs a pole where aMaxNumberOfDisk is the naxinnnn nurnber of disks, aMaxDiskSize s the maxirmum disk size, and the pole character is the vertical bar character public Pole(int aMaxNumberOfDiak) constructs a pole with where the maximum number of disks and the maximum disk size is aMaxNumberofDisk and the pole character is the vertical bar character -Unusual Situations: Obviously, if aMaxNumberfDisk is less than 1, it should be set to . Similarly, if aMaxDiskSize is less than , should be et to 1 - Accessor Methods: - public int getMaxNumberOfDiaka): retuns the maximum umber of disks that this pole can hold - public int getMaxDiakSize): returns the maximum disk size that it can hold public int getNumberOfDisks: returns the current number of disks that is currently holding -public Disk peekTopDiskO: retu the reference to the disk that is on the top of the pole without physically remove the disk out of the poe. Note that if the pole has no disk, this method should return the null reference. It is the user's responsibility to check that te retu value is not equal to null before he/she tries to use it. Users can also check that the umber of disks on this pole is not 0 (getNumberOfDiaks()) and call the peekTopDisk) method. public String toStringO: returns a string representation of the pole together with disks currently on the pole For example, consider the following code snippet (the addDiak) method will be discussed later): Pole aPole new Pole(5) aPole.addDisk(new Disk(S); aPole. addDisk (new Disk (4)) aPole.addDisk(ne Disk1); System. out println (aPole); The output of the code snippet should be 4 Note that the above string is actually the string without double quotations. In the above string, there are the total of 7 lnes. The first line (at the top) and the last line (at the bottom are for decoration. They represent the tip of the pole and the base of the pole, The five es it iddle are for the maximum of five disks. Recall from the code nippet that we constructed this pole with the argument 5 which is the maxiu number of disks and the maximum disk size that this pole can hold. Note that every line actually ends with the character In (newlne character) exoept for the last ine. Every line contains exactly the same number of characters. In the above case, the be of characters in each line is 1 (excluding the newline character) Five characters (maximm disk sixe) on both sides of the center pole character (vertical bar character). By using the sameber of characters in each line, it will simplify an implementation of a method of another class that we will discuss later. Part III The TowerOfHanoi Class (30 Points) Again, we are going to think about an object of type TouerofHanoi as the actual Tower of Hanoi puzzle shown above. A Tower of Hanoi puzzle consists of three poles and 7 dkika (size 7 to size 1) neatly stack from bottom to top in that order For our Tower of Hanoi, we are going to make it a little bit more flexible. In other words, you do not have to always play the Tower of Hanoi with 7 disks. You can choose to play with any number of disks (greater than or equal to 1) Given a Tower of Hanoi with a number of disks, a player can do the following: Move the top disk from one pole to another (or even put it back on the same pole) . Chock the disk that is currently on top of a given pole - Get the number of disks currently on a given pole We are also going to make this class easier to use. When there are a number of poles, most people generally refer to each pole as pole number 1, pole 2, and pole number 3. Unlike computer everything starts at 0. So, we are going to let usersue pole, 1, 2, or 3 nstead of 0, 1, or 2. Note that with a physical Tower of Hanoi object, nothing prevent a person to actually put a larger disk on top of the smaller one. We will enforce this rule when we create the actual game Again, of the above observations, the TowerOfHanoi class should have the following: e Constructors: - TouerOfHanoi ): This is the default constructor that contructs a Tower of Hanoi objects with 7 disks where all disks are neatly stacked (according to the puzzle in the pole number 1 (left-ost po). Note that the maximu umber of disks of each pole should be 7 and the maximu disk size of each pole should also be 7 ToverOfHanoi (int aNumberOfDiska): This constructs a Tower of Hanoi objects where the numbmr of disks is aNumberOfDisks. This will allow user to play the Tower of Hanoi puzzle with smaller or larger than 7 disks. Similarly, the maximumber of disks and the maximum disk size of each pole should be aNumberOfDisks. Note that the number of disks must be greater than or equal to 1. If not, just set it to .Accessor Methods: -public Diak peekTopDisk (int aPoleNumber): This method returns the reference to the top disk of a given pole. Note that if the given pole number is ivalid (not 1, 2, or 3), this method should retur the null reference. Similarly, if the given pole contains no disk, a null should be returned. It is the user's responsibility to check the return value. -public int getNumberOfDiska (int aPoleNumber): This method returns the number of disks currently on a given pole. Note that if the pole number if invalid (not 1, 2, or 3),1 should be returned. -public String toString(): This method returns the string representation of a Tower of Hanoi puzzle. For example, the following code snippet ToverOflanoi toh- neu Toerfllanoi) System.out println(toh); should give you the following output * sI 1 9l 9044144 Note that there are mber( 2, ad 3) indicating pole umber at the top of each pole. The uberof characters in each line (excludigth ewline character) is 47 (15 +115 15) where 15 is 7+1 +7 where 7 is the maxi disk size of each pole (constructed using the default constructor) and 1 is the pole character. The other two (+1s) i 1515 1 15 is a space between two pols. Not the base uses - instead of a space For this project, you just use strings returning from the toString) methods of each pole to enerate the above string. This is one of the main requirements of this class that the above string must be constructed from strings from poles Note that by simply concatenate all strings returning from the toString methods of all three poles will not give you the above string. You may eed to use the splitO method (see String API) using n as the argument togother with array of strings to achieve the result Here is another example from the following code snippet ToverOf Hanoi toh -ne ToerOfHanoi); toh.move(1, 2); toh.move(1, 3) toh.move (1, 2) System.out printin(toh); s1 The goal is to move all 4 disks from pole 1 to pole 3 The least number of moves for 4 disks is 15 Are you ready to play? (y): If the user enter n, simply go back to the main menu (step 1). If the user enter something else other than y o n, ak again Once user answer y, show the current puzzle together with the number of moes that the user has been used so far (0 at the beginning) and ask the user to enter two numbers to move the top disk from one pole to another as shown below 1 4s1ss Nunber of Moves: 0 Enter to move a disk: where is a pole number (1, 2, or 3) and is a pole mbe If a user enter 0 o fro>i0 and is also 0), simply go back to the main mem, If user enter 3 (move the top disk from the pole number 1 to the pole number 3), your program should show the curent puxzle, ber of moves, and ask again as shown below Tower of Hanoi (Modified from Wiki) The Tower of Hanoi is a mathematical game or puzle. It consists of three poles and a nmberof disks of different sizes, which can slide onto any pole. The puzle starts with the disks in a neat stack in ascending order of size on pole mmber 1 (leftmost pole), the smallest at the top, thus making a conical shape The objoctive of the puxzle is to move the entire stack to another pole, obeying the following simple rules: 1. Only one disk can be moved at a tie 2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty pole 3. No larger dik may be placed on top of a aler disk. With 3 disks, the puzzle can be solved in7oves. The minimal number of moves required to solve a Tower of Hanoi puzzl is 2-1, where n is te number of disks. The following is an example of a Tower of Hanoi puzzle: The final goal of this project is for you to create a Tower of Hanoi puxxe game with any number of disks so that a user can play on his/her coputr. No worry, it is going to be a text mode puxale Not a graphic one, The main goal is for you to practice Object-Oriented Programing. We are going to look at a Tower of Hanoi puzzle as a physical object and try to write our program using it as a guideline. For now, let's look at the Tower of Hanoi puzzle shown earlier and see what physical objects are in the picture Disks: There are a mber of disks with different sies. Poles: There are three poles. . The Puxzle: The whole puzzle (Tower of Hanoi) tself that consists of a umber of disks and three poles So, we are going to implement our program base on the above observation. Note that we are going to simulate the situation where your classes may be used by other programmers, In other words, you need to protect your class in case of any unusual situations. These situations will be discussed in each class. Part I: The Disk Class (10 Points) A most important characteristice of a disk itz Recall that in a Tower of Hanoi puxxle, there are a number of disks but none of them have the same size. Because of this, the size should be a part of its constructor to constrct a disk with a given size The other noticeable characteristic of a disk is that, once you construct a disk, you cannot change its size. You eed to destroy it or sand it (which is the same as reconstruct it). So, the Diak class will be what we usually call an immutable class. A use cannot change the data inside the object of type Disk once it has been constructed. However, users can still read data out of it. So, from the above observations, the Diak clas should have the following: Constructors: - public Disk(int aDiakSize, char aDiskChar, char aPoleChar): This costructor allows a user to construct a disk with any size greater than . The aDiskChar ad aPoleChar parameters will be used to generate the String representation of an object of type Disk. This i be discussed later -public Disk(int aDiskSize): This constructor constructs a disk with a give size and use the asterisk character ) as the disk character and use the vertical bar character (1) as the pole character -Unusual Situations: A disk cannot have size 0 or less. However, since your Disk class may be used by another programmer, you have to protect your clas. There is nothing to prevent a programmer to construct a disk with a negative number or zeo for the disk size as shown below: Disk aDisk neu Disk(-4, '''1D Note that a constructor cannot retur a value. So, th is o way for your class to signal to propranmer that he/she construct a Disk object with an invalid argurnent So, for this project, if a programmer construct a disk with a size 0 or ls, you simply construct a disk with size I .Accessor Methods: -public int getsize) returns the size of the disk in integer -public String toString) returns the string presentation of a disk. The String rep resentation of a disk of size is a String starting with n aDiskChar, folwed by the aPoleCha, and followed by another aDiskChar Forexample, the follwing code Hlippet Disk aDisk new Disk (51 x', 'M'); System.out print (aDisk); should produce output as follows: For another example, the following code snippet Diak aDisk new Disk (3) System.out print (aDisk) should produce output as follows: Instance Variables: Obviously, you need a variable of type int to store the size of the disk. Having a string representation of the disk ready to be returned when a user call the toString method is also a good idea. So, y are going to have at least two instance variables: private int diskSize; private String diskString: Note that the diskString should be constructed in the constructor. You can also have more instance variables if you want to A test program (DiskTeater.class)provided. Simply compile your Disk.java and make sure that DiskTester.class is located in the same directory as your Disk.class. Then simply run java DiskTester Part II: The Pole Class (20 Points) The main characteristic of a pole is that it has an ability to hold a mber of disks. A disk can be placed to a pole and can be removed from a pole. Unfortunately, a pole has its limitation. Once you construct a pole, it has finite width and height. In other words, it can only hold a finite number of disks which cannot be chaned. Some disks may be too big to be placed into a pole as l. A pole can also tell you how many disks are there currently in the pole. From this observation, the Pole class should have the following: Constructors: -public Pole(int aMaxNumberOfDisk, int aMaxDiskSize, char aPoleChar): constructs a pole where the maximum mimber of disks i aMaxNumberOfDisk, the maximum disk size that it can hold is aMaxDiskSize, and the pole character is aPoleChar -public Pole(int aMaxNumberOfDiak, int aMaxDiakSize): constructs a pole where aMaxNumberOfDisk is the naxinnnn nurnber of disks, aMaxDiskSize s the maxirmum disk size, and the pole character is the vertical bar character public Pole(int aMaxNumberOfDiak) constructs a pole with where the maximum number of disks and the maximum disk size is aMaxNumberofDisk and the pole character is the vertical bar character -Unusual Situations: Obviously, if aMaxNumberfDisk is less than 1, it should be set to . Similarly, if aMaxDiskSize is less than , should be et to 1 - Accessor Methods: - public int getMaxNumberOfDiaka): retuns the maximum umber of disks that this pole can hold - public int getMaxDiakSize): returns the maximum disk size that it can hold public int getNumberOfDisks: returns the current number of disks that is currently holding -public Disk peekTopDiskO: retu the reference to the disk that is on the top of the pole without physically remove the disk out of the poe. Note that if the pole has no disk, this method should return the null reference. It is the user's responsibility to check that te retu value is not equal to null before he/she tries to use it. Users can also check that the umber of disks on this pole is not 0 (getNumberOfDiaks()) and call the peekTopDisk) method. public String toStringO: returns a string representation of the pole together with disks currently on the pole For example, consider the following code snippet (the addDiak) method will be discussed later): Pole aPole new Pole(5) aPole.addDisk(new Disk(S); aPole. addDisk (new Disk (4)) aPole.addDisk(ne Disk1); System. out println (aPole); The output of the code snippet should be 4 Note that the above string is actually the string without double quotations. In the above string, there are the total of 7 lnes. The first line (at the top) and the last line (at the bottom are for decoration. They represent the tip of the pole and the base of the pole, The five es it iddle are for the maximum of five disks. Recall from the code nippet that we constructed this pole with the argument 5 which is the maxiu number of disks and the maximum disk size that this pole can hold. Note that every line actually ends with the character In (newlne character) exoept for the last ine. Every line contains exactly the same number of characters. In the above case, the be of characters in each line is 1 (excluding the newline character) Five characters (maximm disk sixe) on both sides of the center pole character (vertical bar character). By using the sameber of characters in each line, it will simplify an implementation of a method of another class that we will discuss later. Part III The TowerOfHanoi Class (30 Points) Again, we are going to think about an object of type TouerofHanoi as the actual Tower of Hanoi puzzle shown above. A Tower of Hanoi puzzle consists of three poles and 7 dkika (size 7 to size 1) neatly stack from bottom to top in that order For our Tower of Hanoi, we are going to make it a little bit more flexible. In other words, you do not have to always play the Tower of Hanoi with 7 disks. You can choose to play with any number of disks (greater than or equal to 1) Given a Tower of Hanoi with a number of disks, a player can do the following: Move the top disk from one pole to another (or even put it back on the same pole) . Chock the disk that is currently on top of a given pole - Get the number of disks currently on a given pole We are also going to make this class easier to use. When there are a number of poles, most people generally refer to each pole as pole number 1, pole 2, and pole number 3. Unlike computer everything starts at 0. So, we are going to let usersue pole, 1, 2, or 3 nstead of 0, 1, or 2. Note that with a physical Tower of Hanoi object, nothing prevent a person to actually put a larger disk on top of the smaller one. We will enforce this rule when we create the actual game Again, of the above observations, the TowerOfHanoi class should have the following: e Constructors: - TouerOfHanoi ): This is the default constructor that contructs a Tower of Hanoi objects with 7 disks where all disks are neatly stacked (according to the puzzle in the pole number 1 (left-ost po). Note that the maximu umber of disks of each pole should be 7 and the maximu disk size of each pole should also be 7 ToverOfHanoi (int aNumberOfDiska): This constructs a Tower of Hanoi objects where the numbmr of disks is aNumberOfDisks. This will allow user to play the Tower of Hanoi puzzle with smaller or larger than 7 disks. Similarly, the maximumber of disks and the maximum disk size of each pole should be aNumberOfDisks. Note that the number of disks must be greater than or equal to 1. If not, just set it to .Accessor Methods: -public Diak peekTopDisk (int aPoleNumber): This method returns the reference to the top disk of a given pole. Note that if the given pole number is ivalid (not 1, 2, or 3), this method should retur the null reference. Similarly, if the given pole contains no disk, a null should be returned. It is the user's responsibility to check the return value. -public int getNumberOfDiska (int aPoleNumber): This method returns the number of disks currently on a given pole. Note that if the pole number if invalid (not 1, 2, or 3),1 should be returned. -public String toString(): This method returns the string representation of a Tower of Hanoi puzzle. For example, the following code snippet ToverOflanoi toh- neu Toerfllanoi) System.out println(toh); should give you the following output * sI 1 9l 9044144 Note that there are mber( 2, ad 3) indicating pole umber at the top of each pole. The uberof characters in each line (excludigth ewline character) is 47 (15 +115 15) where 15 is 7+1 +7 where 7 is the maxi disk size of each pole (constructed using the default constructor) and 1 is the pole character. The other two (+1s) i 1515 1 15 is a space between two pols. Not the base uses - instead of a space For this project, you just use strings returning from the toString) methods of each pole to enerate the above string. This is one of the main requirements of this class that the above string must be constructed from strings from poles Note that by simply concatenate all strings returning from the toString methods of all three poles will not give you the above string. You may eed to use the splitO method (see String API) using n as the argument togother with array of strings to achieve the result Here is another example from the following code snippet ToverOf Hanoi toh -ne ToerOfHanoi); toh.move(1, 2); toh.move(1, 3) toh.move (1, 2) System.out printin(toh); s1 The goal is to move all 4 disks from pole 1 to pole 3 The least number of moves for 4 disks is 15 Are you ready to play? (y): If the user enter n, simply go back to the main menu (step 1). If the user enter something else other than y o n, ak again Once user answer y, show the current puzzle together with the number of moes that the user has been used so far (0 at the beginning) and ask the user to enter two numbers to move the top disk from one pole to another as shown below 1 4s1ss Nunber of Moves: 0 Enter to move a disk: where is a pole number (1, 2, or 3) and is a pole mbe If a user enter 0 o fro>i0 and is also 0), simply go back to the main mem, If user enter 3 (move the top disk from the pole number 1 to the pole number 3), your program should show the curent puxzle, ber of moves, and ask again as shown below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions