1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
qaws [65]
2 years ago
11

Write a recursive method named hanoi that prints a solution to the classic Towers of Hanoi puzzle. Your method should accept thr

ee integer parameters representing the number of disks, the starting page number, and ending peg number. Your method should print the solution to the game to move from the given start peg to the given end peg. For example, the call of hanoi(3, 1, 3); should print the following output to move the three pegs from peg
Computers and Technology
1 answer:
dimaraw [331]2 years ago
3 0

Answer:

Explanation:

The following recursive method is written in Java. It creates a simulation of the towers of Hanoi and outputs every step-by-step instruction on how to solve it. The output for the code can be seen in the attached picture below.

class Brainly {

   public static String hanoi(int numOfDisks, int startPeg, int endPeg) {

       int helpPeg;

       String sol1, sol2, MyStep, mysol;   // Contains moves

       if (numOfDisks == 1) {

           return "Move from " + startPeg + " to " + endPeg + "\n";

       } else {

           helpPeg = 6 - startPeg - endPeg;    // Because startPeg + helpPeg + endPeg = 6

           sol1 = hanoi(numOfDisks - 1, startPeg, helpPeg);

           MyStep = "Move from " + startPeg + " to " + endPeg + "\n";

           sol2 = hanoi(numOfDisks - 1, helpPeg, endPeg);

           mysol = sol1 + MyStep + sol2;     // + = String concatenation !

           return mysol;

       }

   }

   public static void main(String[] args) {

      String output = hanoi(3, 1, 3);

       System.out.println(output);

   }

}

You might be interested in
Write a function named replace_at_index that takes a string and an integer. The function should return a new string that is the
aalyn [17]

Answer:

def replace_at_index(str, number):

   new = str.replace(str[number], "-")

   return new

print(replace_at_index("eggplant", 3))

Explanation:

- Create a function called <em>replace_at_index</em> that takes a string and an integer

- Initialize a new variable called <em>new</em>, that will hold the new string

- Replace the character at given index with dash using <em>replace</em> function, it takes two parameters: the first is the character we want to replace, the second is the new character.

- Return the new string

- Call the function with the required inputs

4 0
2 years ago
Persons who have been given access to an installation can be counted on to be of no threat. true or false? (antiterrorism scenar
aliya0001 [1]
<span>The statement that "Persons who have been given access to an installation can be counted on to be of no threat" is false. If they are not honest enough, they could become the dangerous staff the company could ever had because he or she has access to everything, files, information, etc. That is why, in selecting a person to be trusted with such responsibility, he or she undergoes an intensive training.</span>
4 0
3 years ago
BRAINLIEST Which function will add a name to a list of baseball players in Python?
Tju [1.3M]

Answer: Append()

Explanation:

8 0
3 years ago
Read 2 more answers
Software is the brain of computer. Explain this starement
Annette [7]

here you go

i hope this helps you

5 0
3 years ago
Someone help me out eh?
Elodia [21]

Line 4

It should be font-size: 15px

Also there should be curly braces in line 5.

6 0
2 years ago
Read 2 more answers
Other questions:
  • How do open online courses help with independent learning? (1 point)
    13·2 answers
  • Consider the following implementation of a class Square:
    12·1 answer
  • Which of the following statements is used to terminate the program when closing the frame?
    5·1 answer
  • 1. ___________ ensures the integrity and security of data that are passing over a network.
    13·1 answer
  • Let X and Y be two decision problems. Suppose we know that X reduces to Yin polynomial time. Which of the following statements a
    14·1 answer
  • Write a program that has the user input how many classes they are taking this semester and then output how many hours they will
    8·2 answers
  • 4.2 lesson practice help plzs
    6·1 answer
  • QUICK HELP ME PLEASE
    9·1 answer
  • 1.What is the output of the following program? [10 Marks]namespace ConsoleApp1{class Program{static void Main(string[] args){int
    13·1 answer
  • In the bremmer article, the author states that _________ translates into greater risks.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!