Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
CMPT 225 Abstract Data Types.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Two 2x2 (block) matrices can be multiplied C==AB
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.
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Design The goal is to design a modular solution, using the techniques of: Decomposition Abstraction Encapsulation In Object Oriented Programming this is.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
© 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.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Chapter 1 Principles of Programming and Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved 4-1 Chapter 4 Data Abstraction: The Walls.
Data Abstraction: The Walls
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
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.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 3: Data Abstraction: The Walls Data Abstraction & 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.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
April 24, 2017 Chapter 4 Data Abstraction The Walls
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Comp 245 Data Structures (A)bstract (D)ata (T)ypes ADT.
Data Abstaraction Chapter 10.
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.
Introduction to Classes and Objects. Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
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.
Topic 2 Collections. 2-2 Objectives Define the concepts and terminology related to collections Discuss the abstract design of collections.
Chapter 2 Principles of Programming and Software Engineering.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Unit 1 - Introducing Abstract Data Type (ADT) Part 1.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Principles of Programming & Software Engineering
Data Abstraction: The Walls
Abstract Data Types (ADT)
Data Abstraction: The Walls
Data Abstraction: The Walls
Chapter 1 Data Abstraction: The Walls
Principles of Programming and Software Engineering
Chapter 3: Using Methods, Classes, and Objects
Data Abstraction A technique for increasing the modularity of a program The need to support several operations on the data arise need to define abstract.
Ch. 4 Data Abstraction Modular programming Procedural abstraction
Abstract Data Types (ADT)
Data Abstraction: The Walls
Chapter 2. Problem Solving and Software Engineering
Data Abstraction: The Walls
Lecture 3 – Data collection List ADT
Presentation transcript:

Chapter 4 Data Abstraction: The Walls

© 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a large program manageable by systematically controlling the interaction of its components –Isolates errors –Eliminates redundancies –A modular program is Easier to write Easier to read Easier to modify

© 2004 Pearson Addison-Wesley. All rights reserved4-3 Abstract Data Types Procedural abstraction –Separates the purpose and use of a module from its implementation –A module’s specifications should Detail how the module behaves Identify details that can be hidden within the module Information hiding –Hides certain implementation details within a module –Makes these details inaccessible from outside the module

© 2004 Pearson Addison-Wesley. All rights reserved4-4 Abstract Data Types Figure 4.1 Isolated tasks: the implementation of task T does not affect task Q

© 2004 Pearson Addison-Wesley. All rights reserved4-5 Abstract Data Types The isolation of modules is not total –Methods’ specifications, or contracts, govern how they interact with each other Figure 4.2 A slit in the wall

© 2004 Pearson Addison-Wesley. All rights reserved4-6 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 a data collection Data abstraction –Asks you to think what you can do to a collection of data independently of how you do it –Allows you to develop each data structure in relative isolation from the rest of the solution –A natural extension of procedural abstraction

© 2004 Pearson Addison-Wesley. All rights reserved4-7 Abstract Data Types Abstract data type (ADT) –An ADT is composed of A collection of data A set of operations on that data –Specifications of an ADT indicate What the ADT operations do, not how to implement them –Implementation of an ADT Includes choosing a particular data structure + algorithms

© 2004 Pearson Addison-Wesley. All rights reserved4-8 Abstract Data Types Data structure –A construct that is defined within a programming language to store a collection of data –Example: arrays ADTs and data structures are not the same Data abstraction –Results in a wall of ADT operations between data structures and the program that accesses the data within these data structures

© 2004 Pearson Addison-Wesley. All rights reserved4-9 Abstract Data Types Figure 4.4 A wall of ADT operations isolates a data structure from the program that uses it

© 2004 Pearson Addison-Wesley. All rights reserved4-10 Specifying ADTs In a list (sequence of items) –Except for the first and last items, each item has A unique predecessor A unique successor –Head or front Does not have a predecessor –Tail or end Does not have a successor Figure 4.5 list A grocery

© 2004 Pearson Addison-Wesley. All rights reserved4-11 The ADT List ADT List operations –Create an empty list –Determine whether a list is empty –Determine the number of items in a list, i.e., size –Add an item at a given position in the list –Remove the item at a given position in the list –Remove all the items from the list –Retrieve (get) the item at a given position in the list –Display all the items in the list Items are referenced by their position within the list

© 2004 Pearson Addison-Wesley. All rights reserved4-12 The ADT List Specifications of the ADT operations –Define the contract for the ADT list –Do not specify how to store the list or how to perform the operations ADT operations can be used in an application without the knowledge of how the operations will be implemented Pseudo code on page 113

© 2004 Pearson Addison-Wesley. All rights reserved4-13

© 2004 Pearson Addison-Wesley. All rights reserved4-14 The ADT Sorted List The ADT sorted list –Maintains items in sorted order –Inserts and deletes items by their values, not their positions Pseudo code on pages 184, 857, and 858

© 2004 Pearson Addison-Wesley. All rights reserved4-15

© 2004 Pearson Addison-Wesley. All rights reserved4-16 ADT Sorted List – Pre and Postconditions

© 2004 Pearson Addison-Wesley. All rights reserved4-17

© 2004 Pearson Addison-Wesley. All rights reserved4-18 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 a problem require? –What operations does a problem require?

