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);
  }
 }
}

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.

Edited 4 Years Ago by DavidKroukamp: n/a

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);

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.

This question has already been answered. Start a new discussion instead.