• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tower of Hanoi algorithm  RSS feed

 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. What is Stream IO? I know it is like input and output, but what exactly is it? What's the difference between IO.stdout.print and System.out.print?

2. The following is a code that my teacher did in class about a game called The tower of Hanoi. I didn't really understand how it works:


First of all, why declare the method static? How do you know when we need to create an object and when not to and instead, just create static variables and static methods?
What's the difference between line 3 and line 7? For the recursion, what's our base case here? n=3?
I really need someone to clarify it for me! Thank you so much!
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1

Cheryl Scodario wrote:1. What is Stream IO?

Input and output are done on things called streams. Like a stream of characters.

Cheryl Scodario wrote:What's the difference between IO.stdout.print and System.out.print?

System.out.println is standard java. IO.stdout.print is a third party API someone made up. It's not standard, but your teacher presumably wants you to use it.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 2
The methods are static because they don't use any instance variables. Recursive methods tend to pass everything they need as parameters. It would have been ok to make the methods non-static and pass everything as parameters too.

Line 3 is like the public API to Towers of Hanoi. Someone just knows how many disks and poles they have. They don't know what from/to/using are. Line 7 is the actual recursive implementation.

The base case is zero - the if statement on line 8. Three is the starting point.
 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Question 1

Cheryl Scodario wrote:1. What is Stream IO?

Input and output are done on things called streams. Like a stream of characters.

Cheryl Scodario wrote:What's the difference between IO.stdout.print and System.out.print?

System.out.println is standard java. IO.stdout.print is a third party API someone made up. It's not standard, but your teacher presumably wants you to use it.


Thanks, Jeanne. So what you are saying is that System.out and IO.stdout are basically the same thing though one is standard, the other one is not? What do you mean by standard though?
 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Question 2
The methods are static because they don't use any instance variables. Recursive methods tend to pass everything they need as parameters. It would have been ok to make the methods non-static and pass everything as parameters too.


I guess my question is more general, meaning usually when you are coding, how do you decide if you want static method or instance method though I know the definition of each one? It's just hard for me to apply.


The base case is zero - the if statement on line 8. Three is the starting point

I can see why the base case is 0, but what do you mean by 3 is the starting point?

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheryl Scodario wrote:I guess my question is more general, meaning usually when you are coding, how do you decide if you want static method or instance method though I know the definition of each one? It's just hard for me to apply.

I tend to make all methods static unless either (1) the method needs to use an instance variable, or (2) I think the method will need to be overridden in a subclass sometime in the future, or (3) i think I might need to be able to mock out the method in the future. (3) is really a subcase of (2), and since this is Beginning Java, it probably doesn't make much sense at the moment. I'll just say that often it's easier to test a method (and to test other code that uses a method) if that method is not static.

But for now, it's probably fine if you just stick to (1) and (2). Make methods static, unless you have a reason not to make them static. If you do have a reason to make them non static, don't hesitate to do so. At this stage, it's not a big deal - it's usually pretty easy to change your decision later if you need to.

An exception to this: once other people start using your code, especially other people who aren't working right next to you and/or who you don't know about, it becomes a bit harder to change this sort of thing. Still not impossible though.

Alternately, if someone wanted to make all their methods non-static, except a few that had good reason to remain static, I probably wouldn't argue much. There are benefits to this that will show up later (especially for testing), even if they're not apparent now.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheryl Scodario wrote:Thanks, Jeanne. So what you are saying is that System.out and IO.stdout are basically the same thing though one is standard, the other one is not? What do you mean by standard though?

In this case "standard" means that when you talk about System.out, Jeanne and I and everyone else who reads this know exactly what you're talking about, because it's defined in the Sun/Oracle JavaDocs for every currently-maintained version of Java. (Also for every other implementation of Java since it had the name "Java", but we don't really care about that ancient history here.) Specifically you might look at the API for the latest official release of Java, Java SE 6. System.out is defined there, along with many other things that may be of interest. (And more than a few that aren't.)

In contrast, when you talk about IO.stdout, Jeanne and I don't really know what you mean. We can guess (and my guess matches Jeanne's here), but we don't know. Someone we don't know has created this class, and if the details are published anywhere, we don't know about them. Perhaps this someone is your professor. But we don't know, so our comments are guesses at this point.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheryl Scodario wrote:I can see why the base case is 0, but what do you mean by 3 is the starting point?

I think she meant that line 3 is the starting point, at least compared to line 7. Actually line 15 is the overall starting point, but that's outside the range of what you were asking about.
 
Cheryl Scodario
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
Cheryl Scodario wrote:I can see why the base case is 0, but what do you mean by 3 is the starting point?

I think she meant that line 3 is the starting point, at least compared to line 7. Actually line 15 is the overall starting point, but that's outside the range of what you were asking about.


Mike, thanks a lot. Your explanations are very clear. However, regarding the code for the tower of Hanoi, from the main method, I can tell that the input seems to be string argument. So in order to start the game, do i just input "3, Pole 1, Pole 3, Pole 2"? Do you mind sort of walking me through the code (line 7 to 11). I don't understand how will base case being 0 help? and why it's disk-1? I just started learning recursion and am pretty bad at it. Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!