Exception Handling Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com May 7, 2013 1 Agenda • • • • • • • Announcements Turn in Take-home Quiz 7 String class (for Prog Ex 11.2 and Lab 9) Assess Programming Exercises 11.1, 11.2 UML Class Diagrams (in detail) Exception handling Expectations for Final Exam 2 Announcements • Women in Engineering (WIN) meeting – Thursday, May 9, 4-6pm – ACES, Classroom Building • Lab 9 – Part I will be graded in lab today (must attend to get grade) – Part 2 on your own before Thursday – Part 3 will be graded in lab on Thursday • Plan to post all grades (hw, labs, etc.) by beginning of next week • Final Exam – Thursday, May 16, 10:00am-12:45pm – CCSB 1.0704 (this room) – Must get 60% on final exam to pass course • Final Exam Review with Peer Leaders – Friday, May 10, 10am-? , CCSB 1.0704 • Turn in research mini-projects by Friday, May 17, at the latest. 3 String class • The Java String class provides many methods for creating and manipulating strings. • A String object is immutable – i.e., it contents cannot be changed. • Examples (Try and draw a picture to illustrate): – – – – String s1 = new String(“Welcome to Java”); String s2 = “Welcome to Java”); String s3 = “Welcome to Java”); if (s1 == s2) System.out.println(“s1 and s2 are the same object”); – if (s2 == s3) System.out.println(“s2 and s3 are the same object”); – if (s1.equals(s2) System.out.println(“s1 and s2 have the same contents”); • See Chapter 9 and/or http://docs.oracle.com/javase/7/docs/api/java/lang/String.html for more String class methods. 4 Programming Exercise 11.1 • (The Triangle class) Design a class named Triangle that extends GeometricObject. The class contains: – Three private double data fields side1, side2, side3 with default values of 1. – A no-arg constructor that creates a default triangle. – A constructor that creates a triangle with specified side1, side2, and side3. – The accessor methods for all three data fields. – A method getArea() that returns the area of the triangle – A method getPerimeter() that returns the perimeter of the triangle – A method toString() that returns a string representation of the triangle. 5 Programming Exercise 11.2 • (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, senior). Define the status as a constant. An employee has an office, salary, and date hired. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person’s name. 6 UML Class Diagrams • See http://cs.nmu.edu/~mkowalcz/cs120f08/uml/UMLdiagram.html 7 Exception Handling • Enables a program to deal with exceptional (i.e., error) situations and continue operation • Can make programs more reliable • Examples of exceptions – Integer division by zero – Input mismatch (e.g., real number entered when integer was requested) – Illegal argument value • Enable methods to “throw” exceptions back to caller – Why not just have the method print an error message and exit? • Caller uses a try-catch block to invoke the method and handle any exception 8 Exception Types • Exceptions are objects that are defined using classes. • Pre-defined exception types in java.lang.Throwable class – http://docs.oracle.com/javase/7/docs/api/java/lang/R untimeException.html – e.g., ArithmeticException, NullPointerException, IndexOutOfBoundsException, IllegalArgumentException • You can also define your own exception types. 9 Examples • Download from course website: – QuotientWithException.java (trivial example for illustration purposes) – InputMismatchExceptionDemo.java – Circle3.java (also need GeometricObject.java) – Circle4.java – InvalidRadiusException.java – TestCircles.java 10 Class Activity • Download from course website: – Projectile.java – TestProjectile.java • Modify the program to handle the following exceptions – Invalid input • Initial height must be non-negative • Initial velocity must be positive • Angle of inclination must be greater than 0 and less than 90 – Invalid time step interval (must be positive) 11 Final Exam • Comprehensive • Will target learning outcomes from each unit • Review guide and practice problems will be posted by tomorrow. • Review in class on Thursday • Review session with Peer Leaders on Friday at 10:00 12