Results 1 to 4 of 4
  1. #1
    wtbarr is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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){
            //Pre-condition: 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(n-1, A, T, B);
                moveOneDisk(n, A, B);
                moveDisks(n-1, 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
    But what I want to make happen is:

    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! :)

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default 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 ++;
         }
    then after you are finished printing all the moves just print out:

    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 anyways
    Last edited by monkeyjr97; 04-19-2013 at 10:24 PM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default 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){
            //Pre-condition: n>0
            if(n > 0){
                moveDisks(n-1, A, T, B);
                moveOneDisk(n, A, B);
                moveDisks(n-1, T, B, A);
            }
        }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 11-12-2012, 12:43 PM
  2. Replies: 8
    Last Post: 09-26-2012, 11:56 AM
  3. Letter counter program. having trouble understanding it.
    By silverglade in forum New To Java
    Replies: 17
    Last Post: 05-21-2011, 02:06 PM
  4. help with BNF Grammar program using recursive descent parsing
    By carolain79@hotmail.com in forum New To Java
    Replies: 1
    Last Post: 10-21-2009, 09:00 PM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •