Hey guys I had an assignment to recursively write the Towers of Hanoi then test it.

I did not have too much of an issue getting the program written
But I am not sure what the book means by test the efficiency
do they want the number of moves my program makes, or do they want the time it takes to run. And how do I get either one?
help is appreciated

import TerminalIO.KeyboardReader;

 public class towers
 {
  public static void main(String[] args)
  {
  
  KeyboardReader reader = new KeyboardReader();
  int numberofRings = reader.readInt("Enter number of Rings: ");
  move (numberofRings, 1, 3, 2);
  
  }

  static void move(int n, int i, int j, int k)
  {

  if (n > 0)
  {

  move(n - 1, i, k, j);

  System.out.println("Move ring " + n + "from peg" + i + "to" + j);

  move(n - 1, k, j, i);
  }
 }
}
Re: Testing Towers of Hanoi 80 80

Hey guys I had an assignment to recursively write the Towers of Hanoi then test it.

I did not have too much of an issue getting the program written
But I am not sure what the book means by test the efficiency
do they want the number of moves my program makes, or do they want the time it takes to run. And how do I get either one?
help is appreciated

import TerminalIO.KeyboardReader;

 public class towers
 {
  public static void main(String[] args)
  {
  
  KeyboardReader reader = new KeyboardReader();
  int numberofRings = reader.readInt("Enter number of Rings: ");
  move (numberofRings, 1, 3, 2);
  
  }

  static void move(int n, int i, int j, int k)
  {

  if (n > 0)
  {

  move(n - 1, i, k, j);

  System.out.println("Move ring " + n + "from peg" + i + "to" + j);

  move(n - 1, k, j, i);
  }
 }
}

I guess it could be either however my best guess would be that they mean how long it takes the program to execute... You can do this similar to this:

long startTime = System.currentTimeMillis();

		long total = 0;
		for (int i = 0; i < 10000000; i++) {
			total += i;
		}

		long stopTime = System.currentTimeMillis();
		long elapsedTime = stopTime - startTime;
		System.out.println(elapsedTime);

adding in how many iterations it makes could be of help, even if its not needed i doubt they will penalize you.

Re: Testing Towers of Hanoi 80 80

So would I my code in the for loop?

Re: Testing Towers of Hanoi 80 80

So would I my code in the for loop?

no... At the beginning of your code i.e in the main method you'd have:

long startTime = System.currentTimeMillis();

and at the end of the main method, or at the end of all your processing you'd have:

long stopTime = System.currentTimeMillis();
		long elapsedTime = stopTime - startTime;
		System.out.println(elapsedTime);
Re: Testing Towers of Hanoi 80 80

Ok ok, thank you, thats what I was confused on.

Re: Testing Towers of Hanoi 80 80

what is the unit of the time returned by the method System.currentTimeMillis(); ?

Re: Testing Towers of Hanoi 80 80

...

Re: Testing Towers of Hanoi 80 80

seriously? re-opening a year old thread for an unrelated question??

Re: Testing Towers of Hanoi 80 80

mohammedfae:
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules

As for your question - just look at the API documentation for the System class. The description of its currentTimeMillis() method tells you what you need to know. YOu can do that yourself in less than 1 minute - which is a lot faster than waiting for someone to tell you.

Re: Testing Towers of Hanoi 80 80

I DID deleted that question ... I dnt know why it still appears there !!

Re: Testing Towers of Hanoi 80 80

because you can't "delete", perhaps?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.