Vectors

Chapter 3 Vector Class
Agenda
• Design and Implementation of Vector class –
add, get, set remove, copy, equals,
ensureCapacity
• Hangman using Vector class
• Word Frequency
• L-system
• Installing bailey.jar in BlueJ and Eclipse
Bailey’s Vector Interface
(similar to java.util.Vector)
Rest of interface
Creating a list
Adding values to a list
Removing an item
Adding at a given location
Demo Programs
Implementation
Algorithm Development
• size() is easy (return elementCount;)
• What about delete(2) ??
– Before, list= ABCCDEFGHIJKL
– After, list= ABCDEFGHIJKL
– Some kind of shifting required
• for loop
– If we try to guess it, we’ll most likely fail
– Is there a method to developing a correct alg.?
11
Algorithm Method
1. Start with a specific function call
– With specific data & arguments, i.e.
set up list with ABCDEFG
list.remove(3);
2. Draw a picture of data structure
– Before and after function is called
– Required for any assistance from instructor!!
3. Write out statements without any loop
4. Generalize into loop that works for all args
5. Add assert to prevent bad calls
12
Before and after remove(3)
Non-looping Specific Solution
• For our particular case, index (letter to erase) is 3
• Statements to solve our example case:
elementData[3] = elementData[4];
elementData[4] = elementData[5];
elementData[5] = elementData[6];  why is this the last copy?
Then
elementData[6] = null; // free the memory
elementCount--; // we are now one shorter
14
Formulate the General Soln
• We can now see that
– 3 in the first statement is really index.
– 6 in the last statement is size of the list - 1
• Therefore we can derive:
public void delete(int index)
{
for (int k=index; k < elementCount-1; k++)
elementData[k] = elementData[k+1];
elementData[elementCount-1]=null;
elementCount--;
}
• And check desired statements are produced by for loop
15
Importing bailey.jar
• Eclipse: Copy bailey.jar into your workspace folder
Then, To configure build classpath, in Eclipse Project
Properties Window, click Java Build Path in the left panel, and
in the right panel choose Libraries tab. There are more
classpath-related elements in Eclipse: JARs, External JARS,
Variables, Libraries, Class Folders, and other Projects. Click
Add External JARs (not Add JARS) if you want to add a jar file
to classpath, and click Add Class Folders if you want to add a
directory or folder to classpath.
• BlueJ: copy bailey.jar into the C:/BlueJ/lib/userlib folder
Inheritance from Eck
• Access modifiers such as public and private are used to control
access to members of a class. There is one more access modifier,
protected, that comes into the picture when subclasses are taken
into consideration. When protected is applied as an access modifier
to a method or member variable in a class, that member can be
used in subclasses -- direct or indirect -- of the class in which it is
defined, but it cannot be used in non-subclasses. (There is one
exception: A protected member can also be accessed by any class in
the same package as the class that contains the protected member.
Recall that using no access modifier makes a member accessible to
classes in the same package, and nowhere else. Using the protected
modifier is strictly more liberal than using no modifier at all: It
allows access from classes in the same package and from
subclasses that are not in the same package.)
Super() from Eck
•
•
Obviously, there has to be some fix for this, and there is. It involves the special
variable, super. As the very first statement in a constructor, you can use super to
call a constructor from the superclass. The notation for this is a bit ugly and
misleading, and it can only be used in this one particular circumstance: It looks like
you are calling super as a subroutine (even though super is not a subroutine and
you can't call constructors the same way you call other subroutines anyway). As an
example, assume that the PairOfDice class has a constructor that takes two
integers as parameters. Consider a subclass:
public class GraphicalDice extends PairOfDice { public GraphicalDice() { //
Constructor for this class. super(3,4); // Call the constructor from the // PairOfDice
class, with parameters 3, 4. initializeGraphics(); // Do some initialization specific //
to the GraphicalDice class. } . . // More constructors, methods, variables... . } The
statement "super(3,4);" calls the constructor from the superclass. This call must be
the first line of the constructor in the subclass. Note that if you don't explicitly call
a constructor from the superclass in this way, then the default constructor from
the superclass, the one with no parameters, will be called automatically.