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

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

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

Lecture Data Structure Stack

Basic Data Structures

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Basic Data Structures 1 / 24

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

LIFO : Last In First Out

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

Top of the Stack. Stack ADT

ITI Introduction to Computing II

Stacks and Queues

Stacks and Queues. David Greenstein Monta Vista

[2:3] Linked Lists, Stacks, Queues

Stacks, Queues (cont d)

Week 6. Data structures

CMSC 132: Object-Oriented Programming II. Stack and Queue

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Summer Final Exam Review Session August 5, 2009

Midterm Exam (REGULAR SECTION)

Linked Lists

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

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

Stacks and Queues. Chapter Stacks

Data Structures And Algorithms


infix expressions (review)

Associate Professor Dr. Raed Ibraheem Hamed

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CSE 230 Computer Science II (Data Structure) Introduction

== isn t always equal?

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Data Structure. Recitation VII

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

CS 206 Introduction to Computer Science II

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Fun facts about recursion

Stacks Fall 2018 Margaret Reid-Miller

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

Top of the Stack. Stack ADT

CS 211 Programming Practicum Spring 2018

Stack as an ADT. COMP 182: Data Structures and Program Design Lecture Slides 8. Stack as an Idea

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

Abstract Data Types Spring 2018 Exam Prep 5: February 12, 2018

CS24 Week 4 Lecture 2

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Lecture 12 ADTs and Stacks

CS/ENGRD 2110 SPRING 2018

Data Abstraction and Specification of ADTs

Exceptions and Design

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

Stack and Queue. Stack:

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

1. Stack Implementation Using 1D Array

Introduction to Computer Science I

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

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

CS 211 Programming Practicum Fall 2018

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

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

CSC148 Week 2. Larry Zhang

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

Abstract Data Types. Abstract Data Types

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

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

Standard ADTs. Lecture 19 CS2110 Summer 2009

UNIT-2 Stack & Queue

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Queues. Stacks and Queues

ITEC2620 Introduction to Data Structures

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Queues Fall 2018 Margaret Reid-Miller

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Postfix (and prefix) notation

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Midterm Exam 2 CS 455, Spring 2011

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

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

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

CS 211 Programming Practicum Spring 2017

Topic 6: Inner Classes

Project 1: Implementation of the Stack ADT and Its Application

More Data Structures (Part 1) Stacks

Linked Structures. See Section 3.2 of the text.

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

CS1020: DATA STRUCTURES AND ALGORITHMS I

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

CS 302 ALGORITHMS AND DATA STRUCTURES

C Sc 227 Practice Test 2 Section Leader Name 134pts

// The next 4 functions return true on success, false on failure

Queues. CITS2200 Data Structures and Algorithms. Topic 5

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

1 P age DS & OOPS / UNIT II

Stack and Its Implementation

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Transcription:

CS 11 Introduction to Computing II Wayne Snyder Department Boston University Today Object-Oriented Programming Concluded Stacks, Queues, and Priority Queues as Abstract Data Types Reference types: Basic Principles of References/Pointers String type as a reference type Array resizing Next Time Queues continued: Implementing a Queue with a Ring (Circular) Buffer Object-Oriented Design Summar The most important things to remember about Object-Oriented Design are: Ø Divide up your problem and its solution into parts (=classes & objects). Ø When you divide, make the interactions (method calls and field references) as simple and easy to understand as possible; Ø Make the interface follow KISS -- provide as few public methods as possible; Ø Use Information Hiding: Hide as much about your implementation as you possibly can. If you are not sure whether to make something public or private, make it private; The advantages of information hiding are: Ø Your code is easier to understand, and hence to use, and reuse; Ø Users can t get used to back-door ad-hoc features of your code; Ø By separating the (simple) behavior of your system from the messy details of its implementation, you can change the actual implementation any time you want---as long as it behaves the same, this is a huge advantage for maintenance and reuse. 1

Object-Oriented Design: Design Patterns Over the years, system designers have defined a number of standard design patterns for the parts and interactions of a program. The most basic pattern is a single file implementing a simple task: Stand-Alone Program: Printday static main( ) Rules: Ø Everything is static; Ø Main( ) is public; Ø ALL OTHER members are private; Ø Uses no libraries! Object-Oriented Design: Design Patterns A stand-alone program is not very useful! More common is a program which uses the standard Java libraries as a Client to accomplish some task: Client: Histogram Scanner static main( ) Client Rules: Ø Everything is static; Ø Main( ) is public & controls execution; Ø ALL OTHER members are private; Ø Uses standard Java libraries. 4

Object-Oriented Design: Design Patterns A client may use standard Java libraries (Scanner, Math, String, Character,.) or may use a static library written by the user: Static Librar BigInt Client: HW0Client static add( ) static main( ) Client Rules: Ø Everything is static; Ø Main( ) is public & controls execution; Ø ALL OTHER members are private; Ø May use standard Java libraries; Ø May use programmer-defined static libraries; Ø Does not define any objects. Static Library Rules: Ø Everything is static; Ø Interface is small & public; Ø Implementation is private; Ø Stores no local data; Ø May itself use libraries; Ø Static main used to store testing code. Object-Oriented Design: Design Patterns A client may also use one or more Objects it creates dynamically to hold data and related algorithms, called an Abstract Data Type: Client: HW0Client Static Librar BigInt static main( ) static add( ) ADT: S S = [,,4, ] union( ) intersection(..) Static Library Rules: Ø Everything is static; Ø Ø Ø Ø Ø Interface is small & public; Implementation is private; Stores no local data; May itself use libraries; Static main used to store testin Client Rules: Ø Everything is static; Ø Main( ) is public; Ø ALL OTHER members are private; Ø May use standard Java libraries; Ø May use programmer-defined static libraries; Ø Defines objects as Abstract Data Types. Abstract Data Type Rules: Ø Interface is small & public; Ø Implementation is private; Ø Stores data and related algorithms; Ø May itself use libraries; Ø Main used to store testing code, and is only static member 6

