Java Reference
In-Depth Information
Exercise 4.50 Suppose that the Auction class includes a method that makes it possible
to remove a lot from the auction. Assuming that the remaining lots do not have their lotNum-
ber fields changed when a lot is removed, write down what you think the impact would be on
the getLot method.
Exercise 4.51 Rewrite getLot so that it does not rely on a lot with a particular number
being stored at index (number-1) in the collection. For instance, if lot number 2 has been
removed, then lot number 3 will have been moved from index 2 to index 1, and all higher lot
numbers will also have been moved by one index position. You may assume that lots are al-
ways stored in increasing order according to their lot numbers.
Exercise 4.52 Add a removeLot method to the Auction class, having the following
header:
/**
* Remove the lot with the given lot number.
* @param number The number of the lot to be removed.
* @return The Lot with the given number, or null if
* there is no such lot.
*/
public Lot removeLot(int number)
This method should not assume that a lot with a given number is stored at any particular loca-
tion within the collection.
Exercise 4.53 The ArrayList class is found in the java.util package. That package
also includes a class called LinkedList . Find out what you can about the LinkedList
class, and compare its methods with those of ArrayList . Which methods do they have in
common, and which are different?
4.14.7
Using collections
The ArrayList collection class (and others like it) is an important programming tool, because
many programming problems involve working with variable-sized collections of objects.
Before moving on to the rest of this chapter, it is important that you become thoroughly familiar
and comfortable with how to work with them. The following exercises will help you do this.
Exercise 4.54 Continue working with the club project from Exercise 4.40. Define a method
in the Club class with the following description:
/**
* Determine the number of members who joined in the
* given month.
* @param month The month we are interested in.
 
Search WWH ::




Custom Search