1 / 32

CSE 1020: Exceptions and A multiclass application

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

Download Presentation

CSE 1020: Exceptions and A multiclass application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1020: Exceptions and A multiclass application Mark Shtern

  2. Summary • Collection Framework • List • Map • Set

  3. Final Exam • Written Component • UML • Aggregation and Composition • Collection Framework • Inheritance (interfaces, abstract and concert classes) • Exception and error handling • Lab component • Multiclass application

  4. Sources of Errors The programmer The End-User The Runtime Environment

  5. Exception Handling Logical Error Sources Yes Valid Operation? No Error Exception Caught? Handler Yes No Runtime Error

  6. 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

  7. Basic try-catch Construction try{ code fragment } catch (SomeType e) { exception handler }

  8. Handling Multiple Exception try{ code fragment } catch (Type-1 e) { } catch (Type-3 e) { } catch (Type-3 e)

  9. Other construction Finally Nested try-catch

  10. The Throwable Hierarchy

  11. 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;

  12. Building Robust Apps • Validation versus Exception • Defensive Programming • Exception-Based programming

  13. 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!”);

  14. 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 }

  15. Exercise 11.3 • Explain what is wrong with the following try { // some code } catch (Exception e) {}

  16. Exercise 11.4 • Critique the following and compare to the previous exercisepublic static void main (String[] args) throws Exception

  17. A Multiclass Application The abstract foods company UML see on page 448

  18. Classes • AbstractFods • Inventory • Journal • Contact • Item • Fresh • Trx • Contact • Supplier • Catalog • Client

  19. 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

  20. Summary Exception throw vs throws Catch Finally Check vs Uncheck

  21. I/O Stream • Keyboard input • Standard input • Keyboard  source • Program  data sink • Channel  stream • InputStream

  22. System class in out

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

  24. Input Stream BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in));

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

  26. Screen Output Standard Output Output Stream Standard Error PrintStream

  27. Serialization java.io.objectOutputStream FileOutputStream fos = new FileOutputStream (fileName); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(gc); oos.close();

  28. Deserialization java.io.ObjectInputStream FileInputStream fis = FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(fis); Object obj = ois.readObject(); ois.close ();

  29. Date Date Calendar Date/Format SimpleDateFormat

  30. 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

  31. 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

  32. 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

More Related