Question

(IN JAVA) Legend says that this problem was originated either by Buddhist monks, Brahman priests, or...

(IN JAVA)

Legend says that this problem was originated either by Buddhist monks, Brahman priests, or the guardians of a Vietnamese temple, take your pick. In each case, they believed that if the puzzle could be solved using 64 disks, the world would end. Since the number of moves required to solve the Tower of Hanoi problem using “n” disks is 2n - 1, and assuming that each move would take roughly one second, then moving 64 disks from one peg to another would take almost 600 billion years to complete. (Astronomers tell us that universe as we know it today is no more than 14 billion years old, and the life of our sun is roughly 10 billion years, with 5 billion still to go.)

The most common algorithm to solve this problem involves multiple recursion. Here’s the pseudocode for such a method:

  • Invoke the method specifying these 4 parameters for this level of recursion:
    1. n (the number of disks to move)
    2. the from peg
    3. the to peg
    4. an intermediate peg as necessary
  • Base case: if n = 0, then just return (do nothing)
  • Recursive case:
    • Step 1: invoke method again (a new level of recursion) using parameters
      1. n – 1 (the number of disks to move)
      2. from the current from peg
      3. to the current intermediate peg
      4. using the current to peg as necessary
    • Step 2: move the nth peg from the current from peg to the current to peg
    • Step 3: invoke method again (a new level) using as parameters
      1. n – 1 (the number of disks to move)
      2. from the current intermediate peg
      3. to the current to peg
      4. using the current from peg as necessary

Write a method in Java that implements the above pseudocode. Show the moves required by adding a “print” statement in Step 2 of the recursive case showing the current fromand to pegs.

Write a “tester” program to display the moves required when n = 3, n = 4, and n = 5 (input from the user?). To make things easy, label the initial from peg A, the initial intermediatepeg B, and the initial to peg C.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
public class Tower
{
public static void TOH(int N,char P,char Q,char R) //function of tower of hanoi problem
   {
   if (N==0) //if n==0 return it
       return;
   else
       {
       TOH(N-1,P,R,Q); //recursive equation
       System.out.println("Move From " + P + " to " + Q); // printing move from which peg to which peg
       TOH(N-1,R,Q,P); // again invoking recursive function
       }
   }
public static void main(String args[])
   {
   while (true) //iterating loop until user enters the number which is less than or equal to 1
       {
       Scanner input=new Scanner(System.in);
       System.out.println("Enter the N value");
       int n=input.nextInt(); //input taking
       if (n<=1)
           break;   // if number less than or equal to 1 break
       TOH(n,'A','B','C'); //else call TOH() function
       }
   }
}

Code and Screenshot Ouptut:

Note : If you have any queries please post a comment thanks a lot....

Add a comment
Know the answer?
Add Answer to:
(IN JAVA) Legend says that this problem was originated either by Buddhist monks, Brahman priests, or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write...

    write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write a warruse function fibonacci(n) that calculatus 0, 1, 1, 2, 3, 5, 8, 13, 21,... (n) that calculates the n" Fib begins with the terms and 1 and has the property preceding terms. a) Write a cand unsigned long long int for i number. Use unsigned int for the function's paramete her that can be printed on your system. type. b) Determine the largest...

  • Please write a recursive Java program to solve the Tower of Hanoi game for n disks...

    Please write a recursive Java program to solve the Tower of Hanoi game for n disks on pole A. Please read the textbook page 176 – 180 to fully understand this game or puzzle. The game consists of n disks and three poles: A (the source), B (the destination), and C (the spare). Initially, all the disks are on pole A. The game is to move all disks (one by one) from pole A to pole B using pole C...

  • New answers only outcomes for this lab: Recursive concepts Uses for recursion Infinite recursion Problem solving...

    New answers only outcomes for this lab: Recursive concepts Uses for recursion Infinite recursion Problem solving with recursion Say My Number You will produce a short, elegant, recursive algorithm that spells out any number between 1 and 2.1 billion. Allow the user to input a number in the range from 1 to 2.1 billion (a positive 32-bit int), then spell out the number (e.g. 123456 would output "one hundred twenty three thousand four hundred fifty six"). Use a recursive method...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • Those are  JAVA Programs. Please write them in recursions , it requires the methods have to be...

    Those are  JAVA Programs. Please write them in recursions , it requires the methods have to be recursive(I understood how to write them by using for loop). Please don't use any syntax beyond chapter of Recursion. I only covered the materials to recursions and the materials before recursion chapter. Appreciated! #1) Write a recursive method writesquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending...

  • JAVA PROBLEM #1: The British have been shelling the Revolutionary forces with a large cannon from...

    JAVA PROBLEM #1: The British have been shelling the Revolutionary forces with a large cannon from within their camp. You have been assigned a dangerous reconnaissance mission -- to infiltrate the enemy camp and determine the amount of ammunition available for that cannon. Fortunately for you, the British (being relatively neat and orderly) have stacked the cannonballs into a single pyramid-shaped stack. At the top is a single cannonball resting on a square of 8 cannonballs, which is itself resting...

  • JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question...

    JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with  alternating signs.      // For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign.  ...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT