CSE116 / CSE504 Introduction to Computer Science II Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM – 2:00 PM Friday 8:30 AM – 10:30 AM OR request appointment via e-mail PROFESSIONALISM Turn off and put away electronics: cell phones pagers laptops tablets etc. © Dr. Carl Alphonce ANNOUNCEMENTS Sign up for Piazza (up to 95% registration) 15 students have still not signed up piazza.com/buffalo/spring2016/cse116 Recitation Write-ups are starting this week Write-Up Grading If your submission scores 100% on automated grading, you receive 100%. If your submission does not score 100% on automated grading, then (i) the automated grade does not count, and (ii) your submission goes for manual grading. In manual grading the outcome will be one of four grades: 100 – a correct solution 70 – submission is on the right track, but is has errors 30 – submission is wrong, but related to problem 0 – no submission, or unrelated to problem ROADMAP Last class defining the MultiSet managing memory in array invariant properties Today defining the MultiSet Coming up defining the MultiSet public class MultiSet<E> implements Collection<E> { MultiSet<E> development STEP 2: array for storage private E[] _store; private int _size; public MultiSet() { _store = (E[]) (new Object[10]); _size = 0; } @Override public boolean add(E e) { _store[_size] = e; _size = _size + 1; return true; } @Override public int size() { return _size; } // rest of class body omitted } MultiSet<E> development runtime view MultiSet<Jlabel> b = new MultiSet<Jlabel>(); b _store INVARIANT PROPERTIES _size indicates: _size • the number of elements in the MultiSet 3 0 1 • the first available position in _store 2 occupied region 3 4 5 6 7 available region 8 9 Invariants An invariant property of a class must hold between method invocations: • A constructor must establish the invariants. • If a method breaks an invariant it must restore it before exiting. Invariants of our class: • _size indicates the number of elements in the MultiSet • _size refers to the first available position in the _store © Dr. Carl Alphonce re-size On to eclipse to deal with re-size issue. © Dr. Carl Alphonce public class MultiSet<E> implements Collection<E> { Observation private E[] _store; private int _size; Classroom coding Invariants break here when _size == _store.length (because _size does NOT public MultiSet() { refer to an available _store = (E[]) (new Object[10]); position in the array _store _size = 0; points to). } @Override public boolean add(E e) { _store[_size] = e; _size = _size + 1; return true; } @Override public int size() { return _size; } // rest of class body omitted } © Dr. Carl Alphonce public class MultiSet<E> implements Collection<E> { private E[] _store; private int _size; Resolution Classroom coding if (_size == _store.length) { // allocate larger array public MultiSet() { // copy data _store = (E[]) (new Object[10]); } _size = 0; } @Override public boolean add(E e) { _store[_size] = e; _size = _size + 1; return true; } @Override public int size() { return _size; } // rest of class body omitted } © Dr. Carl Alphonce Classroom coding @Override public boolean add(E e) { _store[_size] = e; _size = _size + 1; if (_size == _store.length) { E[] temp = (E[]) (new Object[_store.length * 2]); for (int i=0; i<_store.length; i=i+1) { temp[i] = _store[i]; } _store = temp; } return true; } © Dr. Carl Alphonce Exercise for next class Define the contains method. © Dr. Carl Alphonce