Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Make sure this puzzle is solved with the classes mentioned below and interesect

ID: 3776287 • Letter: #

Question

*Make sure this puzzle is solved with the classes mentioned below and interesection*

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

import java.util.Scanner;
import java.util.Stack;

class Disk {

int diskSize;

public Disk(int diskSize) {
this.diskSize = diskSize;
}

public int getDiskSize() {
return diskSize;
}

public void setDiskSize(int diskSize) {
this.diskSize = diskSize;
}

}

class Rod {

Stack<Disk> listOfDisk = new Stack<>();

public Rod(int n) {
for (int i = 0; i < n; i++) {
this.listOfDisk.add(new Disk(i+1));
}
}

public Disk getDisk() {
return listOfDisk.pop();
}

public void addDisk(Disk listOfDisk) {
this.listOfDisk.push(listOfDisk);
}

@Override
public String toString() {
String rod = "[";
for (Disk s : listOfDisk) {
rod += s.getDiskSize() + ",";
}
return rod + "]";
}
}

public class Hanoi {

Rod rod1;
Rod rod2;
Rod rod3;

/**
*
* @param numberOfdisk
*/
public Hanoi(int numberOfdisk) {

rod1 = new Rod(numberOfdisk);
rod2 = new Rod(0);
rod3 = new Rod(0);
}

public void display() {
System.out.println("rod 1" + rod1.toString());
System.out.println("rod 2" + rod2.toString());
System.out.println("rod 3" + rod3.toString());
}

/**
*
* @param n
* @param a
* @param b
* @param c
*/
public void move(int n, Rod a, Rod b, Rod c) {
if (n > 0) {
move(n - 1, a, c, b);
Disk d = a.getDisk();
c.addDisk(d);
display();
move(n - 1, b, a, c);
}
}

/**
*
* @param n
*/
public void recursiveTOH() {
display();
move(rod1.listOfDisk.size(), rod1, rod2, rod3);
}

public static void main(String[] args) {
  
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number Of Disk:");
int n = sc.nextInt();

Hanoi h = new Hanoi(n);

h.recursiveTOH();
}
}