Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 4 Data Abstraction: The Walls 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 reserved 4-2 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 reserved 4-3 Abstract Data Types Figure 4.1 Isolated tasks: the implementation of task T does not affect task Q © 2004 Pearson Addison-Wesley. All rights reserved 4-4 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 reserved 4-5 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 reserved 4-6 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 reserved 4-7 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 reserved 4-8 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 reserved 4-9 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 reserved 4-10 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 reserved 4-11 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 reserved 4-12 © 2004 Pearson Addison-Wesley. All rights reserved 4-13 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 reserved 4-14 © 2004 Pearson Addison-Wesley. All rights reserved 4-15 ADT Sorted List – Pre and Postconditions © 2004 Pearson Addison-Wesley. All rights reserved 4-16 © 2004 Pearson Addison-Wesley. All rights reserved 4-17 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 reserved 4-18 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 reserved 4-19 Implementing ADTs Figure 4.8 ADT operations provide access to a data structure © 2004 Pearson Addison-Wesley. All rights reserved 4-20 Implementing ADTs Figure 4.9 Violating the wall of ADT operations © 2004 Pearson Addison-Wesley. All rights reserved 4-21 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 • ADT Sphere – Data – Operations • Interface // **************************************** // 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. Example (cont’d) public double circumference(); // Determines a sphere's circumference. // Precondition: None. // Postcondition: Returns the circumference. • Interface (cont’d) 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 Example (cont’d) • Implementation // ***************************************** // 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 (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 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 Example (cont’d) • Application // ******************************************************** // 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()); Example (cont’d) • Application (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(); } } 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 kth item will be stored in items[k-1] © 2004 Pearson Addison-Wesley. All rights reserved 4-31 An Array-Based Implementation of the ADT List Figure 4.11 An array-based implementation of the ADT list © 2004 Pearson Addison-Wesley. All rights reserved 4-32 An Array-Based Implementation of the ADT List • Insertion – Figure 4-12 • Deletion – Figure 4-13 • Code – Pages 209 – 213 3-33 4-12 © 20044-13 Pearson Addison-Wesley. All rights reserved 4-34