Unformatted text preview:

April 4th 2014 Lecture 3 public class Lecture3 1 These two methods are similar usually more specific calls general public void add int value elementData size value size public void add int value add size value public void add int index int value checkIndex index 0 size check bounds are different for add Instead of size 1 size for int i size i index i elementData i elementData i 1 elementData index value size 2 Shifting everything towards the left public void remove int index checkIndex index 0 size 1 for int i index i size 1 i make sure size 1 Didn t crash because end was zeroed out elementData i element i 1 size Allows the client to see and use size but doesn t have access to it directly public int size return size 3 Preconditions something your method assumes is true at the start of its execution often documented as a comment on the method s header Returns element at the given index Precondition 0 index size public int get int index return elementData index 4 Throwing exceptions generates exception that will crash the program unless it has code to catch the exception throw new ExceptionType throw new ExceptionType message ArithmeticException IndexOutOfBoundsException FileNotFoundException IllegalArgumentException etc still have to document the precondition Now not only are we documenting we are implementing throw is implementation side throws is on client side 5 Returns the value at the given index Pre This works only if 0 index size throws IndexOutOfBoundsException otherwise public int get int index if index 0 index size Won t work because it doesn t actually stop the code throw new IndexOutOfBoundsException index System out println BAD index return elementData index Similar to get int index public void set int index int value checkIndex index elementData index value if index 0 index size throw new IndexOutOfBoundsException index index elementData index value Create a helper method Make it private Anything the client doesn t need to know about private private void checkIndex int index int min int max if index min index max throw new IndexOutOfBoundsException index index 6 this keyword this reference to implicit parameter object on which a method constructor is called 7 public class ArrayIntList private int elementData private int size public static final int Gives client ability to set size However public ArrayIntList int capacity elementData new int capacity size 0 optional size is initialized to 0 by default ALSO create a constructor if client doesn t specify size public ArrayIntList elementData new int 10 However redundant Create this code public ArrayIntList this 10 8 Running out of space double the size if data exceeds size a copyOf array length returns new resized copy of an array 9 Postconditions something your method promises will be true at the end of the execution Makes sure that this list s internal array is large enough to store the given number of elemnts Post elementData length capacity


View Full Document

UW CSE 143 - Lecture notes

Documents in this Course
Load more
Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?