Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Today’s Topics Comments and/or Questions? Programming assignment 1 will be returned Wednesday and a new programming assingment will be assigned that day as well (to be due on Tuesday night 11:59 pm.) Towers of Hanoi code with nice printing that shows the recursive method calls indented. Continue exercise with Card and Deck –let's write the Cut method BinBin's way and compare this way with the way we wrote it last time How Object References work vs. how primitive variables are handled in Java "is a" and "has a" relationships Inheritance ideas Bank Account exercise

3 Shuffle & Cut methods Mihaela pointed out in the lab that both the shuffle and cut methods should reset topOfDeck to be 0. That makes sense.

4 Cut method BinBin's Idea for the cut method Have another array of 104 Cards to copy into, then copy the 52 cards back into original. Generate some random number between 0 and 51 and use that as the place to cut the cards. Copy from 0 to cutIndex into indices starting at 52 in the new array. Copy from cutIndex+1 to 51 into the new array at the same indices. Then to copy back go from cutIndex+1 in new array and copy into original deck starting at 0.

5 Fix the Deck class Let's also fix the Deck class so that it can either contain 52 or 54 cards. If it contains 54 cards then these 2 are jokers. e.g. public final int totalCards; // 52 or 54 (includes 2 jokers) The constructor should take in something to specify whether we are creating a Deck with or without Jokers. All the methods and array sizes should refer to the constant called totalCards.

6 Card/Deck implementation Realize that once we create these Card and Deck classes we should be able to use them in any kind of Card Game that uses a deck of 52/54 cards. The Card and Deck classes themselves shouldn't have anything added to them for specific games. The Game classes should take care of Game specific things. For example, for a Poker game a Card or a Deck shouldn't care (or implement anything regarding) a straight or a flush or anything specific to Poker. A seperate class should handle that kind of code.

7 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” I'll draw this on the board.

8 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 large implication of this is that changing one object's values will affect the other. Card c1 = new Card(1, 1); // Ace of Diamonds Card c2; c2 = c1; c1.changeCard(2, 2); // 2 of Clubs // 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

9 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.

10 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

11 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

12 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 Diamonds } // Then in main method a call to it might be like: Card acard = new Card(1, 1); // Ace of Diamonds 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

13 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 References vs. Primitive variables Card c = new Card(2,2); Card d; d = c; d.setRank(12); System.out.println(c.toString()); System.out.println(d.toString()); // c and d refer to the same data =================================================== int x = 2; int y; y = x; y = 12; System.out.println("x = " + x); System.out.println("y = " + y); // x and y refer to different data

14 Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. For example: – A Card has a suit and has a number – A Deck has 52 Cards – A Car has a color and has a make and has a model,... – An Employee has a hire date and has a salary,... – Can we think of any more?

15 Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. public class Card { private int suit; private int number; //... } public class Car { private String color; private String make; //... }

16 Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. public class Date { private int year, month, day; //... } public class Employee { private Date hire_date; private double salary; //... }

17 Is-a vs. Has-a An “is a” relationship is one that corresponds to a class and the class from which it inherits. Also can be said as “is a type of”. For example: – A Faculty member is an Employee – A Staff member is an Employee – A Car is a Vehicle – A Truck is a Vehicle – A Motorcycle is a Vehicle – Can we think of any more?

18 Other inheritance relationships Can you think of anything else that has a “has a” relationship? – Rooms have doors, windows, etc. Can you think of anything else that has an “is a” relationship? – Bedroom is a Room – Dining room is a Room – How about Animals?

19 Bank account Exercise What data members describe a bank account? – That is, what does a bank account have. What types are these? We want to handle Checking and Savings Accounts

20 Bank account Maybe we might want the following data members: – Account number – Balance – Overdrawn Fee – ATM Withdrawal Per Day Limit – Bounced Check Fee – Interest Rate

21 Bank account So we could have a class named BankAccount that contains the following data: String Account_number; double Balance; double Overdrawn_Fee; int ATM_Withdrawal_Per_Day_Limit; double Bounced_Check_Fee; double Interest_Rate;

22 Bank account What methods might we need?

23 Bank account Notice that the data here isn't for all accounts: – Account number (for all accounts) – Balance (for all accounts) – Overdrawn Fee (for all accounts) – ATM Withdrawal Per Day Limit (for all accounts) – Bounced Check Fee (for checking accounts only) – Interest Rate (for savings accounts only)

24 Bank account We could create an Account class that contained the common stuff among all accounts We then could create a SavingsAccount class that inherits all the stuff about an Account from Account class and adds the things that are specific to Savings Accounts. We also could create a CheckingAccount class that inherits all the stuff about an Account from Account class and adds the things that are specific to Checking Accounts. These ideas are Inheritence ideas

25 Inheritance If class A inherits from class B, then class A is a subclass of B and class B is a superclass of A. We also talk of class A as being class B's child and class B being class A's parent. We can draw the inheritance relationships hierarchy by having superclass(es) at the top and subclass(es) lower, using lines to connect where there's an inheritance relationship.

26 Inheritance Class A may inherit from class B which can in turn, inherit from class C. (draw on the board.) However, this is not considered multiple inheritance. Multiple inheritance is an object-oriented concept that allows a subclass to inherit from more than one class --- this is NOT allowed in Java. e.g. Class D cannot inherit from both class E and F directly. (draw on the board.)

27 Inheritance A subclass inherits all the instance variables in its superclass and has access to (can call) any public methods. Even if there is private instance data in a superclass, the subclass inherits that data but can't refer to the variables. e.g. a SavingsAccount will inherit accountNumber from Account, so an object of type SavingsAccount will have an accountNumber.

28 Object is the superclass of all All classes inherit from Java's Object class. All classes that do not use the extends keyword directly inherit from Java's Object class. What does that mean for us? Let's visit the Java API for the Object class. – equals() – toString()

29 super is a reference to the parent class super(); // is a call to the parent's default (no parameter) constructor A call to a parent constructor should be the first thing done in any constructor of a child. If you don't explicitly call it, Java will automatically call the parent's default constructor if it exists.

30 Hierarchy of the account classes SavingsAccount and CheckingAccount each inherit from Account What about AccountTester? How about the Object class?


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

Similar presentations


Ads by Google