Location: Planet Laser Interview Skills Workshop

Similar documents
Location: Planet Laser Interview Skills Workshop

Location: Planet Laser Interview Skills Workshop

Administrivia. CPSC Winter 2008 Term 1. Department of Computer Science Undergraduate Events

1 10 3:30 5: :30 1:30 206, ICICS/CS

Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

Rules and syntax for inheritance. The boring stuff

Polymorphism. return a.doublevalue() + b.doublevalue();

What is Inheritance?

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

CS/ENGRD 2110 SPRING 2018

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Introduction to Inheritance

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

Intro to Computer Science 2. Inheritance

Practice for Chapter 11

Chapter 15 Chapter 11

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CS-202 Introduction to Object Oriented Programming

Lecture 4: Extending Classes. Concept

Binghamton University. CS-140 Fall Dynamic Types

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

type conversion polymorphism (intro only) Class class

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

Java: introduction to object-oriented features

CH. 2 OBJECT-ORIENTED PROGRAMMING

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Implements vs. Extends When Defining a Class

25. Generic Programming

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

First IS-A Relationship: Inheritance

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

Operators and Expressions

The class Object. Lecture CS1122 Summer 2008

Lecture 2: Java & Javadoc

Inheritance (Part 5) Odds and ends

CSE 401/M501 Compilers

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Polymorphism. Agenda

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Principles of Software Construction: Objects, Design, and Concurrency

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Inheritance -- Introduction

Exercise: Singleton 1

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Inheritance and Polymorphism

Class, Variable, Constructor, Object, Method Questions

AP CS Unit 6: Inheritance Notes

Example: Count of Points

More Relationships Between Classes

Inheritance (continued) Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

VIRTUAL FUNCTIONS Chapter 10

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Inheritance (P1 2006/2007)

More On inheritance. What you can do in subclass regarding methods:

Java Fundamentals (II)

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance. The Java Platform Class Hierarchy

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Principles of Software Construction: Objects, Design and Concurrency. Inheritance, type-checking, and method dispatch. toad

C# Programming for Developers Course Labs Contents

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Computer Science II (20073) Week 1: Review and Inheritance

Type Hierarchy. Lecture 6: OOP, autumn 2003

Java Classes, Inheritance, and Interfaces

Chapter 5 Object-Oriented Programming

Department of Computer Science Undergraduate Events

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Today. Homework. Lecture Notes CPSC 224 (Spring 2012) Quiz 5. interfaces. exceptions. best practices. start on swing (if time) hw3 due

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

1 Shyam sir JAVA Notes

Check out Polymorphism from SVN. Object & Polymorphism

Selected Java Topics

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

INSTRUCTIONS TO CANDIDATES

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Java Object Oriented Design. CSC207 Fall 2014

CS260 Intro to Java & Android 03.Java Language Basics

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CS111: PROGRAMMING LANGUAGE II

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

Everything is an object. Almost, but all objects are of type Object!

Handout 9 OO Inheritance.

Outline. 15. Inheritance. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff

Transcription:

Department of Computer Science Undergraduate Events Events this week Drop in Resume/Cover Letter Editing Date: Tues., Jan 19 CSSS Laser Tag Time: 12:30 2 pm Date: Sun., Jan 24 Location: Rm 255, ICICS/CS Bldg. Time: 7 9 pm Location: Planet Laser Interview Skills Workshop @ 100 Braid St., New Westminster Date: Thurs., Jan 21 Time: 12:30 2 pm Location: DMP 201 Registration: Email dianejoh@cs.ubc.ca Project Management Workshop Speaker: David Hunter (ex VP, SAP) Date: Thurs., Jan 21 Time: 5:30 7 pm Location: DMP 110 Event next week Public Speaking 101 Date: Mon., Jan 25 Time: 5 6 pm Location: DMP 101 Administrivia Lecture slides (day by day) are on the web: http://www.cs.ubc.ca/~norm/211/2009w2 Reminder: Assignment #1 is on the web site Due Thursday, January 28 th, 10:00pm Last day for registration changes is today!

Where are we? We ve learned about exceptions An alternative way for a method to complete Checked vs. unchecked Throwing and catching and finally Subclasses of exceptions One last thing how should we use exceptions? Designing Exceptions Need to distinguish boundary cases that can be handled by the method from exceptional cases which should throw exceptions Define individual exception for each type of error can group them into hierarchies allows more flexibility in handling them Exceptions thrown by a method are shown in the method s comment using the @throws tag. Too many exceptions may make the method difficult to use. Exceptions and Postconditions: The postcondition should distinguish the case where an exception is thrown from the case when it is not i.e., if withdraw(amount) throws an exception when the amount is negative, its postcondition would be: IF amount>=0 THEN getbalance() = @pre.getbalance() amount ELSE getbalance() = @pre.getbalance()

On to new stuff Recap + equals() This lecture ensures you are competent with the basics of the Java you learned in 111 and 211 to this point. We ll also add in the concepts of: inheritance and over-riding equals() If there are concepts covered today that are not clear after lecture, review of lecture materials and review of relevant parts of the book, please see an instructor to clarify the confusing points. We ll be building on all of these basics for the rest of term. CPSC 211 Winter 2008, Term 1 6

Inheritance A polymorphic assignment is one of the form: MyClass reference_to_object; reference_to_object = expression; where the type of expression must be a subtype of MyClass. Three types are involved here: the reference type : the type that the reference was declared to be the expression type : the type of the result of the expression (as can be determined at compile time) the actual type: the type of the object that is actually returned by the expression (determined at run time) The expression type must be a subclass of the reference type, otherwise this gives a compile-time error Inheritance Example: Suppose that SavingsAccount is a subclass of Account, and SpecialSavAccount is a subclass of SavingsAccount : Account acc = new SavingsAccount(); reference type : expression type : actual type : Account acc; SavingsAccount sacc = new SpecialSavAccount(); acc = sacc; reference type : expression type : actual type :

Inheritance You can explicitly convert (cast) references between a type and its subtypes: widening: converting a subtype to one of its super-types always allowed in Java (subtype "is-a" super-type) no explicit cast is needed narrowing or downcasting: converting a supertype to one of its subtypes not always possible may throw ClassCastException explicit cast is always needed Account acc = new SavingsAccount(); SavingsAccount sacc; sacc = (SavingsAccount) acc; // cast OK More Downcast Uses The declared (static) type of a reference determines which methods can be called. Suppose that SavingsAccount defines addinterest() and that this method is not defined in Account, then: Account acc = new SavingsAccount(); acc.addinterest(); // Error but ((SavingsAccount) acc).addinterest(); // ok the following also works: SavingsAccount sacc = (SavingsAccount) acc; sacc.addinterest();

Which method is called? The actual method called is determined at run time based on the actual type of the object. Example: Suppose that SavingsAccount is a subclass of Account, and SpecialSavAccount is a subclass of SavingsAccount. Further assume that all three (Account, SavingsAccount, and SpecialSavAccount) define m() : Account acc; SavingsAccount sacc = new SpecialSavAccount(); acc = sacc; acc.m(); calls the method m() defined in SpecialSavAccount. If SpecialSavAccount didn t define m(), then it would call the method inherited from its lowest superclass that defines m(). I wonder In the situation on the previous slide, is there any way for a client (not the implementor of class Account, SavingsAccount or SpecialSavAccount) to invoke the method m() defined in class Account on an object of class SpecialSavAccount?

Comparing References We can compare two object references using Java s == operator two object references are equal if and only if they reference the same object (i.e., have the same value) Bicycle mybike; Bicycle bikeforsale; mybike bikeforsale mybike = new Bicycle(); bikeforsale = mybike; Bicycle In this case bikeforsale == mybike is true Make: Cannondale CurrentGear: 3 CPSC 211 Winter 2008, Term 1 13 Comparing Objects To compare objects we have to use Java s.equals() method two objects are equal if and only if they have the same contents (i.e., have equal object components) a.equals(b) is true if the objects referenced by a and b have equal components Bicycle mybike; Bicycle bikeforsale; mybike bikeforsale mybike = new Bicycle(); bikeforsale=new Bicycle(); Then: mybike.equals(bikeforsale) is true, but mybike == bikeforsale is false Bicycle Make: Cannondale CurrentGear: 3 Bicycle Make: Cannondale CurrentGear: 3 CPSC 211 Winter 2008, Term 1 14

Overriding equals() Its parameter must be of type Object to match the method defined in the Object class The method must check if its explicit argument is null must return false if it is so The method must check that its implicit and its explicit arguments are of the same type must return false if they are of different type For any object references o, o1, o2, o3 in the class,the following must hold o.equals(o) IF o1.equals(o2) THEN o2.equals(o1) IF ( o1.equals(o2) AND o2.equals(o3) THEN o1.equals(o3) The method usually returns true if the two objects are of the same type and their corresponding data components are equal false otherwise CPSC 211 Winter 2008, Term 1 15 Checking an Object s Class To check that two objects are of the same type, equals() must use getclass (defined in Object) to get the actual class of an object instanceof is not specific enough; returns true if one object is a subclass of the other For instance, assuming SavingsAccount extends Account : Account a1 = new Account(); Account a2 = new SavingsAccount(); SavAccount a3 = new SavingsAccount(); Account a4 = (Account) a3; then a1 instanceof Account, a2 instanceof Account, a3 instanceof Account, a4 instanceof Account, are all true but a1.getclass() returns Account a2.getclass() returns SavingsAccount a3.getclass() returns SavingsAccount a4.getclass() returns SavingsAccount CPSC 211 Winter 2008, Term 1 16

Overriding equals() in Account public boolean equals( Object obj ) { if ( obj == null ) return false; if ( getclass()!= obj.getclass() ) return false; Account other = (Account) obj; return id == other.id && balance == other.balance && name.equals(other.name); CPSC 211 Winter 2008, Term 1 17 APPENDIX Credit Card Example

public class SIVACard { private int number; // A unique number for each card private static int nextnumber = 1; // A real credit card # is far more // complicated to generate private CardHolder holder; // The holder of the card protected double rewardsbalance; // The points available to the card holder private double balance; // The balance on the card private Transaction[] currenttransactions; // This period's transactions rivate int currenttransactioncount; private Statement laststatement; // Last statement issued private final static int TRANSACTION_LIMIT = 30; public SIVACard(CardHolder holder) { number = nextnumber++; this.holder = holder; rewardsbalance = 0.0; balance = 0.0; laststatement = new Statement( this ); // Blank statement currenttransactioncount = 0; currenttransactions = new Transaction[TRANSACTION_LIMIT]; public double getrewardsbalance() { return rewardsbalance; public double getbalance() { return balance; public Statement getlaststatement() { return laststatement; public void postpayment(double amount) { balance = amount; public void redeemrewards(double amount) { rewardsbalance = amount; public CardHolder getholder() { return holder; /** * Post a purchase to the card * @param purchase The purchase to post. */ public void postpurchase(transaction purchase) { currenttransactions[currenttransactioncount++] = purchase; computerewards(purchase); /** * Put all current transactions onto the last statement. */ public void generatestatement() { laststatement = new Statement( this ); for (int i = 0; i < currenttransactioncount; i++) { laststatement.addtransaction(currenttransactions[i]); currenttransactioncount = 0; /** * A basic card just gives you one reward point per dollar purchased * @param purchase The purchase from which to determine points */ void computerewards(transaction purchase) { rewardsbalance += purchase.getvalue(); public class CardHolder { private String lastname; private String firstname; public CardHolder( String lastname, String firstname ) { this.lastname = lastname; this.firstname = firstname; public String tostring() { return new String( lastname + ", " + firstname );

public class Statement { private String[] lines; private int numberoflines; // The lines on the statement private SIVACard card; public Statement( SIVACard card ) { numberoflines = 0; lines = new String[30]; this.card = card; public void addtransaction( Transaction atransaction ) { lines[numberoflines++] = atransaction.tostring(); public void print( PrintStream stream ) { stream.println("====== " + card.getholder().tostring() ); stream.println("\ttransactions:"); for ( int i = 0; i < numberoflines; i++ ) { stream.println( "\t\t" + lines[i] ); stream.println("\trewards: " + card.getrewardsbalance() ); stream.println(":::::"); stream.println(); public class Transaction { private int value; private String description; public Transaction( String description, int value ) { this.value = value; this.description = description; public int getvalue() { return value; public String getdescription() { return this.description; public String tostring() { return new String( description + ": " + value );

public class Driver { private final static int NUMBER_OF_CARDS = 10; private static Object[] allcards; private static int allcardsindex = 0; public static void main(string args[]) { CardHolder gail = new CardHolder("Murphy", "Gail"); CardHolder george = new CardHolder("Tsiknis", "George"); SIVACard gailscard = new SIVACard(gail); SIVACard georgescard = new SIVACard(george); gailscard.postpurchase(new Transaction("sushi", 20)); gailscard.postpurchase(new Transaction("air cdn", 750)); georgescard.postpurchase(new Transaction("pizza", 25)); georgescard.postpurchase(new Transaction("air cdn2", 750)); printstatement(gailscard); printstatement(georgescard); private static void printstatement(sivacard acard) { acard.generatestatement(); Statement thestatement = acard.getlaststatement(); thestatement.print(system.out); CardHolder gail2 = new CardHolder("Murphy", "Gail"); if (gail.equals(gail2)) System.out.println("The two cardholders are the same!"); else System.out.println("The two cardholders are different"); SOLUTION

public class CardHolder { private String lastname; private String firstname; public CardHolder( String lastname, String firstname ) { this.lastname = lastname; this.firstname = firstname; public String tostring() { return new String( lastname + ", " + firstname ); public boolean equals( Object ob){ if ( ob == null ) return false; if (getclass()!= ob.getclass()) return false; CardHolder holder = (CardHolder)(ob); return lastname.equals(holder.lastname) && firstname.equals(holder.firstname);