Activity 2.9 - Adding methods to the ComputerCollection class

Topic

This activity involves adding methods to the existing ComputerCollection class. It also requires an additional instance variable to be declared and initialized, and an extra statement to be added to an existing method.

Materials

In addition to the discussion of the solution in the Solution document, the project for this activity contains two classes:

Tasks

A ComputerCollection keeps track of computers connected to a network. (For the purpose of our activity a computer is represented by a string corresponding to its name.) Methods already exist to add a computer to the network, and to remove a computer, as well as to tell the index of a computer in the collection.

  1. Complete the method isInNetwork so that it returns true if its string argument is in the collection, and false otherwise, making use of the existing indexOf method.
  2. Edit removeComputer so that it updates the numOfRemovals variable when a computer is removed from the collection.
  3. Add a method getNumOfRemovals, to return the value stored in numOfRemovals.

Instructions

The project will not compile at this stage.

Open the class ComputerCollection and study the code there. Read the comments and insert the necessary statements at the numbered points.

Examine the class ComputerCollectionTest to see what it does with the collection. Run the project to test your answer.