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.

Slides:



Advertisements
Similar presentations
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Yoshi
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
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.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
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.
1 Why do we need exceptions? In C, return variables must be used to indicate errors: if((fd = fopen(path,...)) == -1){ if(errno==a){...} else if(errno==b){...}
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
© 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 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java and C++, The Difference An introduction Unit - 00.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
Slides Credit Umair Javed LUMS Web Application Development.
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:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions and Assertions Chapter 15 – CSCI 1302.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
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
Chapter 10 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
Java Programming Language
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Creating and Modifying Text part 2
ATS Application Programming: Java Programming
Java Programming Language
Exception Handling and Reading / Writing Files
Data Abstraction: The Walls
Java Exceptions Dan Fleck CS211.
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.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

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? Examples: polynomial, appointment book ` ADT can suggest other ADTs

Appendix 1. Java Exceptions (review) Exception A mechanism for handling an error during execution A method indicates that an error has occurred by throwing an exception

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

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

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

Checked exceptions public class TestExceptionExample { public static void getInput(String fileName) { FileInputStream fis; fis = new FileInputStream(fileName); // file processing code appears here } // end getInput public static void main(String[] args) { getInput("test.dat"); } // end main } // end TestExceptionExample

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

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

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

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

An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object items[]; // an array of list items private int numItems; // number of items in list

Appendix 2. Arrays in Java (review) Arrays are sequences of identically typed values Values are stored at specific numbered positions in the array The first value is stored at index 0, the second at index 1, the ith at index i-1, and so on The last item is stored at position n-1, assuming that n values are stored in the array Values are stored sequentially in main memory

Arrays in Java To declare an array follow the type with (empty) [] s int[] grade; //or int grade[]; //both declare an int array In Java arrays are objects!

Objects in Java String s = new String("cat"); s cat

Objects in Java (review) String s = new String("cat"); s cat The variable s is a reference to the object

Objects in Java String s = new String("cat"); S = null; s cat Makes s not refer to the object any more

Objects in Java String s = new String("cat"); S = null; s cat Makes s not refer to the object any more The object gets deleted by Java’s automated garbage collection

Objects in Java String s = new String("cat"); String t = s; s cat This makes another reference to the object --- but no new object is created!! t

Arrays in Java To declare an array follow the type with (empty) [] s int[] grade; //or int grade[]; //both declare an int array In Java arrays are objects so must be created with the new keyword To create an array of ten integers: int[] grade = new int[10]; Note that the array size has to be specified, although it can be specified with a variable at run-time

Arrays in Java When the array is created memory is reserved for its contents Initialization lists can be used to specify the initial values of an array, in which case the new operator is not used int[] grade = {87, 93, 35}; //array of 3 ints To find the length of an array use its.length variable int numGrades = grade.length; //note: not.length()!!

Array Indexing int[] arr = {3,7,6,8,1,7,2}; creates a new integer array with seven elements The elements are assigned values as given in the initialization list Individual elements can be accessed by referring to the array name and the appropriate index int x = arr[3]; would assign the value of the fourth array element (8) to x arr[5] = 11; would change the sixth element of the array from 7 to 11 arr[7] = 3; would result in an error because the index is out of bounds indexvalue error!

Arrays and Main Memory int[] grade; Declares an array variable grade In Java arrays are objects, so this is a reference variable “null pointer” main memory is depicted below

Arrays and Main Memory int[] grade; grade = new int[4]; Creates a new array of size 4 grade main memory is depicted below Stores 4 int s consecutively. The int s are initialized to 0 points to the newly created array object

Arrays and Main Memory int[] grade; grade = new int[4]; grade[2] = 23; Assigns 23 to the third item in the array grade main memory is depicted below But how does the system “know” where grade[2] is? 23

Offset Calculations Given something like grade[2] = 23; how do we find a particular element in the array? We know the address of the first element in the array Because we know the type of the values stored in the array, we know the size of each element in the array 4 bytes in the case of an int We know which element we want to access We can therefore calculate the address of the desired element as being: address of first element + index * size of stored type

Passing Arrays to Methods Array variables are reference variables When an array variable is passed as an argument to a method the method is being given the address of an array object Not a new copy of the array object Any changes made to the array in the method are therefore made to the original (and only) array object If this is not desired, a copy of the array should be made within the method

Arrays are Static Data Structures The size of an array must be specified when it is created with new and cannot be changed If the array is full new items can’t be added to it There are, time consuming, ways around this To avoid this problem make arrays much larger than they are needed However this wastes space