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

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

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

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

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

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

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

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

JAVA MOCK TEST JAVA MOCK TEST II

csci 210: Data Structures Stacks and Queues

The Java Collections Framework. Chapters 7.5

Introduction to Programming Using Java (98-388)

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

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

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

Lecture 4. The Java Collections Framework

Interfaces & Generics

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

== isn t always equal?

CT 229 Object-Oriented Programming Continued

Standard ADTs. Lecture 19 CS2110 Summer 2009

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

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

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

1 P age DS & OOPS / UNIT II

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Algorithms and Data Structures

Week 6. Data structures

Basic Data Structures

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

CS24 Week 4 Lecture 2

Basic Data Structures 1 / 24

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

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

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

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

Stacks and Queues

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

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Inheritance. Transitivity

DATA STRUCTURE AND ALGORITHM USING PYTHON

Software Design and Analysis for Engineers

Chapter 1: Object-Oriented Programming Using C++

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

COP 3330 Final Exam Review

CS/ENGRD 2110 SPRING 2018

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

Inheritance and Substitution (Budd chapter 8, 10)

CSE 2123: Collections: Priority Queues. Jeremy Morris

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Abstract Data Types. Abstract Data Types

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Data Abstraction and Specification of ADTs

CS 231 Data Structures and Algorithms, Fall 2016

PRIORITY QUEUES AND HEAPS

COMP 401 Fall Recitation 7: Factories and Lists

Linked List. April 2, 2007 Programming and Data Structure 1

3137 Data Structures and Algorithms in C++

CMSC 341 Lecture 14: Priority Queues, Heaps

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

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

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CS260 Intro to Java & Android 03.Java Language Basics

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

HEAPS & PRIORITY QUEUES

WA1278 Introduction to Java Using Eclipse

Inheritance, Polymorphism and the Object Memory Model

Stacks Fall 2018 Margaret Reid-Miller

COS 226 Algorithms and Data Structures Fall Midterm

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

CS 206 Introduction to Computer Science II

CS 221 Review. Mason Vail

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

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

Arrays and Linked Lists

Classes, subclasses, subtyping

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Computer Programming II C++ (830)

Summer Final Exam Review Session August 5, 2009

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.


final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

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

182 review 1. Course Goals

CS18000: Problem Solving And Object-Oriented Programming

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

What is Inheritance?

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Transcription:

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

today s agenda assignments Assignment 3 is due on Monday a quick look back abstract classes and interfaces casting objects abstract data types 2

last time... abstract classes interfaces quiz 3 3

casting objects... class Parent{ class Child extends Parent{ void foo(){...} void bar(){...} } } Parent pp = new Parent(); Parent pc = new Child(); Child cc = new Child(); pp.foo(); pc.foo(); cc.foo(); pp.bar(); pc.bar(); cc.bar(); 4

casting objects... class Parent{ class Child extends Parent{ void foo(){...} void bar(){...} } } Parent pp = new Parent(); Parent pc = new Child(); Child cc = new Child(); pp.foo(); pc.foo(); cc.foo(); pp.bar(); pc.bar(); cc.bar(); 5

casting objects... class Parent{ class Child extends Parent{ void foo(){...} void bar(){...} } } Parent pp = new Parent(); Parent pc = new Child(); Child cc = new Child(); pp.foo(); pc.foo(); cc.foo(); // pp.bar(); we can cast the Parent back to a Child ((Child)pc).bar(); ONLY because it was created (new) cc.bar(); as a Child We can cast down the hierarchy chain if the object really is what we want to cast it to. 6

casting objects... casting down the hierarchy chain will often violate good OOP design/practice class Parent{ class Child extends Parent{ void foo(){...} void bar(){...} } } Parent[] pp = new Parent[10]; for(int i=0; i<10; i+=1){ if(math.random() < 0.5){ pp[i] = new Parent(); } else{ pp[i] = new Child(); } } for(parent p : pp){ ((Child)p).bar(); } // this will not work 7

casting objects... casting down the hierarchy chain will often violate good OOP design/practice class Parent{ class Child extends Parent{ void foo(){...} void bar(){...} } } Parent[] pp = new Parent[10]; for(int i=0; i<10; i+=1){ if(math.random() < 0.5){ pp[i] = new Parent(); } else{ pp[i] = new Child(); } } for(parent p : pp){ if(p instanceof Child) ((Child)p).bar(); } // this will work 8

now let s look at date types/abstract data types/data structures 9

data types primitive data types byte, short, int, long (integer types) float, double (approximate real number types) char, boolean variables store the actual data reference data types classes (abstract and concrete) arrays interfaces enums variables store references to where the actual data is 10

data structures What is a data structure? a data structure is a systematic approach to storing and accessing data so that it can be used efficiently for a specific purpose in Java, data structures are classes (or a primitive data type) 11

abstract data types an abstract data type or ADT is data and operations on that data that are precisely specified independent of any implementation the operations may or may not have efficiency guarantees an ADT is a mathematical construct. We simulate them as APIs or interfaces. an ADT is NOT an implementation of storing data and operations on that data in any programming language a specific way to store data specific ways (algorithms) to act on the data For example, the integers from a math class is an abstract data type. In Java, we use ints or Integers. 12

abstract data types a data structure is the implementation of an abstract data type real world ADT data structure (class) for us, abstract data type and interface (contract) will mean the same thing ADT/interface tells us what can be done with the data API, abstract class, java interface should not reveal how the data is stored data structure/implementation is how those things are done concrete class the actual code COMP2402 will focus on ADTs and data types 13

abstract data types good object-oriented design will separate the interface and the implementation Why? 14

