Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

Similar documents
Data Abstraction: The Walls

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Important when developing large programs. easier to write, understand, modify, and debug. each module understood individually

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object-Oriented Programming

Overview of the lecture. Some key issues in programming Java programming. CMPT Data and Program Abstraction. Some of the key programming issues

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Programming II (CS300)

Object-Oriented Programming

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CMSC 132: Object-Oriented Programming II

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Programming overview

More on Objects in JAVA TM

PROGRAMMING LANGUAGE 2

Chapter 5 Object-Oriented Programming

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Data Structures. 02 Exception Handling

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Chapter 11. Abstract Data Types and Encapsulation Concepts

Programming II (CS300)

Designing Robust Classes

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 10 Classes Continued. Fundamentals of Java

Comp 249 Programming Methodology Chapter 9 Exception Handling

Practice for Chapter 11

JAVA MOCK TEST JAVA MOCK TEST II

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Programming for the Web with PHP

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

What can go wrong in a Java program while running?

Selected Java Topics

CMSC 132: Object-Oriented Programming II

Anatomy of a Class Encapsulation Anatomy of a Method

CS 11 java track: lecture 3

Fundamentals of Object Oriented Programming

Tutorial 8 Date: 15/04/2014

CS112 Lecture: Exceptions and Assertions

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

CMSC 202. Exceptions

VALLIAMMAI ENGINEERING COLLEGE

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

WA1278 Introduction to Java Using Eclipse

Compaq Interview Questions And Answers

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

+ Abstract Data Types

Chapter 7. Inheritance


CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Object-Oriented Design. March 2005 Object Oriented Design 1

Casting. References. References

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Programming II (CS300)

Java OOP (SE Tutorials: Learning the Java Language Trail : Object-Oriented Programming Concepts Lesson )

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Adding Contracts to C#

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Inheritance -- Introduction

Get Unique study materials from

Sri Vidya College of Engineering & Technology Question Bank

Abstract Data Types and Encapsulation Concepts

CS313D: ADVANCED PROGRAMMING LANGUAGE

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

7. C++ Class and Object

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance and Polymorphism

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Programming II (CS300)

Chapter 4: Writing Classes

Lecture 7: Data Abstractions

CIS Intro to Programming in C#

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Exception Handling in C++

Types, Values and Variables (Chapter 4, JLS)

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Chapter 6 Introduction to Defining Classes

Data Abstraction. Hwansoo Han

DEPARTMENT OF INFORMATION TECHNOLOGY

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Chapter 1: Object-Oriented Programming Using C++

Data Structures (list, dictionary, tuples, sets, strings)

Programming Exercise 14: Inheritance and Polymorphism

Exception Handling. Handling bad user input. Dr. Siobhán Drohan Maireád Meagher. Produced by:

Object-Oriented Programming (OOP) Fundamental Principles of OOP

JAVA: A Primer. By: Amrita Rajagopal

Java Errors and Exceptions. Because Murphy s Law never fails

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

Binary search. int index = Collections.binarySearch(list, element);

COMP1008 Exceptions. Runtime Error

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Implementing Subprograms

Transcription:

Chapter 4!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1 2015-09-29 11:44:25 1/45 Chapter-04.pdf (#4)

bubblesort(int[] a) { int last = a.length - 1; while (last > 0) { int i = 0; while (i < last) { if (a[i] > a[i+1]) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } i++; } last--; } } 2015-09-29 11:44:25 2/45 Chapter-04.pdf (2/45)

