1 - 3
Fundamentals of Java Programming Lab 14.4.1.2
Copyright
2003, Cisco Systems, Inc.
Lab 14.4.1.2 Sets and Iterators
Estimated Time: 60 minutes
Learning Objectives:
•
In this lab activity, the student will create a SortedSet object to hold a collection of
Customer objects sorted by customer information (such as id or name) and scan through
the collection using Iterators.
Description:
• Modify the Teller class to make use of SortedSet to maintain Customer objects sorted by
customerIDs. Use the Iterator to scan through the set to access a customer object by its
customerID.
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.4.1.2
in the File name text box and click on Create to create a lab14.4.1.2 subfolder in the
chap14 folder. Import Jbank classes from previous lab14.4.1.1
Tasks:
Step 1 Storing customer objects in a Sorted Set
a. SortedSet is an interface. The class that implements the SortedSet is the TreeSet. In
the Teller class change the variable customers to type SortedSet. The customers
variable can now reference an object of type TreeSet.
b. An object inserted into a SortedSet should implement the interface Comparable. In our
case the Customer class should implement the interface Comparable. Change the class
definition of the Customer class to implement Comparable and define a method called
compareTo(). The compareTo() method is used in the SortedSet methods to insert or
to search for an object by comparing its value with the other objects in the set. In our
case the Customer objects are sorted by the customerID. Use the following code sample
to implement the compareTo() method based on customerID:
public int compareTo(Object objectToCompare)
{
Customer c = (Customer) objectToCompare;
return (this.custID - c.getCustID());
}
c. In the Teller class, read the Customers objects stored in the “customers.dat” file using
Vector output of the CustomerFileReader class from the previous lab and use the
following syntax to create a TreeSet of customers:
customers = new TreeSet(customerFileReader.read());
d. To add a new customer to the customers set use the add() method the SortedSet.
2 - 3
Fundamentals of Java Programming Lab 14.4.1.2
Copyright
2003, Cisco Systems, Inc.
Step 2 Scanning Sorted Set using Iterators
a. To Search for a customer in the customers set by customerID, use the iterator()
method of the SortedSet to get the reference to the SortedSet. Within a while loop
using hasNext() method to break the while loop, iterate through the set. Use the
next() method to get the reference of the object in the set and verify that the
customerID of the object to find the match.
Sample Code
public Customer getCustomer(int customerID) {
Iterator iter = customers.iterator();
while
(iter.hasNext())
{
Customer c = (Customer) iter.next();
if (c.getCustID() == customerID) {
return
c;
}
}
return
null;
}
Step 3 Documentation
Using the Document “How to use UMLTestTool”, follow the instructions to verify your JBANK
classes to match the JBANK UML diagram shown below.
Write javadoc comments to the methods introduced in this lab.
Step 4 Preparing your Application for Deployment
This is the final phase of JBANK Application. Save the classes in lab14.4.1.2 to the
phase4 folder in order to complete your packaging.
3 - 3
Fundamentals of Java Programming Lab 14.4.1.2
Copyright
2003, Cisco Systems, Inc.