Java Reference
In-Depth Information
month and year in which they joined the club. All membership details are filled out when an
instance is created. A new Membership object is added to a Club object's collection via the
Club object's join method, which has the following description:
/**
* Add a new member to the club's collection of members.
* @param member The member object to be added.
*/
public void join (Membership member)
Complete the join method.
When you wish to add a new Membership object to the Club object from the object bench,
there are two ways you can do this. Either create a new Membership object on the object
bench, call the join method on the Club object, and click on the Membership object to
supply the parameter or call the join method on the Club object and type into the method's
parameter dialog box:
new Membership ("member name ...", month, year)
Each time you add one, use the numberOfMembers method to check both that the join
method is adding to the collection and that the numberOfMembers method is giving the cor-
rect result.
We shall continue to explore this project with some further exercises later in the chapter.
Exercise 4.43 Challenge exercise The following exercises present a challenge because
they involve using some things that we have not covered explicitly. Nevertheless, you should
be able to make a reasonable attempt at them if you have a comfortable grasp of the material
covered so far. They involve adding something that most music players have: a “shuffle,” or
“random-play,” feature.
The java.util package contains the Random class whose nextInt method will generate
a positive random integer within a limited range. Write a method in the MusicOrganizer
class to select a single random track from its list and play it.
Hint: You will need to import Random and create a Random object, either directly in the new
method or in the constructor and stored in a field. You will need to find the API documenta-
tion for the Random class and check its methods to choose the correct version of nextInt .
Alternatively, we cover the Random class in the next chapter.
Exercise 4.44 Challenge exercise Consider how you might play multiple tracks in a random
order. Would you want to make sure that all tracks are played equally or prefer favorite tracks?
How might a “play count” field in the Track class help with this task? Discuss the various options.
Exercise 4.45 Challenge exercise Write a method to play every track in the track list exactly
once in random order.
Search WWH ::




Custom Search