Question

In: Computer Science

I have a recursive Tower of Hanoi program and would like to get the index numbers...

I have a recursive Tower of Hanoi program and would like to get the index numbers to be in correct sequence: 1,2,3,4,5,6,7. How can I do this?

Main.java

import java.io.*;

import java.util.List;

import java.util.Scanner;

public class Main {

public static void main(String[] args){

/**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole C without ever putting a larger disk on top of a smaller disk.*/

System.out.println("\nW7L1 Tower of Hanoi");

System.out.println("Program by Quang Pham\n");

char startingrod = ' ';

char endingrod = ' ';

Scanner sc = new Scanner(System.in);

System.out.println("Enter the starting rod letter (A, B, or C).");

startingrod = sc.next().charAt(0);

System.out.println("Enter the ending rod letter (A, B, C).");

endingrod = sc.next().charAt(0);

if (startingrod == endingrod)

{

System.out.println("Sorry. Starting rod and ending rod cannot be the same.");

System.out.println("Enter ending rod letter (A, B, C)");

endingrod = sc.next().charAt(0);

}

else if ((startingrod=='A' && endingrod=='C')||(startingrod=='C' && endingrod=='A'));

{

char other = (char)('A' + 'B' + 'C' - startingrod - endingrod) ;

System.out.println("OK. Starting with disks 1, 2, 3, on rod " + startingrod + "." + "\n");

System.out.println("Moves are as follows:");

int count = playHanoi (3,startingrod,other,endingrod);

System.out.println();

System.out.printf("All disks 1, 2, 3 are on rod %c.\n", endingrod);

System.out.println("All done.");

System.out.println("Took a total of " + count + " moves.");

sc.close();

System.out.println();

}

}

private static int playHanoi( int n, char from , char other, char to)

{

int index = 1;

int count = 1;

if (n == 0)

return 0;

else if(n == 1)

{

System.out.printf("%d.", index);

System.out.printf(" Move disk "+ n +" from rod %c to rod %c \n", from, to);

index++;

return count;

}

else

{

count+= playHanoi(n - 1, from, to, other);

System.out.printf("%d.", index);

System.out.printf(" Move disk " + n + " from rod %c to rod %c \n", from, to);

index++;

count+= playHanoi(n - 1, other, from, to);

index++;

return count;

}

}

}

The program is at:  https://repl.it/@quangnvpham1/W7L1-Recursive-Tower-of-Hanoi-Program#Main.java .

Any help with this problem is greatly appreciated. Yours truly, Quang Pham

Solutions

Expert Solution

I run your code and get your point . Please attach output always if you do have problem in that .

As you mentioned you want to get the index numbers in correct sequence :-

One alternative for that is make your index global and static so that value of it remains unchanged or i can say constant while moving back in recursion. For code you can see below:-

import java.io.*;

import java.util.List;

import java.util.Scanner;

public class Main {

static int index=1; // Firstly i make the index variable global and initializing it with 1 as value.

public static void main(String[] args){

/**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole C without ever putting a larger disk on top of a smaller disk.*/

System.out.println("\nW7L1 Tower of Hanoi");

System.out.println("Program by Quang Pham\n");

char startingrod = ' ';

char endingrod = ' ';

Scanner sc = new Scanner(System.in);

System.out.println("Enter the starting rod letter (A, B, or C).");

startingrod = sc.next().charAt(0);

System.out.println("Enter the ending rod letter (A, B, C).");

endingrod = sc.next().charAt(0);

if (startingrod == endingrod)

{

System.out.println("Sorry. Starting rod and ending rod cannot be the same.");

System.out.println("Enter ending rod letter (A, B, C)");

endingrod = sc.next().charAt(0);

}

else if ((startingrod=='A' && endingrod=='C')||(startingrod=='C' && endingrod=='A'));

{

char other = (char)('A' + 'B' + 'C' - startingrod - endingrod) ;

System.out.println("OK. Starting with disks 1, 2, 3, on rod " + startingrod + "." + "\n");

System.out.println("Moves are as follows:");

int count = playHanoi (3,startingrod,other,endingrod);

System.out.println();

System.out.printf("All disks 1, 2, 3 are on rod %c.\n", endingrod);

System.out.println("All done.");

System.out.println("Took a total of " + count + " moves.");

sc.close();

System.out.println();

}

}

private static int playHanoi( int n, char from , char other, char to)

{

// int index = 1;

int count = 1;

if (n == 0)

return 0;

else if(n == 1)

{

System.out.printf("%d.", index);

System.out.printf(" Move disk "+ n +" from rod %c to rod %c \n", from, to);

index++;

return count;

}

else

{

count+= playHanoi(n - 1, from, to, other);

System.out.printf("%d.", index);

System.out.printf(" Move disk " + n + " from rod %c to rod %c \n", from, to);

index++;

count+= playHanoi(n - 1, other, from, to);

index--; // I just decrease the count of index by 1 as it will increase two times in the recursion due to //which it skips index 4 always.

index++;

return count;

}

}

}

While running the code just remove the comments and the code will work fine.


Related Solutions

Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other...
I have a sequence of natural numbers. How can I get the height of the resulting...
I have a sequence of natural numbers. How can I get the height of the resulting tree/ c++(send code)
I have to code the assignment below. I cannot get the program to work and I...
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help! Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than...
I would like to get som survey question for dry cleaning service , I want to...
I would like to get som survey question for dry cleaning service , I want to attract new people who live in the area like new homewoners.
Q3 a) i) state 3 uses of index numbers ii) State 2 limitations of index numbers...
Q3 a) i) state 3 uses of index numbers ii) State 2 limitations of index numbers b) The prices and quantities (in Kg) of four food items ( gari, milk, sugar, and rice) purchased by an individual household in a typical month in 2010, and 2015 are given in the following table Food item 2010 2015 Price (GHS) Quantity (Kg) Price (GHS) Quantity (Kg) Gari 300 6 410 10 Rice 810 5 420 14 Milk 280 4 1250 4 sugar...
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
How would I get this started: Code a logic program that uses a for loop to...
How would I get this started: Code a logic program that uses a for loop to call your method 5 times successively on the values 1 through 5. (i.e. it would display the val and it’s square…)
Why the rainbow appears in the atmosphere at specific air humidity? I would like to get...
Why the rainbow appears in the atmosphere at specific air humidity? I would like to get a detailed description for better understanding, some schemes, examples based on basic relations. I would like to get some additional articles, books, or sources to read more too.
I would like to get the step by step solution for the below question Question 11...
I would like to get the step by step solution for the below question Question 11 pts What is the difference between positive economics and normative economics? Group of answer choices Positive economics deals with dynamic systems, while normative economics focuses on static systems. Normative economics deals with how the world actually works, whereas positive economics focuses on what people ought to do. Positive economics requires making value judgments, while normative economics relies solely on factual statements. Normative economics applies...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT