This is a java problem. This is the method provided in the class. thank you LAB
ID: 3887515 • Letter: T
Question
This is a java problem.
This is the method provided in the class. thank you
LAB 1a: Towers of Hanoi Directions [ Towers.java ] 10 points. Please write the Java source code solveTowers) method given in class. The method should be written as it was provided in class, and be included inside a class you provide. The class should be named Towers and should have public static void main) method. The main) method should be used to pass the values of the parameters needed to solveTowers0, and should end after the solveTowers0 method has completed. You may use hardcoded values f the variables in main) that are passed as parameters to solveTowers) or you may take user input for the values in main0 that are passed as parameter to solveTowers) Once you have your source code working, try running the program for values of the Towers of Hanoi game that has more than 5 disks. You should try running the program for at least five different numbers of disks. Watch carefully to determine how the recursive calls are being executed for each of the five different numbers of disks you choose.Explanation / Answer
Please find my implementation.
public class TowersOfHano {
// Assuming n-th disk is bottom disk (count down)
static void solveTowers(int n, char sourcePole, char destinationPole, char auxiliaryPole)
{
// Base case (termination condition)
if(0 == n)
return;
// Move first n-1 disks from source pole
// to auxiliary pole using destination as
// temporary pole
solveTowers(n-1, sourcePole, auxiliaryPole,destinationPole);
// Move the remaining disk from source pole to destination pole
System.out.println("Move the disk "+n+" from "+sourcePole+" to "+destinationPole+" ");
// Move the n-1 disks from auxiliary (now source)
// pole to destination pole using source pole as
// temporary (auxiliary) pole
solveTowers(n-1, auxiliaryPole, destinationPole,sourcePole);
}
public static void main(String[] args) {
solveTowers(3, 'S', 'D', 'A');
}
}
/*
Sample run:
Move the disk 1 from S to D
Move the disk 2 from S to A
Move the disk 1 from D to A
Move the disk 3 from S to D
Move the disk 1 from A to S
Move the disk 2 from A to D
Move the disk 1 from S to D
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.