INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM

ICS202 : DATA STRUCTURES

LAB  #02 : More on Design Patterns


Objectives:

To gain experience with:

          The SearchableContainer Pattern

         The Association Pattern


 

1.  Downloads and Review:

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.

 

   

2.  Task #1

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

   

3.  Task #2

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.

4.  Task #3

 

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. Use an instance of MySearchableContainer to store the Association objects.  After processing all the words, print your container using the PrintingVisitor of the ics202 package.

 

5.  Task #4

      (a) Write a visitor DuplicatedElementsFinder whose instance returns a MySearchableContainer set of the duplicated elements, of a
            MyContainer object it visits,
each of which has a frequency greater than three. An empty MySearchableContainer should be
            returned if there are no such duplicated elements.

            Example: If the visited container contains the objects {5, 5, 5, 20, 8, 1, 8, 20, 8, 3, 20, 8, 8,  20} then the returned container is
            {8, 20}

            Hint: Use the Association design pattern and two MySearchableContainer objects in your visit method.

     (b) Write an appropriate test class to test the DuplicatedElementsFinder visitor.