some fundamental ADTs the list ADT 15

some fundamental ADTs the list ADT ordered collection of data x 0,x 1,...,x n 1 15

some fundamental ADTs the list ADT ordered collection of data x 0,x 1,...,x n 1 look at what is in the list size of the list add to the list remove from the list ask if the list is empty create a new list 15

some fundamental ADTs the stack ADT 16

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

some fundamental ADTs the stack ADT 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) 16

some fundamental ADTs the stack ADT 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) 16

some fundamental ADTs the queue ADT 17

some fundamental ADTs the queue ADT ordered collection of data (front and back) 17

some fundamental ADTs the queue ADT 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) 17

some fundamental ADTs the queue ADT 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) 17

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

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

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

some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO principle queue removes items according to FIFO principle 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 18

some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO principle queue removes items according to FIFO principle 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) 18

some fundamental ADTs priority queue a queue in which removal is based on a priority (regardless of when added) highest/lowest 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, ismember, size, etc 19

some fundamental ADTs the map (or dictionary) ADT 20

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

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

abstract data types why use ADTs? 21

abstract data types why use ADTs? good ADTs provide a model of real things (abstraction) capture the essence of the real things the data capture the fundamental behaviour/operation of the real things the operations on the data 21

abstract data types why use ADTs? good ADTs provide a model of real things (abstraction) capture the essence of the real things the data capture the fundamental behaviour/operation of the real things the operations on the data will lead to the interface/contract for the actual data type 21

abstract data types why use ADTs? good ADTs provide a model of real things (abstraction) capture the essence of the real things the data capture the fundamental behaviour/operation of the real things the operations on the data will lead to the interface/contract for the actual data type when creating data types from ADTs interface/contract is mostly defined abstract class or Java interface encapsulation comes for free changing data representation does not affect users of data type changing implementations does not affect users of the data type 21

abstract data types all the basic container ADTs are implemented in Java (some have multiple implementations) list ArrayList<T>, LinkedList<T> priority queue PriorityQueue<T> set HashSet<T>, TreeSet<T> dictionary Dictionary<K,V> 22

abstract data types let s implement a string list ADT data an ordered collection of strings x 0,x 1,...,x n 1 operations size() - returns the number of strings in the list n get(i) - returns string at given position x i set(i, s) - replaces element at given position with given data x i = s add(i, s) - adds a string at a given position in the list make a hole by shifting elements and insert in the hole remove(i) - removes and returns the element in the given position remove the item and then close the resulting gap 23

linked lists a linked list is a sequence of nodes a node stores data and a reference to the next node in the sequence (or indicates that that there is no next node) the head of the linked list is the first node and the tail is the last note: the diagram represents a linked list with four nodes. 24

some review slides (in progress) 25

abstract... abstract methods a method declared without a definition public abstract int foo(string[] in); forces the class to be abstract as well cannot be final abstract classes cannot be instantiated may or may not contain abstract methods are valid reference types and can be subclassed cannot be final concrete classes all methods (declared or inherited) must be defined can be instantiated (all objects other than arrays are instantiations of concrete classes) is a valid data reference type 26

final... final attributes value cannot be changed once it is defined must be defined in constructor or initialization block primitive data types, strings and immutable data types are constants final methods cannot be overridden cannot be abstract final classes cannot be extended cannot be abstract 27

access modifiers... modifier class package subclass world public protected none (default) private everything is accessible from within the class a class in the same package has access to everything except private members a subclass has access to public and protected members everyone else only has access to public members 28

arrays... an array is a container that store a collection of items of the same type int[] intarray; intarray = new intarray[12]; // variable declaration // allocation of memory in heap for array intarray[0] = 13; //... // population of the array with data intarray[11] = 163; // When you declare an array variable you can also initialize it using {...}. This only works when you declare the variable. /* array declaration, allocation and initialization */ int[] intarray = {1,3,5,7,9}; /* all of these are equivalent */ String[] words = {"cat", "dog", "eel"}; String[] words = new String[]{"cat", "dog", "eel"}; String[] words = new String[3]{"cat", "dog", "eel"}; 29

arrays 30

creation and initialization an array is a container that store a collection of items of the same type int[] intarray; intarray = new intarray[12]; // variable declaration // allocation of memory in heap for array intarray[0] = 13; //... // population of the array with data intarray[11] = 163; // When you declare an array variable you can also initialize it using {...}. This only works when you declare the variable. /* array declaration, allocation and initialization */ int[] intarray = {1,3,5,7,9}; /* all of these are equivalent */ String[] words = {"cat", "dog", "eel"}; String[] words = new String[]{"cat", "dog", "eel"}; String[] words = new String[3]{"cat", "dog", "eel"}; 31

partially filled array sometimes it is useful to have an array that is not completely filled /* allocate memory to store 10 ints */ Integer[] numbers = new Integer[10]; /* partially fill the array */ numbers[0] = 1; numbers[1] = 2; numbers[2] = 4; numbers[3] = 8; when we use a partially filled array there are two things we need to keep track of when using the array the capacity of the array. This tells us how many things the array can possibly store. The length attribute of an array tells us this. the size of the array. This is the number of things (data) stored in the array. We will need to have a variable (attribute) to manually keep track of this. 32

partially filled array sometimes it is useful to have an array that is not completely filled /* allocate memory to store 10 ints */ Integer[] numbers = new Integer[10]; int size = 0; // number of ints in the array /* partially fill the array */ numbers[0] = 1; numbers[1] = 2; numbers[2] = 4; numbers[3] = 8; size = 4; // we need to update this as we add/remove from the array there are several ways that we can keep our data in the array 33