Monday 9/20 Announcements: Lab 1 due today at 11 PM Lab 2 and Homework 2 posted Wednesday Quiz 1 : 15-minute quiz Chapter 1 only Next time No new reading assignment Study for the quiz Do the “before lab” for Lab 2 Today: Using a UML to write a class Scope of variables checked and unchecked exceptions try-catch List interface and ArrayList Lab 2 (15 minutes) UML Diagrams Write the class employee Scope of a field variable Scope of a local variable Exceptions Unchecked Exceptions Checked Exceptions Checked and Unchecked What is the difference between checked and unchecked exceptions? Statements or methods that may throw a checked exception must be handled Throw exception to calling method -- throws Surround statement with try-catch Two examples FileNotFoundException (checked) InputMismatchException (unchecked) Use try-catch to handle checked exceptions Starting after Lab 1 try-catch //code inside main or some method String filename = "data.dat"; File infile = new File (filename); Scanner scanFile = null; try { scanFile = new Scanner (infile); } catch (FileNotFoundException e) { System.out.println("Could not open file :" + filename); //e.printStackTrace(); //System.out.println(e.getMessage()); //open some default file //System.exit(-1); } try contains code that may throw an Exception catch has code that is executed when the Exception is thrown Visibility/Access Modifiers Visibility Applied to Class Member - private visibility only inside the class ~ (blank) default visibility to classes in this package # protected visibility to classes in this package and all subclasses + public all classes Client View: Array vs. ArrayList Properties of a primitive array Once created the length is fixed User must keep track of “logical size” Must shift to add/remove elements Can create an array of primitives Properties of an ArrayList Automatically grow and shrink as needed Methods for • Size • Search • Traversal either direction without subscripts Add/Remove without shifting Can only create ArrayList of Objects ArrayList Generic Class Generic ArrayList Type parameter String Example: ArrayList names = new ArrayList(); names.add(“Mary”); … String s = names.get(0); ArrayList is a raw type compile time error, why? (Java 1.5) java.util.List Interface Required List methods: add(i, data) get(i) size() isEmpty() indexOf(data) … Part of List Interface (ArrayList and LinkedList) Client View of ArrayList Table 2.1: Some methods of Class ArrayList public E get(int i) public E set (int i, E entry) public int size() public boolean add(E entry) public void add(int i, E entry) public int indexOf(E target) public E remove(int index) Similar to Problem 2.2.1 String temp; List myList = new ArrayList<> (); myList.add("Pokey"); myList.add("Campy"); int i = myList.indexOf("Happy"); temp = myList.set(i, "Bouncy"); temp = myList.remove(myList.size() - 2); temp = myList.get(1); temp = myList.set(1, temp.toUpperCase()); “Bashful” “Awful” “Doc” “Jumpy” “Happy” “Dopey” ArrayList Practice Assume we are in a main method Declare and instantiate an ArrayList of String Create the List: Marge, Homer, Lisa, and Bart Print the size of the ArrayList Retrieve and print the 3rd element in the list Find and print the index of Bart Write a loop that prints all the elements Lab 2 Questions The “before lab”