320 likes | 431 Views
CSE 1020: Exceptions and A multiclass application. Mark Shtern. Summary. Collection Framework List Map Set. Final Exam. Written Component UML Aggregation and Composition Collection Framework Inheritance (interfaces, abstract and concert classes) Exception and error handling
E N D
CSE 1020: Exceptions and A multiclass application Mark Shtern
Summary • Collection Framework • List • Map • Set
Final Exam • Written Component • UML • Aggregation and Composition • Collection Framework • Inheritance (interfaces, abstract and concert classes) • Exception and error handling • Lab component • Multiclass application
Sources of Errors The programmer The End-User The Runtime Environment
Exception Handling Logical Error Sources Yes Valid Operation? No Error Exception Caught? Handler Yes No Runtime Error
The Delegation Model • Handle or delegate policy • Detects an invalid operation in a method • Throws an exception • Searches the method for an exception handler • If the handler found then control is given to the it • If no, the search is transferred to the caller of the method • If no method can handle the throw exception, a runtime error occurs and the program terminated
Basic try-catch Construction try{ code fragment } catch (SomeType e) { exception handler }
Handling Multiple Exception try{ code fragment } catch (Type-1 e) { } catch (Type-3 e) { } catch (Type-3 e)
Other construction Finally Nested try-catch
Object Oriented Exception Handling • The substitutability principle • When a parent is expected, a child is accepted • Throwable methods • getMessage() • printStackTrace() • Explicit exception creation and throw ArithmeticExceptionae = new ArithmeticException() throw ae;
Building Robust Apps • Validation versus Exception • Defensive Programming • Exception-Based programming
Exercise 11.1 • Write a code fragment that performs the same function as the statement below without using the crash methodToolbox.crash(amount < 0, “A negative amount!”);
Exercise 11.2 • Locate the error and specify if it is compile-time, runtime, or logic try { // some code } catch (java.io.IOException e) { // some code } catch (java.io.FileNotFoundException e) { // some code }
Exercise 11.3 • Explain what is wrong with the following try { // some code } catch (Exception e) {}
Exercise 11.4 • Critique the following and compare to the previous exercisepublic static void main (String[] args) throws Exception
A Multiclass Application The abstract foods company UML see on page 448
Classes • AbstractFods • Inventory • Journal • Contact • Item • Fresh • Trx • Contact • Supplier • Catalog • Client
Exception Examples The program reads a string of two slash-delimited integers, extracts two integers, divides them, and outputs their quotient. Modify the program to handle input errors
Summary Exception throw vs throws Catch Finally Check vs Uncheck
I/O Stream • Keyboard input • Standard input • Keyboard source • Program data sink • Channel stream • InputStream
System class in out
InputStream InputStream keyboard = System.in; int input = keyboard.read();
Input Stream BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in));
File input • Open Stream BufferedReader filer = new BufferedReader (new InputStreamReader(new FileInputStream (fileName))); • Reading for (String line = filer.readLine(); line != null; line = filer.readline()) { //process } • Close Stream filer.close();
Screen Output Standard Output Output Stream Standard Error PrintStream
Serialization java.io.objectOutputStream FileOutputStream fos = new FileOutputStream (fileName); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(gc); oos.close();
Deserialization java.io.ObjectInputStream FileInputStream fis = FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(fis); Object obj = ois.readObject(); ois.close ();
Date Date Calendar Date/Format SimpleDateFormat
Exercise 10.1 • Determine the interface that should be used for each of the following collections • The names of all students registered in a course • The letter grades obtained in a course • The names of your contacts and the telephone number of each • The reserved words in Java
Exercise 12.2 • Write a program that • Creates an Item instance named Tuna with price $2.45 • Purchases 200 units for a total of $250 • Sells 50 units • Sells 25 units for a total of $30 • Purchases 100 units for a total of $175 • Outputs unit cost price
Exercise 12.3 • Write a program that • Creates a Fresh instance called “Pacific Salmon”, with any item number, a price of $20, and an expiry date three weeks from now • Creates a Fresh instance called “Atlantic Salmon”, with the same item number as above, a price of $25, and an expiry date 20 days from now • Outputs the return of the equals method invoked on one passing the other as argument • Outputs the return of the toString method for the two items