Results 1 to 4 of 4
 04192013, 07:02 PM #1Member
 Join Date
 Feb 2013
 Posts
 6
 Rep Power
 0
Implementing a counter for recursive program
Hi all,
So I was given a homework assignment to write a code that completes the "Towers of Hanoi" as a program based on recursion. I have completed the code, so I am NOT asking anybody to write the code for me.
However, I would like to improve the code by adding a total moves counter at the very end, but I am not sure where to place my counter or how to implement it in my current code.
Here is my code:
Java Code:import java.util.Scanner; public class TowersofHanoi { public static void main(String[] args) throws IllegalArgumentException { System.out.println("How many disks?"); Scanner input = new Scanner(System.in); int n = input.nextInt(); char a = 'A', b = 'B', t = 'T'; moveDisks(n, a, b, t); } public static void moveDisks(int n, char A, char B, char T){ //Precondition: n>0 //Throws an error if number of disks equals 0 if(n==0){ throw new IllegalArgumentException("Number of disks must be an integer greater than 0"); } else if(n==1){ //base case, one disk moveOneDisk(n, A, B); } else { //recursive case, n>1 moveDisks(n1, A, T, B); moveOneDisk(n, A, B); moveDisks(n1, T, B, A); } } public static void moveOneDisk(int n, char A, char B){ System.out.println("Move disk " + n + " from " + A + " to " + B); } }
Currently the output would look like (this is for 3 disks):
Java Code:Move disk 1 from A to B Move disk 2 from A to T Move disk 1 from B to T Move disk 3 from A to B Move disk 1 from T to A Move disk 2 from T to B Move disk 1 from A to B
Java Code:Move disk 1 from A to B Move disk 2 from A to T Move disk 1 from B to T Move disk 3 from A to B Move disk 1 from T to A Move disk 2 from T to B Move disk 1 from A to B Total moves: 7
Any help is greatly appreciated! :)
 04192013, 09:19 PM #2Senior Member
 Join Date
 Nov 2012
 Posts
 257
 Rep Power
 3
Re: Implementing a counter for recursive program
every time a move happens just increment an integer:
define at the start of move disks
int moves = 0;
then every time you make a move increment the counter or call a method that does so:
Java Code:public void increment() { moves ++; }
System.out.println("Total number of " + moves);
that's the info you need, but ill leave it to you, to work out where to put the print statement.
This is way id go about it anywaysLast edited by monkeyjr97; 04192013 at 09:24 PM.
 04192013, 11:26 PM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,630
 Rep Power
 7
Re: Implementing a counter for recursive program
You could just use a static variable named moveCount and increment it in your moveOneDisk method. Also, why not do the check for n != 0 before your first call to moveDisks as opposed to each time you enter moveDisks? One design methodology is to have a single public method which checks the validity of the arguments and then have the actual processing performed in a private method which is called from the public method.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 04202013, 11:41 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,005
 Blog Entries
 7
 Rep Power
 23
Re: Implementing a counter for recursive program
If you relax your precondition a bit; i.e. if n <= 0 there is nothing to move, you can rewrite your recursive method to:
Java Code:public static void moveDisks(int n, char A, char B, char T){ //Precondition: n>0 if(n > 0){ moveDisks(n1, A, T, B); moveOneDisk(n, A, B); moveDisks(n1, T, B, A); } }
JosI have the stamina of a seal; I lie on the beach instead of running on it.
Similar Threads

Recursive program keeps saying Stackoverflow HELP :(
By zygmfb in forum New To JavaReplies: 1Last Post: 11122012, 12:43 PM 
recursive 99 bottles program is there better way to write it as a beginner?
By darkbandit in forum New To JavaReplies: 8Last Post: 09262012, 10:56 AM 
Letter counter program. having trouble understanding it.
By silverglade in forum New To JavaReplies: 17Last Post: 05212011, 01:06 PM 
help with BNF Grammar program using recursive descent parsing
By carolain79@hotmail.com in forum New To JavaReplies: 1Last Post: 10212009, 08:00 PM 
problem with recursive binary search program
By imran_khan in forum New To JavaReplies: 3Last Post: 08022007, 03:08 PM
Bookmarks