RHS =>  Allan =>  Basic Programming => Exercises => BorrowerVector Updated:  02/02/2004 23:36
WebMaster: allanhn@rhs.dk

BorrowerVector

This exercise is no. 4 in the story of Fantasy Library. The previous exercises were:
Fantasy Library No. 1: Book
Fantasy Library No. 2: LibraryHits
Fantasy Library No. 3: BorrowerArray (collection)

The objective
We want a program, which can register and keep records of borrowers using the data type Vector instead of array.

Assignment 1: Collection class: BorrowerCollectionVector
The borrowers must be recorded in a BorrowerCollectionVector class encapsulating a vector of borrower objects using the following template

Data fields:
Vector data (holds the data for borrowers)

Constructors:
BorrowerCollectionVector()

Methods operating on the data fields:
boolean add(Borrower borrower)
Borrower get(int index)
Borrower remove(int index)
Borrower int size()
etc.

First of all note that we can reuse the old array based version of BorrowerCollection as all the methods signatures are the same !!!!!
Copy the old version to BorrowerCollectionVector.java.

Change the class into a BorrowerCollectionVector the following way:

Import java.util.Vector;

Change class-name to BorrowerCollectionVector

Vector data instead of Borrower[] data (so no freeIndex is needed)

BorrowerCollectionVector() as the constructor declaring the data-vector
Note that now there is no noOfBorrowers and freeIndex, i.e. this method is just constructing the vector

boolean add(Borrower borrower): adds borrower to the vector, returns true always as Vector expands automatically

String toString(): returns a String with all borrower data separated by line breaks("\n")

Before compiling just out comment the methods that you cannot call at the moment!
Compile it!

Assignment 2: New Application class: (BorrowerCollectionVectorApp)
Now create a new application class BorrowerCollectionVectorApp with the usual main() method:

construct an object, borrowers, of the class BorrowerCollectionVector
read in borrower data
construct an object, borrower, of the class Borrower
add borrower to the collection borrowers
print out the content of borrowers

Remark
Very similar to BorrowerCollectionApp. Except for the name "BorrowerCollectionVector" and the construction of the borrowerData (now no initial size is necessary), there are no changes at all !!!!
Just out comment the methods that you cannot call right now.
Compile and run!


Assignment 3: Number of borrowers
Change getNoOfBorrowers(), so it uses appropriate method from Vector.
In the BorrowerCollectionVector class show the use of this methods, e.g. by printing the numbers.
Remove getCapacity() (as Vectors can grow as needed).
Also remove extend() (and the add() method that uses extend automatically.


Assignment 4: public Borrower get(int index)
Change get(int index), so it uses appropriate method from Vector.
Remember that elementAt(int) returns the element as an Object type.

Assignment 5: public Borrower remove(int index)
Change remove(int index), so it uses appropriate method from Vector.
Note that removeElementAt(int) doesn't return the object removed - your method must do that!!
Returns null if no object exists or index out of range.

Assignment 6: public Borrower get(String cprNo)
Change get(String cprNo), so it uses appropriate method from Vector.
You have to do it nearly the same way as when you used an array - but remember again that when you get an element from the Vector it is returned as the type Object.
Returns null if no object exists or index out of range.


Assignment 7: public Borrower remove(String cprNo)
Change remove(String cprNo), so it uses appropriate method from Vector.
Returns null if no object exists or index out of range.

Assignment 8: public int remove(int ageLimit, String sex) - difficult
In BorrowerCollectionVector declare a new method, public int remove(int ageLimit, String sex), which removes all borrowers who is elder than ageLimit and has the same sex as sex.
You have to find a way to calculate the age....
The method should return the number of removed borrowers.
In BorrowerCollectionVectorApp show how this method can be used to remove males over 70, and print out this number.


Assignment 9: public BorrowerCollectionVector split(int ageLimit, String sex)
- difficult
In BorrowerCollectionVector declare a new method, public BorrowerCollectionVector split(int ageLimit, String sex), which splits the Vector into two:
From the original Vector all borrowers who is elder than the ageLimit and with the same sex as sex must be moved into a new Vector.
The method should return new Vector.
In BorrowerCollectionVectorApp show the use of this method and print the removed borrowers.