Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Today’s Topics Comments and/or Questions? Exercise to design classes for Card and Deck How Object References work

3 User defined types to represent a Card and a Deck Let's create a class to represent a Card and one to represent a Deck. There are a variety of choices one can make about how to do this. I'll ask for suggestions and make decisions about how we'll implement these. Note that nothing game specific is desired to be in the classes.

4 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. What data describes a Card? What data describes a Deck? What behaviors does a Card exhibit? What behaviors does a Deck exhibit?

5 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

6 Card/Deck implementation Ideas for the deal method – in Deck, maintain a "top of Deck" index. – when deal is called, return the Card at that index and increment the "top of Deck" index. – In this way, we won't have to actually remove Cards from the Deck, we'll just keep track of "top of Deck" and if this index ever gets to the last valid index, then we are at the last Card in the Deck.

7 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?

8 Cut method Ideas for the cut method Have another array of 52 Cards to copy into, then 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?

9 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Object References When we declare an object to be of some Class type, we are declaring a REFERENCE to an object of that class. The reference stores the “memory address” of where the actual data for the object is stored. e.g –String s = “Mike”; –// s is the name of the reference to a String. –// The value in s is a memory address –// that memory address is where to find “Mike”

10 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Object References When we assign one object reference to another –the references both contain the same memory address A main implication is that changing one object's values will affect the other. Card c1 = new Card(1, 1); // Ace of Clubs Card c2; c2 = c1; c1.changeCard(2, 2); // 2 of Diamonds // since c1 and c2 both reference the SAME memory // address, changing one's values changes the others // because c1 and c2 are different names for the same memory

11 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Object References All variables passed into methods have a local copy of them made each time the method executes. –Primitive type variables When variables of primitive types are passed into a method the value that is copied is the actual value of the data in the variable –Object References (and array references) When we pass an object reference to a method the value that is copied is the memory address, NOT the contents at that memory address.

12 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Passing primitives Example with primitives: public static void meth1(int parm) { parm++; System.out.println(“value = “ + parm); } // Then in main method a call to it might be like: int x = 5; meth1(x); // parm has the value 5 copied into it when meth1 is called // meth1 works on that copy, so x's value isn't altered in meth1

13 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Passing References Example with arrays: public static void meth2(int parm[]) { parm[0] = 4; System.out.println(“value = “ + parm[0]); } // Then in main method a call to it might be like: int x[] = { 3, 1, 2, 5 }; meth2(x); // Array names like x here are references, parm gets a copy of // the memory address of x. meth2 works on that address, so x's // array values are altered in meth2

14 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Passing References Example with object references: public static void meth3(Card parm) { parm.changeCard(11, 1); // Jack of Clubs } // Then in main method a call to it might be like: Card acard = new Card(1, 1); // Ace of Clubs meth3(acard); // Object names like acard are references, parm gets a copy of // the memory address of acard. meth1 works on that address, so // acard's object values are altered in meth3


Download ppt "CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann."

Similar presentations


Ads by Google