Object-Oriented Design: Design Patterns Sometimes this is called the Client/Server Model: Math Client: Controls execution of whole program. 1,, sum(..) Servers (store data with associated algorithms used by client) BigInt static main( ) [,, ] reverse(..) Libraries: Store code used by client. Wayne get( ) Stack Interface (informal): void Push(int n) Put integer n on top of the stack int Pop() Remove top integer and return it int Peek() Return the top integer without removing it int size() Return the number of integers in the stack boolean isempty() Returns true iff stack has no members 8 4

Stack Interface: push( ); void Push(int n) int Pop() int Peek() int size() boolean isempty() 9 Stack Interface: push( ); push( ); void Push(int n) int Pop() int Peek() int size() boolean isempty() 10

Stack Interface: push( ); push( ); push( ); void Push(int n) int Pop() int Peek() int size() boolean isempty() 11 Stack Interface: push( ); push( ); push( ); int n = pop(); void Push(int n) int Pop() int Peek() int size() boolean isempty() n: 1 6

Stack Interface: push( ); push( ); push( ); int n = pop(); int m = pop(); n: void Push(int n) int Pop() int Peek() int size() boolean isempty() m: 1 Stack Interface: push( ); push( ); push( ); int n = pop(); int m = pop(); int i = pop(); n: void Push(int n) int Pop() int Peek() int size() boolean isempty() m: i: 14

Applications of Stacks: Reversing an array or a String Keeping track of nested or recursive structure Parenthesis Matching Evaluating an arithmetic expression Run-time Stack to keep track of method/function calls [Examples on Board] Applications of Stacks: Reversing an array or a String Keeping track of calls waiting Parenthesis Matching Expression Evaluation Run-time Stack to keep track of method/function calls Problems with Stacks: Underflow: Trying to pop() or peek() and empty stack! Solution: check if empty before doing a peek or pop! Overflow: Pushing too many numbers and causing an ArrayIndexOutOfBoundsException! Solution: Array Resizing.. 8

Reference types: Data in Computer Memory To understand the notion of references (also called pointers), we need to understand how computer memory works to organize data: RAM: 0 1 1 4-4 6 6 8 9 10 10-8 11 1 4 1 14 1-1 16 Computer instructions say things like: Put a in location 8: RAM[8] = ; Add the numbers in locations 8 and 9 and put the sum in location : RAM[] = RAM[8] + RAM[9] This is why arrays are so common and so efficient: RAM is just a big array! Access time = about 10 - secs 1 Reference types: Data in Computer Memory When you create variables in Java (or any programming language), these are nicknames or shortcut ways of referring to locations in RAM: RAM: These shortcut names for primitive types can not change during execution. z: 0 1 4 6 8 9 10 11 1 1 14 1 16 1-4 6 10-8 4-1 int x; int y; int z; // same as RAM[8] // same as RAM[9] // same as RAM[] // now the previous computation // would be x = ; y = 10; z = x + y; When we draw our diagrams of variables, we are really just giving a shortcut view of RAM without the addresses: 18 9

Reference types: Objects/Classes in Computer Memory BUT Reference Types (arrays, Strings, objects anything you can use the word new to create new instances of) are references or pointers to their values: they store the location of the value, not the value itself. z: A: P: 0 1 1 4 10 6 14 8 9 10 10 11 11 1 4 1 8 14 1-1 16 int x; int y; int z; int [] A = { 11,, 4 }; Point P = new Point(, -1); A A[0]: A[1]: A[]: P -1 11 4 19 Reference types: Objects/Classes in Computer Memory Now we can change the meaning of the reference variable by assigning it a new location; in fact, new returns the new location, which is stored in the reference variable as its value. z: A: P: 0 1 1 4 0 6 0 0 8 9 10 10 11 11 1 4 1 8 14 1-1 16... int [] A = { 11,, 4 }; Point P = new Point(, -1); A = new int[]; P = new Point(,); A A[0]: A[1]: P 0 0 0 10

Reference types: Objects/Classes in Computer Memory Now we can change the meaning of the reference variable by assigning it a new location; in fact, new returns the new location, which is stored in the reference variable as its value. z: A: P: Old objects are garbage and the memory will be reclaimed by the garbage collection and reused. 0 1 1 4 0 6 0 0 8 9 10 10 11 11 1 4 1 8 14 1-1 16... int [] A = { 11,, 4 }; Point P = new Point(, -1); A = new int[]; P = new Point(,); A A[0]: A[1]: P 0 0 1 Reference Types: String We have seen two different reference types so far in this course: The first is Strings: 11

Reference Types: String We have seen two different reference types so far in this course: The first is Strings: equals checks for same structure; == checks for same reference (pointing to same location). Reference Types: String We have seen two different reference types so far in this course: The second is arrays: Let s look at how to solve the problem of stack overflow, using array resizing: 4 1