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

My recursive Tower of Hanoi program does not render the correct index numbers. Also, it does...
My recursive Tower of Hanoi program does not render the correct index numbers. Also, it does not prompt user to enter the starting rod letter and the ending rod letter. Could you help me fix it? Main.java import java.io.*; import java.util.List; import java.util.Scanner; public class Main { int index = 1; 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...
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...
C++ Create a recursive program what will test three recursive functions. It would have a menu...
C++ Create a recursive program what will test three recursive functions. It would have a menu like: 1. Recursive factorial 2. Towers of Hanoi 3. Recursive summation 0. Exit Enter selection: 3 Enter number: 4 The recursive summation will take an integer and sum value from 1 to the integer. Don’t enter a number if the selection is 0 for quit. Please have the functions in an implementation file and the prototypes of the functions in a header file and...
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.
I am trying to get this code to work but I am having difficulties, would like...
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible package edu.hfcc; /* * Create Java application that will create Fruit class and Bread class * * Fruit class will have 3 data fields name and quantity which you can change. * The third data field price should always...
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){...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT