Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Similar documents
Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Lecture 2: Implementing ADTs

Day 5. COMP1006/1406 Summer M. Jason Hinek Carleton University

Introduction to Programming Using Java (98-388)

Data Abstraction and Specification of ADTs

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

LIFO : Last In First Out

csci 210: Data Structures Stacks and Queues

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

Lecture 4. The Java Collections Framework

Lecture 3: Queues, Testing, and Working with Code

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

The Java Collections Framework. Chapters 7.5

Announcements. CS18000: Problem Solving And Object-Oriented Programming

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Standard ADTs. Lecture 19 CS2110 Summer 2009

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

3137 Data Structures and Algorithms in C++

CS24 Week 4 Lecture 2

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CT 229 Object-Oriented Programming Continued

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

== isn t always equal?

CS 206 Introduction to Computer Science II

Programming Languages and Techniques (CIS120)

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

Today s lecture. CS 314 fall 01 C++ 1, page 1

cs Java: lecture #6

CSE 143 SAMPLE MIDTERM

CSC148H Week 3. Sadia Sharmin. May 24, /20

CS 302 ALGORITHMS AND DATA STRUCTURES

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

Announcements/Follow-ups

Stacks and Queues

Interfaces & Generics

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

1 P age DS & OOPS / UNIT II

CS61A Lecture 16 Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 16, 2012

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

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

CSE 373: Data Structures and Algorithms

CSC148 Week 2. Larry Zhang

Basic Data Structures

Announcements/Follow-ups

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

PRIORITY QUEUES AND HEAPS

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Basic Data Structures 1 / 24

CE221 Programming in C++ Part 1 Introduction

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Object Oriented Programming. Java-Lecture 11 Polymorphism

Abstract Data Types. Abstract Data Types

CSE 373: Introduction, ADTs, Design Decisions, Generics. Michael Lee Wednesday Jan 3, 2017

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

COMP Data Structures

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

Chapter 14 Abstract Classes and Interfaces

CS3500: Object-Oriented Design Fall 2013

CS W3134: Data Structures in Java

Abstract Classes and Interfaces

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Name CPTR246 Spring '17 (100 total points) Exam 3

Algorithms and Data Structures

CISC 3130 Data Structures Fall 2018

JAVA MOCK TEST JAVA MOCK TEST II

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Object-Oriented Design Lecture 23 CS 3500 Fall 2009 (Pucella) Tuesday, Dec 8, 2009

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CPSC 221: Algorithms and Data Structures Lecture #0: Introduction. Come up and say hello! Fibonacci. Fibonacci. Fibonacci. Fibonacci. (Welcome!

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

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


ITI Introduction to Computing II

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Queues. Stacks and Queues

ECE Object-Oriented Programming using C++ and Java

CS/ENGRD 2110 SPRING 2018

Chapter 1: Object-Oriented Programming Using C++

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Inheritance and Substitution (Budd chapter 8, 10)

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Transcription:

Lecture 6 COMP1006/1406 (the OOP course) Summer 2014 M. Jason Hinek Carleton University

today s agenda assignments A1,A2,A3 are all marked A4 marking just started A5 is due Friday, A6 is due Monday a quick look back abstract things final things generics, interfaces, Comparable OOP and Java interfaces polymorphism inheritance 2

announcements Midterm 6:05-7:35pm, Wednesday July 30 UC 231 no aids class will resume at 8:00pm in the usual room All requests for accommodations must be received by noon on Friday, July 25 3

announcements Tutorials 6-8pm tutorial (no help for assignments) 8-9pm general help from TAs (including assignments) go to both if it will help if there is a seat you are welcome to attend (no homework help during first two hours) Office Hours Tuesday 4:00-6:00 in HP4155 (Sean) Thursday 11:00-12:30 in HP532 (Jason) for class material or Friday s assignment Thursday 5:00-6:00 in HP4155 (Connor) Friday 11:00-12:30 in HP5332 (Jason) for class material or Monday s assignment 4

last time... abstract classes cannot be instantiated no objects of the class can exist are valid reference types can have variables of this type (cannot use new and create an object of an abstract class) no restrictions on what can be in an abstract class attributes, methods, constructors does not need to have abstract methods cannot be final intention is for a descendant class to be concrete a concrete class can be instantiated (can use new and create an object of a concrete class) 5

last time... abstract methods have no body (definition) public abstract String getname(); forces class to also be abstract cannot be final intention is for a descendant class to define this method 6

last time... final final classes cannot be extended and cannot also be abstract final methods cannot be overriden and cannot also be abstract final attributes cannot change they must be defined (using initialization block or in constructor) the reference they store cannot be changed once defined 7

abstract classes why use abstract classes? abstract GameCharacters User abstract Game Friend Foe provides interface between implementation and users of code helps modularize code 8

let s take a break... for 5 minutes 9

interfaces Java interfaces contain method declarations (without definition) are public by default (you can omit this) implicitly abstract (you do not write this) contain constant attributes are public static final by default (can omit this) can extend any number of other interfaces are valid reference type but cannot be instantiated (like an abstract class) 10

interfaces public interface Flyable{ } int C = 299792458; boolean takeoff(); boolean fly(); boolean land(); provides a contract between code and users of code specifies behaviour does not specify state any class can implement this interface 11

abstract class vs interface abstract class valid reference data type not instantiable static and instance attributes method declarations and definitions defines identity of children (is-a) class extends 0,1 abstract class all subclasses are (very) related Java interface valid reference data type not instantiable public static final constants method declarations only can define identity (is-a) often defines abilities (-able) class implements n 0 interfaces independent classes implement same interface 12

abstract class vs interface abstract class valid reference data type not instantiable static and instance attributes method declarations and definitions defines identity of children (is-a) class extends 0,1 abstract class all subclasses are (very) related Java interface valid reference data type not instantiable public static final constants method declarations only can define identity (is-a) often defines abilities (-able) class implements n 0 interfaces independent classes implement same interface 12

data types 13

data types 13

generics allow us to create classes that uses any data type (non-primitive) public class Box<E>{ public E data; } public Box(E object){ this.data = object; } public class Box{ public Object data; } public Box(Object object){ this.data = object; } we could already do this by storing Objects this involves a lot of overhead checking types casting 14

generics allow us to create methods that use/input/return any data type (non-primitive) public static <T,K> T foo(int x, K[] k){ if( k.length > x ){ return new T(x); }else{ return new T(k[0].toString()); } } how could we do this with generics? 15

generics allow us to create methods that use/input/return any data type (non-primitive) public static <T,K> T foo(int x, K[] k){ if( k.length > x ){ return new T(x); }else{ return new T(k[0].toString()); } } how could we do this with generics? Java 8... (pass methods as input arguments) 15

generics generics can be a very useful tool in Java (and other languages) allows for generic algorithms eliminates casts provides stronger compile time type checks only work with Objects! (no primitive data types) 16

let s take a break... for 5 minutes 17

this again... this has two uses a reference to the current object used in instance methods cannot be used in static methods 18

this again... this has two uses a reference to the current object used in instance methods cannot be used in static methods used to call a constructor from within a constructor 18

this again... this has two uses a reference to the current object used in instance methods cannot be used in static methods used to call a constructor from within a constructor public Box(String name, int id){ // constructor this.name = name; this.id = id; } public Box(){ this("no name", -1); } public Box me(){ return this; } // constructor // method 18

initialization there are several ways to do initialization constructors (for objects) initialization blocks code gets added to constructors static initialization blocks 19

initialization there are several ways to do initialization constructors (for objects) initialization blocks code gets added to constructors static initialization blocks public class Box{ static String s1; String s2; static { System.out.println("static initialization block") } { } System.out.println("static initialization block") s2 = s1 + s1;... 19

inheritance access modifiers public class access, package access, subclass access, world access protected class access, package access, subclass access none (friendly) class access, package access private class access 20

inheritance access modifiers public class access, package access, subclass access, world access protected class access, package access, subclass access none (friendly) class access, package access private class access which modifier should you use? 20

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. 21

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... 21

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier 21

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) 21

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... 21

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... for(string s: list) 21

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... for(string s: list) for each String s in list,... 21

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... for(string s: list) for each String s in list,... for(arraylist<string> list: biglist) 21

for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... for(string s: list) for each String s in list,... for(arraylist<string> list: biglist) for each ArrayList<String> list in biglist,... 21

abstract data types an abstract data type or ADT is a collection of data a set of operations on that data possibly specified runtimes for the operation 22

let s take a break... for 5 minutes 23

some fundamental ADTs stack 24

some fundamental ADTs stack ordered collection of data (top is the last item added ) 24

some fundamental ADTs stack ordered collection of data (top is the last item added ) push (add an element to the top) pop (remove top element) peek (look at the top) isempty (is the stack empty) 24

some fundamental ADTs stack ordered collection of data (top is the last item added ) push (add an element to the top) pop (remove top element) Last In First Out (LIFO) peek (look at the top) isempty (is the stack empty) 24

some fundamental ADTs queue 25

some fundamental ADTs queue ordered collection of data (front and back) 25

some fundamental ADTs queue ordered collection of data (front and back) enqueue (add an element to the back) dequeue (remove element from the front) peek (look at the front) isempty (is the queue empty) 25

some fundamental ADTs queue ordered collection of data (front and back) enqueue (add an element to the back) dequeue (remove element from the front) First In First Out (FIFO) peek (look at the front) isempty (is the queue empty) 25

some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO 26

some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO Why do we need (or want) restricted lists? 26

some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO Why do we need (or want) restricted lists? forces the LIFO or FIFO principal might be more efficient 26

some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO Why do we need (or want) restricted lists? forces the LIFO or FIFO principal prevents intended misuse (cheating) prevents unintended misuse (mistakes) might be more efficient 26

some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO Why do we need (or want) restricted lists? forces the LIFO or FIFO principal prevents intended misuse (cheating) prevents unintended misuse (mistakes) might be more efficient array vs linked lists (we ll look at this soon) 26

some fundamental ADTs priority queue a queue in which removal is based on a priority (regardless of when added) highest priority item removed first emergency room in a hospital uses a priority queue deque a double-ended queue allows arbitrary adding/removing from front and back set an unordered collection of data add, remove, check for membership, size, etc 27

some fundamental ADTs map (or dictionary) 28

some fundamental ADTs map (or dictionary) a collection of (key,value) pairs keys are unique 28

some fundamental ADTs map (or dictionary) a collection of (key,value) pairs keys are unique mutable keys are dangerous some maps have order and some do not 28