© 2004 Pearson Addison-Wesley. All rights reserved4-19 Implementing ADTs Choosing the data structure to represent the ADT’s data is a part of implementation –Choice of a data structure depends on Details of the ADT’s operations Context in which the operations will be used Implementation details should be hidden behind a wall of ADT operations –A program would only be able to access the data structure using the ADT operations

© 2004 Pearson Addison-Wesley. All rights reserved4-20 Implementing ADTs Figure 4.8 ADT operations provide access to a data structure

© 2004 Pearson Addison-Wesley. All rights reserved4-21 Implementing ADTs Figure 4.9 Violating the wall of ADT operations

Java Mechanism for ADT Specification The specification is documented as an "interface". –This interface declaration is provided in a separate file (in some package) and contains the public portion of an associated class declaration. –In a second file the class that "implements" this interface is declared and implemented. 3-22

Java Mechanism for ADT Specification Alternation: Abstract Base Class –An abstract base class can be declared with all public methods and no implementation. –A concrete class is declared in a second file that implements these methods and in general declares and implements data members and other non-public methods. 3-23

Java Mechanism for ADT Specification For the most part we will adopt the "interface" approach to a specification. We will also assume that all user defined interfaces and classes exists in a single (default) package. 3-24

Example // **************************************** // File name: SphereInterface.java // Interface SphereInterface for the ADT Sphere // **************************************** public interface SphereInterface { public void setRadius(double newRadius); // Sets (alters) the radius of an existing sphere. // Precondition: newRadius is the desired radius. // Postcondition: The sphere’s radius is newRadius. public double radius(); // Determines a sphere's radius. // Precondition: None. // Postcondition: Returns the radius. public double diameter(); // Determines a sphere's diameter. // Precondition: None. // Postcondition: Returns the diameter. ADT Sphere –Data –Operations Interface

Example (cont’d) public double circumference(); // Determines a sphere's circumference. // Precondition: None. // Postcondition: Returns the circumference. public double area(); // Determines a sphere's surface area. // Precondition: None. // Postcondition: Returns the surface area. public double volume(); // Determines a sphere's volume. // Precondition: None. // Postcondition: Returns the volume. public void displayStatistics(); // Displays statistics of a sphere. // Precondition: Assumes System.out is available. // Postcondition: None. } // end SphereInterface Interface (cont’d)

Example (cont’d) // ***************************************** // File name: Sphere.java // An implementation of the ADT Sphere. // ***************************************** public class Sphere implements SphereInterface { private double theRadius; public Sphere() { // Default constructor: Creates a sphere and // initializes its radius to a default value. // Precondition: None. // Postcondition: A sphere of radius 1 exists. setRadius (1.0); } // end default constructor public Sphere(double initialRadius) { // Constructor: Creates a sphere and initializes its radius. // Precondition: initialRadius is the desired radius. // Postcondition: A sphere of radius initialRadius exists. setRadius (initialRadius); } // end constructor Implementation

public double area() { return 4.0 * Math.PI * theRadius * theRadius; } // end area public double volume() { return (4.0*Math.PI * Math.pow(theRadius, 3.0)) / 3.0; } // end volume public void displayStatistics() { System.out.println("\nRadius = " + radius() + "\nDiameter = " + diameter() + "\nCircumference = " + circumference() + "\nArea = " + area() + "\nVolume = " + volume()); } // end displayStatistics } // end Sphere Implementation (cont’d, spec. omitted for space) public void setRadius(double newRadius) { if (newRadius >= 0.0) { theRadius = newRadius; } // end if } // end setRadius public double radius() { return theRadius; } // end radius public double diameter() { return 2.0 * theRadius; } // end diameter public double circumference() { return Math.PI * diameter(); } // end circumference

Example (cont’d) // ******************************************************** // File name: SphereDriver.java // An example of using the ADT Sphere. // ******************************************************** public class SphereDriver { static public void main(String[] args) { Sphere sphere1 = new Sphere(); sphere1.setRadius(2.5); System.out.println("The radius of sphere1 is " + sphere1.radius()); System.out.println("The diameter of sphere1 is " + sphere1.diameter()); System.out.println("The circumference of sphere1 is " + sphere1.circumference()); System.out.println("The area of sphere1 is " + sphere1.area()); System.out.println("The volume of sphere1 is " + sphere1.volume()); Application

Example (cont’d) System.out.println("The statstics of sphere1:"); sphere1.displayStatistics(); Sphere sphere2 = new Sphere(5.0); System.out.println("\n\n"); System.out.println("The radius of sphere2 is " + sphere2.radius()); System.out.println("The diameter of sphere2 is " + sphere2.diameter()); System.out.println("The circumference of sphere2 is " + sphere2.circumference()); System.out.println("The area of sphere2 is " + sphere2.area()); System.out.println("The volume of sphere2 is " + sphere2.volume()); System.out.println("\nThe statstics of sphere2:"); sphere2.displayStatistics(); } Application (cont’d)

© 2004 Pearson Addison-Wesley. All rights reserved4-31 An Array-Based Implementation of the ADT List An array-based implementation –A list’s items are stored in an array items –A natural choice Both an array and a list identify their items by number –A list’s k th item will be stored in items[k-1]

© 2004 Pearson Addison-Wesley. All rights reserved4-32 An Array-Based Implementation of the ADT List Figure 4.11 An array-based implementation of the ADT list

An Array-Based Implementation of the ADT List Insertion –Figure 4-12 Deletion –Figure 4-13 Code –Pages 209 –

© 2004 Pearson Addison-Wesley. All rights reserved