cse 1020: exceptions and a multiclass application

32
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1

Upload: geraldine-green

Post on 03-Jan-2016

27 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE 1020: Exceptions and A multiclass application

CSE 1020: Exceptions and A multiclass application

Mark Shtern

1-1

Page 2: CSE 1020: Exceptions and A multiclass application

Summary

• Collection Framework– List– Map– Set

1-2

Page 3: CSE 1020: Exceptions and A multiclass application

Final Exam

• Written Component– UML– Aggregation and Composition– Collection Framework– Inheritance (interfaces, abstract and concert

classes)– Exception and error handling

• Lab component– Multiclass application

1-3

Page 4: CSE 1020: Exceptions and A multiclass application

Sources of Errors

• The programmer• The End-User• The Runtime Environment

1-4

Page 5: CSE 1020: Exceptions and A multiclass application

Exception Handling

1-5

Sources

Error Valid Operation?

Logical Error

Exception

Caught?Handler

Runtime Error

Yes

Yes

No

No

Page 6: CSE 1020: Exceptions and A multiclass application

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

1-6

Page 7: CSE 1020: Exceptions and A multiclass application

Basic try-catch Construction

try{ code fragment} catch (SomeType e) {

exception handler}

1-7

Page 8: CSE 1020: Exceptions and A multiclass application

Handling Multiple Exception

try{ code fragment} catch (Type-1 e){} catch (Type-3 e){} catch (Type-3 e)

1-8

Page 9: CSE 1020: Exceptions and A multiclass application

Other construction

• Finally• Nested try-catch

1-9

Page 10: CSE 1020: Exceptions and A multiclass application

The Throwable Hierarchy

1-10

Page 11: CSE 1020: Exceptions and A multiclass application

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 ArithmeticException ae = new

ArithmeticException() throw ae;

1-11

Page 12: CSE 1020: Exceptions and A multiclass application

Building Robust Apps

• Validation versus Exception– Defensive Programming– Exception-Based programming

1-12

Page 13: CSE 1020: Exceptions and A multiclass application

Exercise 11.1

• Write a code fragment that performs the same function as the statement below without using the crash method

Toolbox.crash(amount < 0, “A negative amount!”);

Page 14: CSE 1020: Exceptions and A multiclass application

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 }

Page 15: CSE 1020: Exceptions and A multiclass application

Exercise 11.3

• Explain what is wrong with the following

try{ // some code }catch (Exception e){}

Page 16: CSE 1020: Exceptions and A multiclass application

Exercise 11.4

• Critique the following and compare to the previous exercise

public static void main (String[] args) throws Exception

Page 17: CSE 1020: Exceptions and A multiclass application

A Multiclass Application

• The abstract foods company• UML see on page 448

1-17

Page 18: CSE 1020: Exceptions and A multiclass application

Classes• AbstractFods– Inventory– Journal– Contact

• Item• Fresh• Trx• Contact• Supplier– Catalog

• Client

1-18

Page 19: CSE 1020: Exceptions and A multiclass application

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

1-19

Page 20: CSE 1020: Exceptions and A multiclass application

Summary

• Exception• throw vs throws• Catch• Finally• Check vs Uncheck

1-20

Page 21: CSE 1020: Exceptions and A multiclass application

I/O Stream

• Keyboard input– Standard input– Keyboard source– Program data sink– Channel stream

• InputStream

1-21

Page 22: CSE 1020: Exceptions and A multiclass application

System class

• in• out

1-22

Page 23: CSE 1020: Exceptions and A multiclass application

InputStream

InputStream keyboard = System.in;int input = keyboard.read();

1-23

Page 24: CSE 1020: Exceptions and A multiclass application

Input Stream

BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in));

1-24

Page 25: CSE 1020: Exceptions and A multiclass application

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();

1-25

Page 26: CSE 1020: Exceptions and A multiclass application

Screen Output

• Standard Output• Output Stream• Standard Error• PrintStream

1-26

Page 27: CSE 1020: Exceptions and A multiclass application

Serialization

java.io.objectOutputStreamFileOutputStream fos = new FileOutputStream

(fileName);ObjectOutputStream oos = new

ObjectOutputStream(fos);oos.writeObject(gc);oos.close();

1-27

Page 28: CSE 1020: Exceptions and A multiclass application

Deserialization

java.io.ObjectInputStreamFileInputStream fis = FileInputStream(filename);ObjectInputStream ois = new

ObjectInputStream(fis);Object obj = ois.readObject();ois.close ();

1-28

Page 29: CSE 1020: Exceptions and A multiclass application

Date

• Date• Calendar• Date/Format• SimpleDateFormat

1-29

Page 30: CSE 1020: Exceptions and A multiclass application

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

Page 31: CSE 1020: Exceptions and A multiclass application

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

Page 32: CSE 1020: Exceptions and A multiclass application

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