background image

Lab 14.4.1.1  File I/O using Collection Classes 

 
Estimated Time:
 60 minutes  
 
Learning Objectives:  

•  In this lab, the student will use Vector objects to store Customer objects retrieved from a 

file. 

 
Description: 

•  This lab combines the use of I/O and collection technologies. The skills developed in this 

lab will be useful for creating classes to extract data from databases. 

•  Modify the CustomerFileWriter class to accept a Vector and save the contents of the 

Vector to “customers.dat” file.  

•  Modify the CustomerFileReader class to read back the Vector from the “customers.dat” 

file. 

 
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.1 
in the File name text box and click on Create to create a lab14.4.1.1 subfolder in the 
chap14 folder. Import JBANK classes from lab13.5.1.2. 

 
 Tasks: 
 

Step 1 Using Object Vectors to write and read customers Objects 

a. Modify 

the 

saveCustomers() method signature of the CustomerFileWriter class to 

accept an argument of type Vector.  In this method, pass the Vector as a parameter to 
the writeObject() method of the ObjectOutputStream objectOutputStream. 

b. Modify 

the 

readCustomers() method signature of the CustomerFileReader class to 

return a Vector. Modify the customers attributes to hold a Vector.  In this method, type 
cast the output of the readObject() method of the ObjectInputStream 
objectInputStream to Vector type and return the Vector. 

c.  In the Teller class modify the attribute customers of type Customer array object to 

Vector type. Include the import statement import java.util.*;. In the Teller class 
constructor, read the customer data stored in the file by calling the readCustomers() 
method of the CustomerFileReader. Handle the IOExceptions in a try-catch block. 
Modify the createNewCustomer() method in the Teller class to add customers to the 
Vector. Modify the getCustomer() method by using an Iterator to traverse through the 

Vector and return a Customer object.  

d.  In the Teller class main method create Customers  objects and save them in the file. 
e.  Use the Console class to accept a customerId. Call the getCustomer() method 

which takes a customerId as an argument. Display the Customer with customerId of 

1002. 

 
Step 2 Documentation 

Using the Document “How to use UMLTestTool”, follow the instructions to verify the 
JBANK classes match the JBANK UML diagram shown below.

 

 
Write javadoc comments to the changes introduced in this lab. 

 

1 - 1 

Fundamentals of Java Programming Lab 14.4.1.1 

Copyright 

 2003, Cisco Systems, Inc. 

background image

 
 
 
 
 

 

2 - 2 

Fundamentals of Java Programming Lab 14.4.1.1 

Copyright 

 2003, Cisco Systems, Inc.