CSC 205 Lab 4: Useful Utility Classes
Goals
After this lab, you should be able to
· use the StringTokenizer and Vector classes in the java.util package
· use the List and TextField classes in the java.awt package
· write simple application with your own “database”
· use the GUI template used in your new textbook
Lab Preparation
Read Appendix 2 in Collins’s text.
Materials Needed
Be sure you have the following on hand during the lab.
· This sheet
· Your textbook.
Lab Setup
The source code files we are going to use as a code base are located in the following directory.
Courses on ‘Blackhawk’\Martin Zhao\205\labs\lab4
Make a folder for you on the C: drive temporarily or on a floppy disk. Make a directory (desirably csc205/labs/lab4) on Cobra also and ftp your code there after you’ve finished. Turn in your answer sheet or sent your answers by email.
Part 1: The MyPhoneDirectory Application
/ This application provides a GUI to a simple “database” of names and corresponding phone numbers, which is stored in a text file (My-PhoneDirectory.txt). It also allows a user to manage his/her phone directory by adding, updating, and deleting name-number entries.This application features file accessing, utility classes (StringTokenizer and Vector), in addition to AWT programming.
1.0 Prepare a text file. It’s perfectly fine to store the phone directory in a text file since the phone number will not be used as a numeric type. Although a graphical interface is available, a user can read the file if he/she really wants to. An entry is written in the following format
<name>:<number>
Entries are separated by semicolons (“;”), as follows
<entry>;<entry>;… …
Generate such a file with your favorite name-number entries, and name your file as PhoneDirectory.txt.
1.1 Write the PhoneDirectoryEntry class. This class encapsulates a name and a corresponding number, as well as several methods. Implement this class after the instructions given in file MyPhoneDirectory.java. (You can do a search on the string **1. to jump to the right location in file MyPhoneDirectory.java. In many of the IDE editors, you can click ^f to invoke the search dialog.)
1.2 Parse the text file. The StringTokenizer class can be used to parse entries from the file. The first level delimiter (;) can be used to create a StringTokenizer object as follows
StringTokenizer st = new StringTokenizer(line, ”;”);
After an entry is parsed out, it can be used to create another StringTokenizer object by using the delimiter (:) to separate the name and the number. With the name and number available, you can create a new PhoneDirectoryEntry object and add it to the vector v.
Question: what will be returned if any exception being thrown during the file access, e.g., the file doesn’t exist?
1.3 Prepare the name list. The name saved in the Vector variable entries will be retrieved and add to the name list used in the GUI. Two points to watch out:
a. The number of entries in entries is needed in the loop;
b. The element of entries with an index i needs to be cast into type PhoneDirectoryEntry before you can get the name from it.
1.4 Update number. The name or number information of an existing entry can be updated in the GUI, by simply changing the data in the corresponding text field and hitting Enter. You are asked to implement the updateNumber() method, following the example given in the updateName() method.
1.5 Add an entry. Two ways of adding a new entry are supported in this application. The first one is to append the entry to the end of the name list, and, at the same time, to the database, entries. This happens when the list is empty or after you click the Clear button. Implement this functionality in the if clause. The second way is to insert the new entry next to the current entry. This happens when you select an entry from the list and change the name and/or button and click the Add button. Implement this functionality in the else clause.
1.6 Complete the WindowCloser class. Before the execution can be terminated by invoking System.exit(0), you have to update your database file with the current information stored in entries. Add one line of code to invoke the right method to do it, and implement that method, updateDirectory(), as described in 1.7.
1.7 Update the text file. Write all entries currently available in the entries variable into the file. You have to take care of delimiters needed to separate adjacent entries.
Part 2: The Graphic Interface Used In Collins’s Book
Compile the following Java source files, Age.java, GUIListener.java, GUI.java, and Process.java. Execute (using java Age) and play with it. You will see a window with input and output fields as illustrated below. Read the specified source code and answer the following questions.
2.1 In the GUI class, there are four different versions of either print() or println() method. Would these methods be sufficient for printing all possible types of data? Two different styles are adopted in the two methods. Use print(long i) and println(long i) as an example, explain why is the case?
2.2 In the GUIListener class, explain the event handling pattern as defined in the actionPerformed() method. Use the definition of Process when needed.
2.3 In the Age class, there are two major scenarios when an input string is processed, depend on whether or age is equal to the SENTINEL value or not. Explain what this SENTINEL value is used for.
2.4 Write an Average class, which resembles the Age class. The only difference is to print out an average value when the program terminates, instead of the maximum value. Use messages that are meaningful for the new application.
The GUI for the Age application, when first started.The GUI for the Age application, when terminated with the Input field disabled.
4