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

Recommended Answers

All 10 Replies

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.

So would I my code in the for loop?

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

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

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

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

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.

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

because you can't "delete", perhaps?

Be a part of the DaniWeb community

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