The objective of the assignment is to model the puzzle Towers of Hanoi using Obj
ID: 3776227 • Letter: T
Question
The objective of the assignment is to model the puzzle Towers of Hanoi using Object Oriented Programming (Java) and recursion.
There have to be 3 classes:
Disk.java
- Fields: An integer representing the size (width) of the disk
- Methods: A get and set method for the size of the disk
- Constructor: A constructor is called when creating a new instance of the Disk.
Rod.java
- Fields: A collection to store the disks
- Methods: getDisk() and addDisk() methods to return the top disk from the rod and add a disk to top of the rod. Also a method to return the contents of the rod toString().
- Constructor: Creates a rod with n number of disk
Hanoi.java
- Fields: The hanoi class should hold 3 rods
- Constructor: It creates a rod with n number of disks accepted from the user
- Methods: printStatus() to show the contents of the game status after every move. move() to move disk from one rod to another and recursiveTOH(), which will find the solution to the Towers of Hanoi puzzle recursively.
The program should start by asking the user to enter the number of disks on the source rod. For every move show the status of each of the rod and which disk were moved from one rod to another.
*Make sure this puzzle is solved with classes and interesection*
Explanation / Answer
program
package DataStructures.StacksQueues;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Stack;
public class TowersOfHanoi
{
public static void main(String[] args)
{
TowersHanoi game = new TowersHanoi(3);
game.solve(true);
game.print();
}
public static class Disc
{
Tower tower1;
Tower tower2;
Tower tower3;
int numberDiscs;
HashMap<Tower, Integer> towerHash;
TowersHanoi(int numberOfDiscs)
{
numberDiscs = numberOfDiscs;
tower1 = new Tower();
tower2 = new Tower();
tower3 = new Tower();
towerHash = new HashMap<>();
towerHash.put(tower1, 0);
towerHash.put(tower2, 1);
towerHash.put(tower3, 2);
for(int counter = numberDiscs; counter >= 1; counter--)
{
tower1.placeDisk(counter);
}
}
public void solve(boolean showSteps)
{
int nDisks = numberDiscs;
hanoi(nDisks, tower1, tower3, showSteps);
}
int steps = 0;
public void hanoi(int nDiscs, Tower from, Tower to, boolean showSteps){
if(nDiscs == 1)
{
to.placeDisk(from.getDisc());
if(showSteps) print();
}
else
{
int helperTowerIndex = (6 - (towerHash.get(from) + 1) - (towerHash.get(to) + 1)) - 1;
for(Tower key: towerHash.keySet())
{
if(towerHash.get(key) == helperTowerIndex)
{
hanoi(nDiscs-1, from, key, showSteps);
to.placeDisk(from.getDisc());
if(showSteps) print();
hanoi(nDiscs-1, key, to, showSteps);
}
}
}
}
public void print()
{
System.out.println("Tower 1: BOTTOM " + tower1.printTower() + " TOP");
System.out.println("Tower 2: BOTTOM " + tower2.printTower() + " TOP");
System.out.println("Tower 3: BOTTOM " + tower3.printTower() + " TOP");
System.out.println(" ");
}
}
public static class Rod
{
Stack<Integer> towerContents;
Tower()
{
towerContents = new Stack<>();
}
public int getStackSize()
{
return towerContents.size();
}
public void placeDisk(int diskSize)
{
towerContents.push(diskSize);
}
public int getDisc()
{
return towerContents.pop();
}
public String printTower()
{
return Arrays.toString(towerContents.toArray());
}
public class Disc
{
int size;
Disc(int diskSize)
{
size = diskSize;
}
public int getSize()
{
return size;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.