Data Structures and Java CS 105. 12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:

Slides:



Advertisements
Similar presentations
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Advertisements

CS102--Object Oriented Programming
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
COMP 121 Week 5: Exceptions and Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Class Hierarchy (Inheritance)
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions Exception. Exceptions Overview Exceptional Situations Syntax of Try, Catch, Throw Defining and Using Your Own Exceptions.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Files and Streams CS 21a Chapter 11 of Horstmann.
© 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.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
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 Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Abstract classes and Interfaces. Abstract classes.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
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
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
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.
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:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Java Programming Review (Part II) Enterprise Systems Programming.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
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.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Interfaces.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Fundamental Error Handling
Classes CS 21a: Introduction to Computing I
CMSC 202 Exceptions.
Presentation transcript:

Data Structures and Java CS 105

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 2 Review of Java Topics The following Java features play an important role when implementing data structures in Java Interfaces Exceptions The Java class hierarchy and the Object class

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 3 Interfaces An interface indicates the method signatures for the operations of a data structure An implementation of the data structure is a Java class that implements this interface to enforce the definition of all methods There can be multiple implementations of the same interface/data structure

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 4 Example: Dictionary Dictionary SimpleDictionaryBetterDictionary

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 5 public interface Dictionary { public void addWord( String word, String definition ); public void getDefinition( String word ); } public class SimpleDictionary implements Dictionary { // define addWord and getDefinition } public class BetterDictionary implements Dictionary { // another implementation // define addWord and getDefinition }

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 6 Exceptions Some operations of data structures may be invalid in certain situations One option: handle the error within that method by printing an error message Can be annoying since the user of the method may get the message interspersed with other output Better alternative: throw exceptions so that the user of the method can decide how to deal with the error

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 7 Exceptions in Java Exceptions are handled using a try-catch statement Exceptions are thrown from the method that could cause the exception What needs to be done Define a class that extends Exception (the class may be empty) In the method declaration, include a throws clause In the method body, include a throw statement where the exception occurs

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 8 Example public class SimpleDictionary implements Dictionary { //… public void addWord( String word, String definition ) throws DuplicateWordException { if ( getDefinition( word ) != null ) throw new DuplicateWordException(); // code to add dictionary entry here… } // … } public class DuplicateWordException extends Exception { // this class could be empty }

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 9 Example Dictionary d = new SimpleDictionary(); try { d.addWord( “bat”, “mammal with wings” ); d.addWord( “cat”, “animal with whiskers” ); d.addWord( “bat”, “equipment used in baseball” ); d.addWord( “elephant”, “a large mammal” ); } catch( DuplicateWordException e ) { System.out.println( “Duplicate Word Error” ); } An exception will be thrown on this call

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 10 More on Exceptions Different kinds of exceptions can be handled using a try-catch chain Can have a more elaborate exception class by defining exception/error details inside the class; for example: error message additional data about the error (in the example, the word that causes the duplicate to occur can be stored in the DuplicateWordException class)

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 11 RuntimeException Make the exception class extend RuntimeException instead of Exception whenever you do not want to require that the exception be caught The user of the method may or may not use a try-catch statement (try-catch is required for Exceptions) If not within a try-catch, the program aborts

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 12 Interfaces and Exceptions In general, when a class implements an interface, the throws clause should be present in both the interface and the class that implements it However, an implementing class can throw additional exceptions as long as they are runtime exceptions

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 13 Inheritance hierarchy The extends keyword/feature in Java creates an inheritance hierarchy If a class does not extend another class, it implicitly extends Object, a built-in class in Java This means all classes are subclasses of Object Variables of type Object can refer to an instance of any class

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 14 The Object class and data structures When establishing interfaces for data structures, it might be better to use the Object class instead of particular types Example for the Queue interface: public void enqueue( Object o ); public Object dequeue(); instead of public void enqueue( String s ); public String dequeue(); Will need to cast when retrieving an object from the data structure: String s = ( String ) q.dequeue(); Supports Strings and other types of objects

12/14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: Abstract Class Slide 15 Summary Interfaces allow us to standardize method signatures and to have multiple implementations in a uniform manner Exceptions allow us to elegantly handle errors/unexpected situations The Object class allows our data structures to contain instances of any type/class