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

Similar documents
Data Abstraction: The Walls

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

Programming overview

Sri Vidya College of Engineering & Technology Question Bank

2.6 Error, exception and event handling

COMP1008 Exceptions. Runtime Error

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

About this exam review

Casting. References. References

Selected Java Topics

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

Lists. Chapter 12. Copyright 2012 by Pearson Education, Inc. All rights reserved

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

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

Arrays Classes & Methods, Inheritance

Fundamentals of Object Oriented Programming

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

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?

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

CS159. Nathan Sprague

CSPP : Introduction to 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

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

Array. Prepared By - Rifat Shahriyar

Modern Programming Languages. Lecture Java Programming Language. An Introduction

THE CONCEPT OF OBJECT

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

Programming II (CS300)

BBM 102 Introduction to Programming II Spring Exceptions

Java Exceptions Java, winter semester

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

Programming II (CS300)

Exceptions Handling Errors using Exceptions

Today. Book-keeping. Exceptions. Subscribe to sipb-iap-java-students. Collections. Play with problem set 1

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Java Errors and Exceptions. Because Murphy s Law never fails

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

16-Dec-10. Consider the following method:

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

Introduction to Programming Using Java (98-388)

Exception-Handling Overview

Special error return Constructors do not have a return value What if method uses the full range of the return type?

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Java Review: Objects

Object Oriented Programming Exception Handling

Exceptions - Example. Exceptions - Example

Principles of Software Construction: Objects, Design and Concurrency. Exceptions, scope, static data and methods, and Generics.

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

Tutorial 8 Date: 15/04/2014

20 Most Important Java Programming Interview Questions. Powered by

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Object-Oriented Programming

Std 12 Lesson-10 Exception Handling in Java ( 1

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

Object-Oriented Design. March 2005 Object Oriented Design 1

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Brief Summary of Java

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

Full file at Chapter 2 - Inheritance and Exception Handling

Java Primer 1: Types, Classes and Operators

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

CS159. Nathan Sprague

Java Strings Java, winter semester

ITI Introduction to Computing II

Recitation 3. 2D Arrays, Exceptions

In this lab we will practice creating, throwing and handling exceptions.

Arrays and Linked Lists

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

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Exceptions in Java

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

What can go wrong in a Java program while running?

National University. Faculty of Computer Since and Technology Object Oriented Programming

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

Class, Variable, Constructor, Object, Method Questions

Here is a hierarchy of classes to deal with Input and Output streams.

Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

STRUCTURING OF PROGRAM

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

Lecture 19 Programming Exceptions CSE11 Fall 2013


ASSERTIONS AND LOGGING

ITI Introduction to Computing II

CS 61B Discussion 5: Inheritance II Fall 2014

Exceptions and Libraries

CMSC 202. Exceptions

Defensive Programming

Transcription:

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-114 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 The variable s is a reference to the object cat

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

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

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

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 index because the index is out of bounds 6 2 0 1 2 3 4 5 value 3 7 6 8 1 11 7 error!

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

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

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

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