Comp 170 Lab 2 - Adding to the Notebook Sept 17, 2007
Goals
- Introduction to Loops
- Introduction to ArrayLists
Overview
For this lab you are to take the "stub" Club project -- dealing with club membership records -- and add some
features. The club project is zipped here;
save it and open it.
I've added a few things that are not in the version from the book. You
are to extend the constructor in class Membership, and add the methods
indicated below to class Club.
The Constructor
The existing constructor for Membership has parameters for name, month,
and year. I've added a field donations to this class; create a second
constructor with parameters for name, month, year, and donations. Do
not delete or change the existing constructor.
Methods / Loops
You are to provide the following methods to do the indicated things:
1. join(Membership member): This will add member to the list members.
2. listMembers(): This will be a loop to print all the members.
3. search(String s): Prints all members whose name contain the given string s as a
substring. To check if s is contained in a member, use the library
contains() method as follows:
Membership member = members.get(N);
if ( member.getName().contains(s) ) {
// print member
}
4. totalDonations():
Returns (not prints) the total of the donations of all the members. You will need a temporary variable such as sum.
5. earliestMember():
Prints the member who joined earliest. It is acceptable to print the member with the smallest year of joining.
int minYear = 3000; // initialize to a value >= any valid year
int position = 0;
Membership m = members.get(N);
if (m.getYear() < minYear) { // earliest year found so far
position = N;
}
After the completion of the loop, you print out members.get(position), the note with the given position.
For one point of extra credit, get this to work for the minimum (year,
month) combination: have the loop return the member with the minimum
joining date. You will need two variables: minYear and now minMonth,
and the test for whether member m is earlier now becomes
if (m.getYear() < minYear || (m.getYear() == minYear && m.getMonth() < minMonth) )
Email me your completed project. To create a zipfile out of a
folder, navigate to the folder in the Windows Explorer (that is, not in
BlueJ), right-click
on the folder, and select Send To => Compressed (zipped) folder.
This should create an ordinary file named <foldername>.zip. Then
attach that zipfile to an email to me.