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

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 04 / 02 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 06 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 16 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 09 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 07 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Topic 4 Inheritance.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Data Structures and Algorithms revision
Lecture 12 Inheritance.
Object Oriented Programming (OOP) LAB # 8
Inheritance Inheritance is a fundamental Object Oriented concept
Presentation transcript:

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

Michael Eckmann - Skidmore College - CS 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

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.

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.

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.

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.

Michael Eckmann - Skidmore College - CS 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.

Michael Eckmann - Skidmore College - CS 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

Michael Eckmann - Skidmore College - CS 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.

Michael Eckmann - Skidmore College - CS 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

Michael Eckmann - Skidmore College - CS 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

Michael Eckmann - Skidmore College - CS 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

Michael Eckmann - Skidmore College - CS 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

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?

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; //... }

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; //... }

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?

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?

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

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

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;

Bank account What methods might we need?

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)

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

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.

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

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.

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

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.

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