Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Chapter 4 Data Abstraction: The Walls

2 © 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

3 © 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

4 © 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

5 © 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

6 © 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

7 © 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

8 © 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

9 © 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

10 © 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

11 © 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

12 © 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

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

14 © 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

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

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

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

18 © 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?

19 © 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

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

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

22 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

23 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

24 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

25 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

26 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)

27 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

28 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

29 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

30 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)

31 © 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]

32 © 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

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

34 © 2004 Pearson Addison-Wesley. All rights reserved4-34 4-12 4-13


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

Similar presentations


Ads by Google