© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.

Slides:



Advertisements
Similar presentations
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Advertisements

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.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
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.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
Chapter 1 Principles of Programming and Software Engineering.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
© 2006 Pearson Addison-Wesley. All rights reserved 4-1 Chapter 4 Data Abstraction: The Walls.
ASP.NET Programming with C# and SQL Server First Edition
Chapter 10 Classes Continued
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.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Programming Languages and Paradigms Object-Oriented Programming.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Java Programming: Guided Learning with Early Objects
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Object-Oriented Programming Chapter Chapter
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
ISBN Object-Oriented Programming Chapter Chapter
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
COP3530 Data Structures300 Data Abstraction: The Walls Abstract Data Types (ADT) Specifying ADTs –The ADT List –The ADT Sorted List –Designing an ADT Implementation.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 2 Principles of Programming and Software Engineering.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Principles of Programming & Software Engineering
Data Abstraction: The Walls
Data Abstraction: The Walls
Principles of Programming and Software Engineering
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Data Abstraction: The Walls
Data Abstraction: The Walls
Data Abstraction: The Walls
CMSC 202 Exceptions.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls

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

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

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

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

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

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

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

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

© 2006 Pearson Addison-Wesley. All rights reserved4-10 Specifying ADTs In a list –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

© 2006 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 –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 Items are referenced by their position within the list

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

© 2006 Pearson Addison-Wesley. All rights reserved4-13 The ADT List Figure 4-7 The wall between displayList and the implementation of the ADT list

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

© 2006 Pearson Addison-Wesley. All rights reserved4-15 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?

© 2006 Pearson Addison-Wesley. All rights reserved4-16 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

© 2006 Pearson Addison-Wesley. All rights reserved4-17 Implementing ADTs Figure 4-8 ADT operations provide access to a data structure

© 2006 Pearson Addison-Wesley. All rights reserved4-18 Implementing ADTs Figure 4-9 Violating the wall of ADT operations

© 2006 Pearson Addison-Wesley. All rights reserved4-19 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]

© 2006 Pearson Addison-Wesley. All rights reserved4-20 An Array-Based Implementation of the ADT List Figure 4-11 An array-based implementation of the ADT list More Discussion

© 2006 Pearson Addison-Wesley. All rights reserved4-21 Java Classes Revisited Object-oriented programming (OOP) views a program as a collection of objects Encapsulation –A principle of OOP –Can be used to enforce the walls of an ADT –Combines an ADT’s data with its method to form an object –Hides the implementation details of the ADT from the programmer who uses it

© 2006 Pearson Addison-Wesley. All rights reserved4-22 Java Classes Revisited Figure 4-10 An object’s data and methods are encapsulated

© 2006 Pearson Addison-Wesley. All rights reserved4-23 Java Classes Revisited A Java class –A new data type whose instances are objects –Class members Data fields –Should almost always be private Methods –All members in a class are private, unless the programmer designates them as public

© 2006 Pearson Addison-Wesley. All rights reserved4-24 Java Classes Revisited Inheritance –Base class or superclass –Derived class or subclass Inherits the contents of the superclass Includes an extends clause that indicates the superclass super keyword –Used in a constructor of a subclass to call the constructor of the superclass

© 2006 Pearson Addison-Wesley. All rights reserved4-25 Java Interfaces An interface –Specifies methods and constants, but supplies no implementation details –Can be used to specify some desired common behavior that may be useful over many different types of objects –The Java API has many predefined interfaces Example: java.util.Collection

© 2006 Pearson Addison-Wesley. All rights reserved4-26 Java Interfaces A class that implements an interface must –Include an implements clause –Provide implementations of the methods of the interface To define an interface –Use the keyword interface instead of class in the header –Provide only method specifications and constants in the interface definition

© 2006 Pearson Addison-Wesley. All rights reserved4-27 Java Interfaces Interfaces can be useful for describing ADTs –an interface only specifies the operations –the “how”s get answered in the implemented class This will be the approach we take in this course

© 2006 Pearson Addison-Wesley. All rights reserved4-28 Java Interfaces Example public interface GenericADT { public add(…); public remove(…); public find(…); public display(…); }

© 2006 Pearson Addison-Wesley. All rights reserved4-29 Summary Data abstraction: a technique for controlling the interaction between a program and its data structures An ADT: the specifications of a set of data management operations and the data values upon which they operate Only after you have fully defined an ADT should you think about how to implement it

© 2006 Pearson Addison-Wesley. All rights reserved4-30 Summary A client should only be able to access the data structure by using the ADT operations An object encapsulates both data and operations on that data –In Java, objects are instances of a class, which is a programmer-defined data type A Java interface is a useful tool for specifying the operations of an ADT

© 2006 Pearson Addison-Wesley. All rights reserved4-31 Java Classes Revisited A Java class (Continued) –Constructor A method that creates and initializes new instances of a class Has the same name as the class Has no return type –Java’s garbage collection mechanism Destroys objects that a program no longer references

© 2006 Pearson Addison-Wesley. All rights reserved4-32 Java Classes Revisited Constructors –Allocate memory for an object and can initialize the object’s data –A class can have more than one constructor –Default constructor Has no parameters Typically, initializes data fields to values the class implementation chooses

© 2006 Pearson Addison-Wesley. All rights reserved4-33 Java Classes Revisited Constructors (Continued) –Compiler-generated default constructor Generated by the compiler if no constructor is included in a class Client of a class –A program or module that uses the class

© 2006 Pearson Addison-Wesley. All rights reserved4-34 Java Classes Revisited Object Equality –equals method of the Object class Default implementation –Compares two objects and returns true if they are actually the same object Customized implementation for a class –Can be used to check the values contained in two objects for equality

© 2006 Pearson Addison-Wesley. All rights reserved4-35 Java Exceptions Exception –A mechanism for handling an error during execution –A method indicates that an error has occurred by throwing an exception

© 2006 Pearson Addison-Wesley. All rights reserved4-36 Java Exceptions Catching exceptions –try block A statement that might throw an exception is placed within a try block Syntax try { statement(s); } // end try

© 2006 Pearson Addison-Wesley. All rights reserved4-37 Java Exceptions Catching exceptions (Continued) –catch block Used to catch an exception and deal with the error condition Syntax catch (exceptionClass identifier) { statement(s); } // end catch

© 2006 Pearson Addison-Wesley. All rights reserved4-38 Java Exceptions Types of exceptions –Checked exceptions Instances of classes that are subclasses of the java.lang.Exception class Must be handled locally or explicitly thrown from the method Used in situations where the method has encountered a serious problem

© 2006 Pearson Addison-Wesley. All rights reserved4-39 Java Exceptions Types of exceptions (Continued) –Runtime exceptions Used in situations where the error is not considered as serious Can often be prevented by fail-safe programming Instances of classes that are subclasses of the RuntimeException class Are not required to be caught locally or explicitly thrown again by the method

© 2006 Pearson Addison-Wesley. All rights reserved4-40 Java Exceptions Throwing exceptions –A throw statement is used to throw an exception throw new exceptionClass (stringArgument); Defining a new exception class –A programmer can define a new exception class