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

Hello, I am working through a TowersOfHanoiProblem. This is the recursive proble

ID: 3740661 • Letter: H

Question

Hello, I am working through a TowersOfHanoiProblem. This is the recursive problem, and I am trying to output the information into a txt file. I would also like to have the time displayed for how long my program took to run at the bottom of the text file. How could I go about doing this?

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

public class TowersOfHanoi {

  

   static //define numOfDisks to start with and label rods

   int numDisks;

   char startingRod;

   char moveRod;

   char emptyRod;

  

  

   static void TowersOfHanoiRecursive(int numDisks, char startingRod, char moveRod, char emptyRod) {

      

       if (numDisks == 1) {

           System.out.println("Move disk 1 from tower " + startingRod + " to tower " + moveRod);

          

          

       }

      

           TowersOfHanoiRecursive(numDisks -1, emptyRod, moveRod, startingRod);

           System.out.println("Move disk " + numDisks + " from tower " + startingRod + " to tower" + moveRod);

           TowersOfHanoiRecursive(numDisks-1, emptyRod, moveRod, startingRod);

      

   }

  

   public static void main (String args[]) throws IOException {

      

       PrintWriter out = new PrintWriter (new FileWriter("K:\Desktop\RecursiveTowers.txt"));

       int numDisks = 4;

       out.println(TowersOfHanoiRecursive(numDisks, 'A', 'C', 'B'));

       out.close();

  

      

   }

}

Explanation / Answer

import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; public class TowersOfHanoi { //define numOfDisks to start with and label rods static PrintWriter out; public static void TowersOfHanoiRecursive(int numDisks, char startingRod, char moveRod, char emptyRod) { if (numDisks == 1) { out.println("Move disk 1 from tower " + startingRod + " to tower " + moveRod); return; } TowersOfHanoiRecursive(numDisks - 1, startingRod, emptyRod, moveRod); TowersOfHanoiRecursive(1, startingRod, moveRod, emptyRod); TowersOfHanoiRecursive(numDisks - 1, emptyRod, moveRod, startingRod); } public static void main(String args[]) throws IOException { out = new PrintWriter(new FileWriter("RecursiveTowers.txt")); int numDisks = 3; long start = System.nanoTime(); TowersOfHanoiRecursive(numDisks, 'A', 'C', 'B'); long end = System.nanoTime(); out.println("Total time : " + (end - start) + " nano seconds"); out.close(); } } Move disk 1 from tower A to tower C Move disk 1 from tower A to tower B Move disk 1 from tower C to tower B Move disk 1 from tower A to tower C Move disk 1 from tower B to tower A Move disk 1 from tower B to tower C Move disk 1 from tower A to tower C Total time : 191182 nano seconds Please do asks me if you have any doubts, I ll ping you as soon as possible

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote