lab 14 3 7 2

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

a

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

e. Define

a

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

a

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

a

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


Wyszukiwarka

Podobne podstrony:
elektro lab 14
lab 14 4 1 1
lab 14 4 3
Lab 14 - rozszerzalność, Skały14, Nr
Lab 14 - rozszerzalność, Skały141, Nr
lab 14
lab 14, MIBM WIP PW, fizyka 2, sprawka fiza 2, fizyka, fizyka
lab 14 4 1 2
PW LAB 14
IE RS lab 14 diagram
IE RS lab 14 solutions
lab 14 3 5
lab 14 1 8
Lab 14 - rozszerzalność, wsp.ciep.roz.14
lab t 14, t14 tabela pomiarowa, Przepływ współprądowy
lab 14 3 7 1
Lab 14 id 257442 Nieznany
elektro lab 14

więcej podobnych podstron