Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

2017-11-01

posted Nov 1, 2017, 10:21 AM by Konstantinovich Samuel   [ updated Nov 1, 2017, 12:40 PM ]
Goal: Ordered Super Array and Exceptions


How do we create meaningful error messages?

Runtime errors that stop your program:

Syntax is:
throw new ExceptionConstructor();

e.g.
throw new IndexOutOfBoundsException();
throw new UnsupportedOperationException();



Assignment: Due Friday Morning 
Modify your super array, and your OrderedSuperArray

SuperArray
Add one extra constructor:

SuperArray(int startingCapacity)


OrderedSuperArray

Since Constructors are not inherited:

public OrderedSuperArray()  // You already did this!

public OrderedSuperArray(int capacity)  //-> just call super( capacity );

Do this constructor last:

    public OrderedSuperArray(String [] ary) //-> call super and add all the elements


Methods you need to change:

0. CONVERT Your printed error messages in SuperArray into exceptions.

1. set(int index, String element)

    - throw an UnsupportedOperationException

2. add(int index, String element) 

    call add(String) and ignore the index. You should already have this. 

3. You can make the following methods more efficient using a binary Search.  (Discuss today)

    indexOf(String element) HINT: make indexOfBinary(String element) , then compare to the original working method!  

    lastIndexOf(String element)

3. add (String element) 

    You should have already written a private method to find the index that you want to add the element to:

         private int findIndex( String) - a linear search for the proper location.

    Now you can write this alternate version and test against the original findIndex method!

         private int findIndexBinary(String) - a binary search for the proper location




 
Comments