Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 3 Data.

Slides:



Advertisements
Similar presentations
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CHAPTER 13 Graphs DATA ABSTRACTION AND PROBLEM SOLVING WITH C++ WALLS AND MIRRORS Third Edition Frank M. Carrano Janet J. Prichard Data Abstraction and.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 2 Recursion:
CMPT 225 Abstract Data Types.
Abstract Data Types. Typical operations on data  Add data to a data collection  Remove data from a data collection  Ask questions about the data in.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 2006 Pearson Addison-Wesley. All rights reserved 4-1 Chapter 4 Data Abstraction: The Walls.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Data Abstraction: The Walls
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Implementations of the ADT Stack Chapter 7 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Modularity Keeps the complexity of a large program manageable by systematically controlling the interaction of its components Isolates errors Eliminates.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & Problem.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
April 24, 2017 Chapter 4 Data Abstraction The Walls
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Chapter 1 Data Abstraction: The Walls CS Data Structures Mehmet H Gunes Modified from authors’ slides.
COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.
1 Object-Oriented Programming Using C++ CLASS 6. 2 Specifying ADTs: The ADT List Except for the first and last items in a list, each item has a unique.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Data Abstraction: The Walls
Data Abstraction: The Walls
DATA ABSTRACTION AND PROBLEM SOLVING WITH C++
Abstract Data Types (ADT)
Data Abstraction: The Walls
Data Abstraction: The Walls
Chapter 1 Data Abstraction: The Walls
List Implementations Chapter 9
Data Abstraction: The Walls
Abstract Data Types (ADT)
Data Abstraction: The Walls
Array-Based Implementations
Data Abstraction: The Walls
Lecture 3 – Data collection List ADT
A List Implementation that Uses An Array
Presentation transcript:

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 3 Data Abstraction: The Walls DATA ABSTRACTION AND PROBLEM SOLVING WITH C++ WALLS AND MIRRORS Third Edition Frank M. Carrano Janet J. Prichard Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley

Outline Abstract data types (ADTs) Specifying ADTs (ADT list, ADT sorted list, designing an ADT) Implementing ADTs (C++ classes, C++ namespaces, array based implementation of ADTs, C++ exceptions)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley ADTs Modular program is easier to write, read and modify. Write specification for each module before implementing it. Use information hiding. ADT (collection of data and operations)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-1 Isolated tasks: the implementation of task T does not affect task Q

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-2 A slit in the wall

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley ADTs (contd.) Basic operations (add, remove, ask questions) Think always “what” instead of “how” Data structures are part of an ADT’s implementation Carefully specify an ADTs operations before implementing. ADTs vs. data structures.

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-3 A dispenser of chilled water, crushed ice, and ice cubes A program should not depend on the details of an ADT’s implementation

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-4 A wall of ADT operations isolates a data structure from the program that uses it

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Specifying an ADTs Head or front Tail or end Predecessor Successor Operations (add, remove, find, display)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-5 A grocery list

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley ADT List Create empty list Destroy a list Is the list empty ? Num. of elements in the list Insert an item at a given position in the list Delete an item Retrieve an element. ADT sorted list (maintains items in sorted order)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-6 UML diagram for ADT List

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-7 The wall between displayList and the implementation of the ADT list

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Designing an ADT What data the problem requires ? What operations ? Example: Determine the dates of all the holidays in a given year (data: dates, operations: determine if a date is holiday, determine the date of the day that follows a given date, determine whether a date is before an other date, etc.)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Axioms (aList.createList()).getLength()=0 (aList.insert(i,x)).getLength() = aList.getLength() + 1 (aList.remove(i)).getLength() = aList.getLength() – 1 (aList.createList()).is_empty() = true (aList.insert(i,item)).is_empty() = false (aList.createList()).remove(i) = error (aList.insert(i,x)).remove(i) = aList (aList.createList()).retrieve(i) = error (aList.insert(i,x)).retrieve(i) = x aList.retrieve(i) = (aList.insert(i,x)).retrieve(i+1) aList.retrieve(i+1)=(aList.remove(i)).retrieve(i)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Implementing an ADT Figure 3-8 ADT operations provide access to a data structure

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-9 Violating the wall of ADT operations, solution: use OOL such as C++

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-10 C++ classe. An object’s data and methods are encapsulated

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-11 An array-based implementation of the ADT list

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-12 Shifting items for insertion at position 3

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-13a (a) Deletion causes a gap

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Figure 3-13b (b) Fill gap by shifting

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley Array based impl. of an ADT Look at files ListA.h and ListA.cpp.

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley ADT implementation using exceptions Exception: mechanism for handling an error during execution C++: try-catch blocks Try block: statements that might cause an exception Each try block has one or more catch blocks (in case of multiple errors)

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley ADT implementation using exceptions(contd.) General syntax: try { statement(s); } catch (ExceptionClass identifier) { statement(s); } When error is detected, exception is thrown: throw ExceptionClass(stringArgument);

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley ADT implementation using exceptions(contd.) Use C++ exception class, or define your own. Example: a function whose code can throw an exception: void myMethod(int x) throw (BadArgException, MyException) { … if (x==MAX) throw BadArgExceotion(“BadArgException: reason”); … throw MyException (“MyException: reason”); … }

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley ADT implementation using exceptions(contd.) Previous array based implementation of ADT used success flag. Possible errors are: an out-of-bounds list index, insert into a full list, delete or retrieve from an empty list. 2 user defined exceptions: ListIndexOutOfRangeException, ListException Look at files: ListIndexOutOfRangeException.h, ListException.h, ListAexcept.h, ListAInsertexcept.cpp.

Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley SELF-TEST EXERCISES (page 157) Answers are on page A-112.