Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 February 24, 2000. Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.

Similar presentations


Presentation on theme: "Lecture 7 February 24, 2000. Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the."— Presentation transcript:

1 Lecture 7 February 24, 2000

2 Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the @author tag itself (for my convenience). –Must put –version and –author on javadoc command line to get them to have any effect. But you don’t actually have to run javadoc for this course; just comment your code properly.

3 param, return, and throws Tags Put these in the comments at the beginning of each method you define. –@param One for each parameter passed to the method. Give its name, followed by a sentence telling what it is used for. –@return Tell what information is returned. Note that the type of value does not need to be mentioned. –@throws One for each throws clause in the method signature. Name the type of exception that might be thrown, and under what circumstances it would be thrown.

4 Insertion Sort Algorithm Each time a new word is to be added, it must be done in such a way that the array is always in sorted order. –So each word must be inserted into its proper place in the array, which may be at the beginning, middle, or end of the array. Examples that follow assume the capacity issue has already been solved. That is, that there is room in the array to add the new word. –Examples show just words, not word/counter pairs, to save space.

5 Add “hello” to empty array. hello length: 5 5 size:capacity: 15 size:capacity: 05

6 Add “world” to the end. hello length: 5 size:capacity: 15 hello length: 5 size:capacity: 25 world

7 Add “new” to the middle. hello length: 5 size:capacity: 25 world hello length: 5 size:capacity: 35 world new

8 Find the place to insert a word. for ( int i = 0; i < size(); i++ ) { if ( theWord.equals( theList[i].word ) ) { theList[i].counter++; needToAdd = false; break; } if ( theWord.compareTo( theList[i].word ) < 0 ) { insertWordAt( theWord, i ); needToAdd = false; break; } } // End of for loop if ( needToAdd ) { insertWordAt( theWord, size() ); }

9 Double Capacity of an Array Allocate new array. Copy old array to new one. Clean up extra reference. Point to the new one.

10 Double the capacity of an array. if ( ++numUsed >= capacity ) { WordCounter[] temp = new WordCounter[ capacity + capacity ]; for ( int i=0; i< capacity; i++ ) { temp[i] = theList[i]; } capacity += capacity; theList = temp; temp = null; }

11 Make room for a word at pos. for ( int i=numUsed; i>=Math.max(pos, 1); i-- ) { theArray[i] = theArray[i-1]; } theArray[pos] = theWordCounter; Move everything from pos to the end of the array “up” one position. Handles special case when numUsed and pos are both zero.

12 Vector -- a “Collection” class Like arrays –Vectors are objects –Vectors have an ordered set of elements Unlike arrays –The number of elements can vary during the lifetime of a Vector –The elements must be Objects, not primitives –Very flexible (add/remove elements, etc.) –Methods execute slower than array operations Actual implementation uses an array.

13 Class Vector A member of package java.util A subclass of java.util.AbstractList –This was not true in JDK 1.1, but is true in 1.2 Implements the following interfaces: –java.util.List A JDK 1.2 feature. –java.lang.Cloneable –java.io.Serializable Has: –4 Constructors –42 methods of its own Some of these override superclass methods. –3 methods inherited from java.util.AbstractList –7 methods inherited from java.Object

14 Some Vector Methods Add/Remove elements –boolean add( Object) –boolean remove( Object ) –Object remove( int ) –void clear() How many elements now? –int size() –boolean isEmpty() Access Methods –Object get( int ) –Object set( int, Object )

15 Sample Program [ Vector_ex.java ]Vector_ex.java Run it with various sequences of command line arguments to make sure it works for all cases.

16 Interface Iterator No constructor because it’s an interface, not a class. –Interfaces will be covered with the material in Chapter 9. Important methods: –boolean hasNext() –Object next()


Download ppt "Lecture 7 February 24, 2000. Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the."

Similar presentations


Ads by Google