Download presentation
Presentation is loading. Please wait.
1
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 106 - Fall 2007 Today’s Topics Comments and/or Questions? DecimalFormat class ternary conditional operator recursion
3
Recursion Understand that the Factorial example, was for instructional purposes only, since it is a very simple example of recursion. For the factorial method, in practice, we would always prefer the non-recursive (iterative) solution. It is more efficient in terms of both time and space. Recursive solutions often take more time to run, because making a method call is slower and requires more memory than doing the same statements without a method call.
4
Recursion Direct vs. indirect recursion – When a method calls itself, it is direct recursion – When method1 calls method2 and method2 calls method1, it is indirect. – There can be more than just two methods involved in the indirection.
5
Towers of Hanoi Three pegs Start with different size discs on first peg Move them all to the third peg using the following rules – Move only one disc at a time – Can't place a larger disc on a smaller disc – All discs must be on some peg except for the disc in transit between pegs Let's see an applet on the web.
6
Towers of Hanoi Do a solution with 3 discs. Do a solution with 4 discs. See any patterns emerging? Is it recursive?
7
Towers of Hanoi To move N discs from first peg to third – Move topmost N-1 discs from first peg to second – Move the largest disc from the first peg to third – Move the N-1 discs from the second peg to the third
8
Towers of Hanoi During the solution, each peg takes on a role – Either the start peg, end peg, or intermediate peg. – Notice: In the first and third parts, notice that they are the same as the whole, with the roles of the pegs changing. First part: – Move from: first peg (start) – To: second peg (end) – Using as intermediate: third peg (intermediate) Third part: – Move from: second peg (start) – To: third peg (end) – Using as intermediate: first peg (intermediate)
9
Towers of Hanoi void moveTower(int numDiscs, int start, int end, int intermed) { if (numDiscs == 1) moveOneDisc(start, end); else { moveTower(numDiscs – 1, start, intermed, end); moveOneDisc(start, end); moveTower(numDiscs – 1, intermed, end, start); } void moveOneDisc(int p1, int p2) { System.out.println(“Move the disc from peg “ + p1 + “ to peg “ + p2); }
10
Towers of Hanoi Let's put this in a program and run it. Does anyone think that an iterative solution would be possible? If so, do you think it'll be easier? You can think about this on your own if you wish and try to come up with an iterative (that is, non-recursive) solution.
11
Card/Deck implementation Purpose of these classes will be to create a general Card class and general Deck class that could be used for an arbitrary card game. So, nothing game specific is desired to be in the classes. What data describes a Card? What data describes a Deck? What behaviors does a Card exhibit? What behaviors does a Deck exhibit?
12
Card/Deck implementation What data describes a Card? – Value and suit (types for these?) What data describes a Deck? – An ordered group of some fixed number of cards What behaviors does a Card exhibit? – None really independent of a Deck What behaviors does a Deck exhibit? – Can shuffle it – Can cut it – Can deal a card from it
13
Shuffle method Ideas for the shuffle method – try to simulate human shuffling – or just swap 2 cards a bunch of times to get a mixed deck – or other ideas?
14
Cut method Ideas for the cut method Have another array of 52 Cards to copy into (then later copy this array back into original.) Generate some random number between 0 and 51 and use that as the place to cut the cards. Keeping track of starting and ending indicies is a challenge but its doable if we're careful. Draw copying idea on board. – How many loops and what will each one do?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.