background image

1 - 2 

Fundamentals of Java Programming Lab 14.3.7.2 

Copyright 

 2003, Cisco Systems, Inc. 

Lab 14.3.7.2

  

Creating a Collection to Store Integer Objects

 

 
Estimated Time:
 20 minutes  
 
Learning Objectives:  

•  In this lab activity, the student will use the HashSet object for storing integers using 

objects of the Integer class, and include exception handling. 

 
Description/Scenario: 

 

•  The two Set objects discussed in this chapter are the HashSet and the TreeSet.  

•  The HashSet class extends from AbstractSet, which extends from AbstractCollection.  

HashSet implements the Set interface.  A Set object does not allow duplicate objects 
to enter the collection. The collection is unordered and unsorted.  Note that the 
collection will fail when it tries to add duplicates.  The HashSet class overrides the 
toString method and creates a sequence of the items separated by commas, 
delimited by the open and close braces. 

•  Create a collection class called IntegerSet. This class will store only integers and no 

duplicate values of an integer is allowed.  This class will have methods for adding 
and removing values always ensuring that the rule stated here is followed.  The class 
will also have a method to display the objects in the collection using an Iterator 
object. 

•  Create an 'InvalidIntegerObjectException' class and use this in the IntgerSet class. 

This exception will be thrown in the methods that enforce the rules disallowing 
duplicates or other objects being added to the collection. 

 
File Management: 

Open BlueJ. Click on Project from the BlueJ main menu and select New. In the New 
Project window and in the Look in: list box select c:\. Now, double click the javacourse 
folder listed in the text window and a different New Project window opens with javacourse 
in the Look in: list box. Now, double click the chap14 folder listed in the text window and a 
different New Project window opens with chap14 in the Look in: list box. Type lab14.3.7.2 
in the File name text box and click on Create to create a lab14.3.7.2 subfolder in the 
chap14 folder. 

 
Tasks:
 
 

Step 1  Defining IntegerSet class 
 

a.  A Set is collection of objects without duplicates. The collection is unordered and 

unsorted. Define a class called IntegerSet, in this class define a private variable of 
type Set called integerElements to reference Set object. 

 
Sample Code: private Set elements = new HashSet(); 

 

b.  Define a method called addElement() that takes an Integer object as an argument. 

In the addElement() method use the add() method of the Set class to add the 

Integer objects to the set.  

c.  Define a method called exists() that takes an Integer object as an argument. In 

the exists() method use the contains() method of the Set class to check 

whether the element exists in the Set.  

d. Define 

getIterator() method that returns an Iterator of the Set object.  

e. Define 

removeAllElements() method that removes all the objects in the Set. 

This method uses the clear() method of the Set class.  

background image

2 - 2 

Fundamentals of Java Programming Lab 14.3.7.2 

Copyright 

 2003, Cisco Systems, Inc. 

f. Define 

removeElement() method which takes an Integer object as an argument 

and removes the object from the list. Use remove() method of the Set class to 

remove an object from the Set.  

g. Define 

toString() method which returns a String object containing the values of 

all the Objects in the Set. 

 

Step 2  Adding InvalidIntegerObjectException 
 

a.  This program should not allow duplicate integer values to be entered to the Set. 

Define an Exception class called InvalidIntegerObjectException that extends from the 
Exception class. This exception is thrown by the addElement() method which 

checks whether a duplicate element exists and raises the 
InvalidIntegerObjectException. 

 
Step3 Testing your program 

a. In 

the 

main method create an instance of the IntegerSet class. Add integer elements 

to the Set using addElement() method.  

b.  Test your program by removing an element from the Set, displaying all the integer 

elements in the Set, and removing all elements in the Set. Use try and catch 
blocks to handle Exceptions. 

 

 
 


Document Outline