You may write your program in either Java, C++, or C#. No other languages will b
ID: 3549304 • Letter: Y
Question
You may write your program in either Java, C++, or C#. No other languages will be accepted.
please go by the instructions given and making sure each step is outputed as in the in the instructions.
The Towers of Hanoi puzzIe has n disks of different sizes that can slide onto any of three pegs. Initially, alI the disks a re on the first peg in order of size, the largest on the bottom and the smallest on top. The goal is to move all the disks to the third peg, using the second one as an auxiliary, if necessary. We can move only one disk at a time, and it is forbidden to place a larger disk on top of a smaller one. Sample solution: Write a pro gram that solves the Towers of Hanoi problem using recursion. (No hard-coding!) The user must be able to specify the number of starting disks (1Explanation / Answer
JAVA CODE import java.io.*; public class TowersOfHanoi { static int moves = 0; static int totalDisks = 0; public static void main(String[] arguments) throws java.io.IOException { int disks; char fromPole = 'A'; char withPole = 'B'; char toPole = 'C'; disks = getNumber(" How many disks are there on the tower? "); totalDisks = disks; if(totalDisks > 10){ System.out.println("Working..."); } FileOutputStream fos = new FileOutputStream("TowersOfHanoiSolution.txt"); PrintStream ps = new PrintStream(fos); solveHanoi(disks, fromPole, toPole, withPole, ps); ps.close(); System.out.println(); System.out.println(" Amount of moves: " + moves + " "); System.out.print("You can now access the TowersOfHanoiSolution.txt file"); System.out.println(" which is in the same directory as the .class file of this program."); } static void solveHanoi(int disks, char fromPole, char toPole, char withPole, PrintStream ps) { if (disks >= 1) { solveHanoi(disks-1, fromPole, withPole, toPole, ps); moveDisk(fromPole, toPole, ps); solveHanoi(disks-1, withPole, toPole, fromPole, ps); } } static void moveDisk(char fromPole, char toPole, PrintStream ps) { moves++; if(totalDisksRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.