Download the BlueJ project for this week’s exercises. There are three classes in the project that help you to represent a club and its members.
The Member class is fully defined and contains all the methods you need. Note a couple of things we haven’t covered in lectures. The constructor uses a throw statement to create a Java runtime error. Try entering a month with a number outside the range 1 .. 12 and see what happens.
The Member class also defines a method called toString. This returns a string representation of the object. If you try to print the object using System.out.println, the system will used toString to convert your object into a string for printing.
The ClubDemo class is the “main” class that we use to create a club and members and test the other classes.
The Club class is incomplete. The methods in the class are empty and your job is to write the code inside to make them work.
The club class needs a way of storing a collection of members. You should use an ArrayList to do that. The class includes two constructors. Let’s start with the first one. It should initialise the ArrayList and leave it empty.
The Join method is passed a new member as an argument. You should add this member to the ArrayList.
The numberOfMembers should return the number of elements in the ArrayList.
The listMembers method should go through the ArrayList of members and print each one, one per line.
The removeMember method should remove a member from the ArrayList when a member decides to leave the club. The argument to the method is a string, which is the member’s name. You have to search the ArrayList to find the member with that name. Use a simple algorithm, just go through the list, one-by-one until you find the member with the given name and remove it from the ArrayList.
The second constructor in the class allows you to pass an array of members. Initialise the club membership with the members in the array.
There are no comments yet...Kick things off by filling out the form below.
You must to post a comment.