COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Week 8 Lab – ArrayLists and Classes

Posted by on September 14th, 2014 · Lab

The self-test quiz for week 8 is here.

ArrayLists and Classes

Download the BlueJ project club.zip 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.

Question 1

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.

Question 2

The Join method is passed a new member as an argument. You should add this member to the ArrayList.

Question 3

The numberOfMembers should return the number of elements in the ArrayList.

Question 4

The listMembers method should go through the ArrayList of members and print each one, one per line.

Question 5

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.

Question 6

The second constructor in the class allows you to pass an array of members. Initialise the club membership with the members in the array.

 

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.