3

Good evening everyone,

I have a quick question on a homework assignment my class is doing about recursion. The idea is that we have this towers of hanoi program, and we need to write a main that will make a table that will display the numbers 5-25, and how many moves it would take to solve a tower of that size, for example

5 ---- 31 Moves

6 ---- 63 Moves

etc...

I am having a little bit of trouble doing that as the TowersOfHanoi class is set up to print out each move, and I don't think we're supposed to get rid of that, but I'm not too sure.

Here is the TowersOfHanoi class

public class TowersOfHanoi {
    private int totalDisks;
    private int count;

    public TowersOfHanoi(int disks) {
        totalDisks = disks;
        count = 0;
    }

    public void solve() {
        moveTower (totalDisks,1,3,2);
    }

    private void moveTower(int numDisks, int start, int end, int temp) {
        if (numDisks ==1) {
            moveOneDisk(start,end);
        }
        else {
            moveTower (numDisks-1, start, temp, end);
            moveOneDisk (start, end);
            moveTower (numDisks-1, temp, end, start);
        }
    }

    private void moveOneDisk(int start, int end) {
        count = count+1;
        System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
    }
}

Now I just need to write a main that will create that table without printing out every single move for every single tower, but I'm not really sure how to. Any help is much appreciated

3
  • 2
    I'm not sure why you're being down voted as homework questions are allowed so long as you're not asking for an all out solution which you don't seem to be. @Ademiban is right though, this site is full of people who are volunteering their time to assist random strangers with their questions about programming. All they ask in return is that you check the answer that was most helpful to you. :) Commented Apr 26, 2012 at 6:13
  • 1
    Spencer - this was actually helpful and constructive. I'm new here and don't quite understand how things work yet, so thank you Commented Apr 26, 2012 at 6:16
  • Glad to help. :) It seems like part of your question pertains to how the question is intended to be answered so I'd highly recommend you consult your teacher. I expect they will say to simply comment out the println statement in moveOneDisk(). Commented Apr 26, 2012 at 16:00

3 Answers 3

2

You'll need one object of TowersOfHanoi class for each disk you'll be solving the puzzle for. For this you'll create these objects in the main method by passing them different arguments (from 5 to 25). Once the object is constructed you just call the solve method on it.

I'll leave the implementation to you as this is tagged homework.

Sign up to request clarification or add additional context in comments.

1 Comment

I actually tried this with a for loop that would pass TowersOfHanoi 5-25 and than run solve() on each of them, but the problem with this is that it will print out each and every move for every tower, which I am trying to avoid, as I only want the number of moves it would take to solve that tower
0

you could remove the print statement in moveOneDisk, then after you call the recursion method in main write a print statement that will output count.

private void moveOneDisk(int start, int end) {
    count = count+1;
    //System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
}

public static void main(){
TowersOfHanoi tower = TowersOfHanoi(5);
tower.solve();
system.out.print(tower.count);
}//end of main

removing the print statement in the moveOneDisk would make it so that not every single move is reported, but the count will still increment. then you can assign whatever variable needed when constructing the table with the class member count. in this case tower.count

Comments

0

package midterm;

public class TowersofHanoi {
    public int totalDisks;
    public static int count;

    public TowersofHanoi(int disks) {
        totalDisks = disks;
        count = 0;
    }

    public void solve() {
        moveTower (totalDisks,1,3,2);
    }

    private void moveTower(int numDisks, int start, int end, int temp) {
        if (numDisks ==1) {
            moveOneDisk(start,end);


        }
        else {
            moveTower (numDisks-1, start, temp, end);

            moveOneDisk (start, end);
            moveTower (numDisks-1, temp, end, start);
        }
    }

    private void moveOneDisk(int start, int end) {
        count = count+1;
        //System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
    }
    public static void main(){
        TowersofHanoi tower = new TowersofHanoi(5);
        tower.solve();

        System.out.println(tower.count);
        }

}

1 Comment

You should post also some explanation along with your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.