INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM
ICS202 : DATA STRUCTURES
LAB #02 : More on Design
Patterns
To gain experience with:
·
The SearchableContainer
Pattern
·
The Association
Pattern
Download the file lab02.zip and
unzip it inside the ics202 main folder. WinZip will add the
following files in the ics202 folder.
·
SearchableContainer.java
·
Association.java
WinZip will also create a sub-folder lab02 under the ics202 folder and store the following files:
MySearchableContainer.java
Student.java
Instructor.java
TestInstructorStudentAssociation.java
TestAssociation.java
ByWordLength.java
input.txt
Now open each of the four java files
to make sure you understand them
Open the file,
MySearchableContainer.java and study it carefully. You
will notice that it is extending MyContainer of ics202.lab01
package and also claiming to implement the SearchableContainer
interface. For this claim to be true, it has to implement the
four methods of the interface. Now insert method has
already been implemented by MyContainer, so we only have three methods
left. Of the remaining three, the withdraw method has
been implemented for you.
(a) Implement the isMember and find methods. These methods must be implemented such that each one ultimately uses the private
method findIndex.
(b) Test your implementation of MySearchableContainer class by completing the menu driven program
TestMySearchableContainer.java
As explained in Unit-02, the
Association design pattern is used to map
(or associate) a set of objects with another set of objects, using the items in one set as keys
and the items in the other set as values. Some examples
are:
· Associating an instructor with his students
· Mapping student numeric grades with letter grades
·
Mapping items in a priority queue with their priorities
·
Mapping words in a file with their number of occurrence, etc.
(a) The files Student.java, Instructor.java, and TestInstructorStudentAssociation.java show how an instructor object can be
associated with a MySearchableContainer containing the students taught by the instructor. Several operations on the created
Association object are then shown. Study and then execute the program.
(b) Open the file ByWordLength.java to see another concrete example. This example reads words in a text file and associate each
word with its length. It then prints the words according to their lengths, from shortest to longest. Run the program using the sample
input file,
input.txt to see how it works.
Complete the program
TestAssociation.java to associate each word in a text file with its
number of occurrence (i.e., with its frequency), using the word as the key and
the number of occurrence as the value.