Question

In: Computer Science

In Java please Consider the following problem: There are three pegs, denoted A, B, and C,...

In Java please

Consider the following problem: There are three pegs, denoted A, B, and C, and N disks of different sizes. Originally, all the disks are on peg A, stacked in decreasing size from bottom to top. Your goal is to transfer all the disks to peg B, and the rules are that you can only move one disk at a time, no disk can be moved onto a smaller one, and you make use of all pegs.

Develop a solution to this problem based on the principle of recursion, i.e., this problem has to be solved by programming a recursive procedure. The use of global variables is not permitted. Your solution must print a protocol about the movements of the disks. Use only pseudocode to write down your solution.

Solutions

Expert Solution

Pseudocode :

//recursive function to solve a problem
static void solution(int n, char from_peg, char to_peg, char aux_peg)
{
if (n == 1)
{
System.out.println("Move disk 1 from peg " + from_peg + " to peg " + to_peg);
return;
}
solution(n-1, from_peg, aux_peg, to_peg);
System.out.println("Move disk " + n + " from peg " + from_peg + " to peg " + to_peg);
solution(n-1, aux_peg, to_peg, from_peg);
}

Approach :

Remove all N-1 disks to other pegs as per requirement and then set N number disk to peg B and again perform same process for N-1 disks.

Java Code is as below :

import java.util.*;
public class Main
{
    //recursive function to solve a problem
    static void solution(int n, char from_peg, char to_peg, char aux_peg) 
    { 
        if (n == 1) 
        { 
            System.out.println("Move disk 1 from peg " +  from_peg + " to peg " + to_peg); 
            return; 
        } 
        solution(n-1, from_peg, aux_peg, to_peg); 
        System.out.println("Move disk " + n + " from peg " +  from_peg + " to peg " + to_peg); 
        solution(n-1, aux_peg, to_peg, from_peg); 
    } 

    public static void main(String args[]) 
    { 
        //number of disks 
        int N = 3; 
        
        // A, B and C are names of pegs
        solution(N, 'A', 'B', 'C');  
    } 
}

Please refer to the screenshot of the code to understand the indentation of the code.

output is also attached in the above screenshot for 3 disks.


Related Solutions

Consider a class with 3 sections denoted as A, B, C. Let the number of students...
Consider a class with 3 sections denoted as A, B, C. Let the number of students in section A be 10, in section B be 20, and in section C be 15. Let the score of uniformly chosen student be denoted by random variable X. The student’s section be denoted by the random variable Y where Y = 1 if the student is from section A, Y = 2 if the student is from section B and Y = 3...
Consider the following information for three stocks, Stocks A, B, and C. The returns on the...
Consider the following information for three stocks, Stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (That is, each of the correlation coefficients is between 0 and 1.) Stock Expected Return Standard Deviation Beta A 7.94 16 0.7 B 9.62 16 1.1 C 11.72 16 1.6 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 5%, and the market is...
Consider the following information for Stocks A, B, and C. The returns on the three stocks...
Consider the following information for Stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (that is, each of the correlation coefficients is between 0 and 1). Stock Expected Return Standard Deviation Beta A 9.55% 15.00% 0.9 B 10.45% 15.00% 1.1 C 12.70% 15.00% 1.6 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 5.5%, and the market is in equilibrium....
Consider the following information for stocks A, B, and C. The returns on the three stocks...
Consider the following information for stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (That is, each of the correlation coefficients is between 0 and 1.) Stock Expected Return Standard Deviation Beta A 9.35% 15% 0.7 B 12.65    15    1.3 C 14.85    15    1.7 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 5.5%, and the market is in equilibrium....
Consider the following information for stocks A, B, and C. The returns on the three stocks...
Consider the following information for stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (That is, each of the correlation coefficients is between 0 and 1.) Stock Expected Return Standard Deviation Beta A 8.65% 16% 0.7 B 10.45    16    1.1 C 12.25    16    1.5 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 5.5%, and the market is in equilibrium....
Consider the following information for stocks A, B, and C. The returns on the three stocks...
Consider the following information for stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (That is, each of the correlation coefficients is between 0 and 1.) Stock Expected Return Standard Deviation Beta A 8.75% 14% 0.9 B 9.75    14    1.3 C 10.75    14    1.7 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 6.5%, and the market is in equilibrium....
Consider the following information for three stocks, Stocks A, B, and C. The returns on the...
Consider the following information for three stocks, Stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (That is, each of the correlation coefficients is between 0 and 1.) Stock Expected Return Standard Deviation Beta A 9.34 % 14 % 0.8 B 11.26 14 1.2 C 13.18 14 1.6 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 5.5%, and the...
Consider the following information for stocks A, B, and C. The returns on the three stocks...
Consider the following information for stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (That is, each of the correlation coefficients is between 0 and 1.) Stock Expected Return Standard Deviation Beta A 9.69% 14% 0.9 B 10.92    14    1.2 C 12.56    14    1.6 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 6%, and the market is in equilibrium....
Consider the following information for three stocks, Stocks A, B, and C. The returns on the...
Consider the following information for three stocks, Stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (That is, each of the correlation coefficients is between 0 and 1.) Stock Expected Return Standard Deviation Beta A 8.01 % 15 % 0.7 B 10.16 15 1.2 C 11.88 15 1.6 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 5%, and the...
Consider the following information for three stocks, Stocks A, B, and C. The returns on the...
Consider the following information for three stocks, Stocks A, B, and C. The returns on the three stocks are positively correlated, but they are not perfectly correlated. (That is, each of the correlation coefficients is between 0 and 1.) Stock Expected Return Standard Deviation Beta A 8.01 % 15 % 0.7 B 10.16 15 1.2 C 11.88 15 1.6 Fund P has one-third of its funds invested in each of the three stocks. The risk-free rate is 5%, and the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT