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.

Slides:



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

Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
COMP 121 Week 5: Exceptions and Exception Handling.
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.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
CMPT 225 Abstract Data Types.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
© 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
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
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.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Object Oriented Programming
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
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.
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.
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
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.
Data Structures and Java CS /14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
(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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Object-Oriented Programming Using C++ CLASS 4 Honors.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
An Array-Based Implementation of the ADT List
Data Abstraction: The Walls
Java Programming Language
Abstract Data Types (ADT)
Data Abstraction: The Walls
Introduction to Exceptions in Java
Introduction to Exceptions in Java
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
Data Abstraction: The Walls
Abstract Data Types (ADT)
Data Abstraction: The Walls
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Data Abstraction: The Walls
CMSC 202 Exceptions.
Presentation transcript:

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

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 A data structure is a construct that can be defined in a programming language to store a collection of data

Abstract Data Types-Walls Figure 4-1 Isolated tasks: the implementation of task T does not affect task Q

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

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

The ADT List ADT List operations  Create an empty list aList.createList();  Determine whether a list is empty aList.isEmpty();  Determine the number of items in a list aList.size();  Add an item at a given position in the list aList.add(index, item);  Remove the item at a given position in the list aList.remove(index);  Remove all the items from the list aList.removeAll();  Retrieve (get) the item at a given position in the list aList.get(index); Items are referenced by their position within the list

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 If you request these operations be performed, this is what will happen. ADT operations can be used in an application without the knowledge of how the operations will be implemented

Using an ADT. displayList(in aList:List){ for(index=1 through aList.size()){ dataItem=aList.get(index); Print dataItem; } The following methods manipulate the data in the list without knowing the implementation details of the List. replace(in aList:List, in i:int, in newItem:ListItemType){ if(i>=1 and i<=aList.size()){ aList.remove(i); aList.insert(i, newItem); }

The ADT List Figure 4-7 The wall between displayList and the implementation of the ADT list

The ADT Sorted List The ADT sorted list  Maintains items in sorted order  Inserts and deletes items by their values, not their positions

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

Implementing ADTs Figure 4-8 ADT operations provide access to a data structure

Implementing ADTs Figure 4-9 Violating the wall of ADT operations

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]

An Array-Based Implementation of the ADT List Figure 4-11 An array-based implementation of the ADT list

Create an Interface for the List ADT public interface ListInterface{ public boolean isEmpty(); public int size(); public void add(int index, Object item) throws ListException; public Object get(int index) throws ListException; public void remove(int index) throws ListException; public void removeAll(); }

Array-Based implementation. public class ListArrayBased implements ListInterface{ private static final int MAX_LIST = 50; private Object items[]; private int numItems; public ListArrayBased(){ //act as the createList() items = new Objects[MAX_LIST]; numItems = 0; } public boolean isEmpty(){ return (numItems == 0); } public int size() {return numItems;} public void add(int index, Object item) throws ListException {[…]} public Object get(int index) throws ListException {[…]} public void remove(int index) throws ListException{[…]}; public void removeAll(){[…]} }

Using the List ADT Static public void main(){ […] ListArrayBased groceryList = new ListArrayBased(); groceryList.add(1, “Milk”); […] String item=groceryList.get(1); //violates the terms of abstraction. String item=groceryList.items[0]; //illegal statement. […] }

Java Exceptions (review) Exception  A mechanism for handling an error during execution  A method indicates that an error has occurred by throwing an exception  When a method throws an exception the control of the program returns to the point at which the method is called.  At this point you must catch the exception an deal with it.

Java Exceptions –catching exception Java provides try-catch blocks to handle exceptions Syntax: try{ statements; } catch(exceptionClass id){ statements; } try block  A statement that might throw an exception is placed within a try block

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

When a statement throws an exception it returns an object that must be caught by the catch block. When a statement in a try block throws an exception the remainder of the try block is abandoned, and the control is passed to the catch block that corresponds to the type of exception thrown. try { int result = 99/0; //this statement throws an exception //other statements may appear here; }catch (ArithmetichException e){ System.out.println(“ ArithmetichException caught”); }

Some exceptions must be handled. public static void getInput(String filename){ FileInputStream fis; fis = new FileInputStream(filename); […] } The new FileInputStream statement throws an exception if file is not found. In the above example this exception is not handled and therefore it won’t get compiled. public static void getInput(String filename){ FileInputStream fis; try{ fis = new FileInputStream(filename); }catch(FileNotFoundException e){ System.out.println(“The file”+filename+”Not found”); } […] }

Java Exceptions Types of exceptions  Checked exceptions Instances of classes that are subclasses of the java.lang.Exception class Must be handled locally Used in situations where the method has encountered a serious problem

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

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

class MyException extends Exception { public MyException(String s) { super(s); } // end constructor } // end MyException public void myMethod() throws MyException{ //some code here. if( some condition){ throw new MyException(“MyException: reason”); } public void yourMethod(){ try{ myMethod(); } catch (MyException e){ //code to handle the exception }

Finally block The finally block  Executed whether or not an exception is thrown  Can be used even if no catch block is used  Syntax finally { statement(s); }