2015-09-29 11:44:25 3/45 Chapter-04.pdf (3/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-2 2015-09-29 11:44:25 4/45 Chapter-04.pdf (4/45)

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!! 2011 Pearson Addison-Wesley. All rights reserved 4-3 2015-09-29 11:44:25 5/45 Chapter-04.pdf (5/45)

Abstract Data Types Figure 4-1 Isolated tasks: the implementation of task T does not affect task Q 2011 Pearson Addison-Wesley. All rights reserved 4-4 2015-09-29 11:44:25 6/45 Chapter-04.pdf (6/45)

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 2011 Pearson Addison-Wesley. All rights reserved 4-5 2015-09-29 11:44:25 7/45 Chapter-04.pdf (7/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-6 2015-09-29 11:44:25 8/45 Chapter-04.pdf (8/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-7 2015-09-29 11:44:25 9/45 Chapter-04.pdf (9/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-8 2015-09-29 11:44:26 10/45 Chapter-04.pdf (10/45)

Abstract Data Types Figure 4-4 A wall of ADT operations isolates a data structure from the program that uses it 2011 Pearson Addison-Wesley. All rights reserved 4-9 2015-09-29 11:44:26 11/45 Chapter-04.pdf (11/45)

Specifying ADTs Figure 4-5 list A grocery 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! 2011 Pearson Addison-Wesley. All rights reserved 4-10 2015-09-29 11:44:26 12/45 Chapter-04.pdf (12/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-11 2015-09-29 11:44:26 13/45 Chapter-04.pdf (13/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-12 2015-09-29 11:44:26 14/45 Chapter-04.pdf (14/45)

The ADT List Figure 4-7 The wall between displaylist and the implementation of the ADT list 2011 Pearson Addison-Wesley. All rights reserved 4-13 2015-09-29 11:44:26 15/45 Chapter-04.pdf (15/45)

The ADT Sorted List The ADT sorted list! Maintains items in sorted order! Inserts and deletes items by their values, not their positions! 2011 Pearson Addison-Wesley. All rights reserved 4-14 2015-09-29 11:44:26 16/45 Chapter-04.pdf (16/45)

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?! 2011 Pearson Addison-Wesley. All rights reserved 4-15 2015-09-29 11:44:26 17/45 Chapter-04.pdf (17/45)

Axioms (Optional) For complex abstract data types, the behavior of the operations must be specified using axioms! Axiom: A mathematical rule! 2011 Pearson Addison-Wesley. All rights reserved 4-16 2015-09-29 11:44:26 18/45 Chapter-04.pdf (18/45)

Axioms (Optional) Axioms for the ADT List! (alist.createlist()).size() = 0! (alist.add(i, x)).size() = alist.size() + 1! (alist.remove(i)).size() = alist.size() 1! (alist.createlist()).isempty() = true! (alist.add(i, item)).isempty() = false! (alist.createlist()).remove(i) = error! (alist.add(i, x)).remove(i) = alist! (alist.createlist()).get(i) = error! (alist.add(i, x)).get(i) = x! alist.get(i) = (alist.add(i, x).get(i+1)! alist.get(i+1) = (alist.remove(i)).get(i)! 2011 Pearson Addison-Wesley. All rights reserved 4-17 2015-09-29 11:44:26 19/45 Chapter-04.pdf (19/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-18 2015-09-29 11:44:26 20/45 Chapter-04.pdf (20/45)

Implementing ADTs Figure 4-8 ADT operations provide access to a data structure 2011 Pearson Addison-Wesley. All rights reserved 4-19 2015-09-29 11:44:26 21/45 Chapter-04.pdf (21/45)

Implementing ADTs Figure 4-9 Violating the wall of ADT operations 2011 Pearson Addison-Wesley. All rights reserved 4-20 2015-09-29 11:44:26 22/45 Chapter-04.pdf (22/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-21 2015-09-29 11:44:27 23/45 Chapter-04.pdf (23/45)

Java Classes Revisited Figure 4-10 An object s data and methods are encapsulated 2011 Pearson Addison-Wesley. All rights reserved 4-22 2015-09-29 11:44:27 24/45 Chapter-04.pdf (24/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-23 2015-09-29 11:44:27 25/45 Chapter-04.pdf (25/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-24 2015-09-29 11:44:27 26/45 Chapter-04.pdf (26/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-25 2015-09-29 11:44:27 27/45 Chapter-04.pdf (27/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-26 2015-09-29 11:44:27 28/45 Chapter-04.pdf (28/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-27 2015-09-29 11:44:27 29/45 Chapter-04.pdf (29/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-28 2015-09-29 11:44:27 30/45 Chapter-04.pdf (30/45)

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 2011 Pearson Addison-Wesley. All rights reserved 4-29 2015-09-29 11:44:27 31/45 Chapter-04.pdf (31/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-30 2015-09-29 11:44:27 32/45 Chapter-04.pdf (32/45)

Java Exceptions Exception! A mechanism for handling an error during execution! A method indicates that an error has occurred by throwing an exception! 2011 Pearson Addison-Wesley. All rights reserved 4-31 2015-09-29 11:44:27 33/45 Chapter-04.pdf (33/45)

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 2011 Pearson Addison-Wesley. All rights reserved 4-32 2015-09-29 11:44:27 34/45 Chapter-04.pdf (34/45)

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 2011 Pearson Addison-Wesley. All rights reserved 4-33 2015-09-29 11:44:27 35/45 Chapter-04.pdf (35/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-34 2015-09-29 11:44:27 36/45 Chapter-04.pdf (36/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-35 2015-09-29 11:44:28 37/45 Chapter-04.pdf (37/45)

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! 2011 Pearson Addison-Wesley. All rights reserved 4-36 2015-09-29 11:44:28 38/45 Chapter-04.pdf (38/45)

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] 2011 Pearson Addison-Wesley. All rights reserved 4-37 2015-09-29 11:44:28 39/45 Chapter-04.pdf (39/45)

An Array-Based Implementation of the ADT List Figure 4-11 An array-based implementation of the ADT list 2011 Pearson Addison-Wesley. All rights reserved 4-38 2015-09-29 11:44:28 40/45 Chapter-04.pdf (40/45)

2015-09-29 11:44:28 41/45 Chapter-04.pdf (41/45)

2015-09-29 11:44:28 42/45 Chapter-04.pdf (42/45)

2015-09-29 11:44:28 43/45 Chapter-04.pdf (43/45)

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! The formal mathematical study of ADTs uses systems of axioms to specify the behavior of ADT operations! Only after you have fully defined an ADT should you think about how to implement it! 2011 Pearson Addison-Wesley. All rights reserved 4-39 2015-09-29 11:44:28 44/45 Chapter-04.pdf (44/45)

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 class contains at least one constructor, which is an initialization method! Typically, you should make the data fields of a class private and provide public methods to access some or all of the data fields! 2011 Pearson Addison-Wesley. All rights reserved 4-40 2015-09-29 11:44:28 45/45 Chapter-04.pdf (45/45)