University of Helsinki / Department of Computer Science / Advanced
course in programming / Copyright © 2008 Arto Wikla.

Advanced course in programming, exam 8th December 2008/AW

Please write the name of the course, the date of the exam, your name,
student number and your signature at the top of each paper. Write each
answer on a separate paper! No additional  material is allowed during this
exam.

1. You have the class InfoSearcher at your disposal. It can search
  for information in one way or another, from somewhere, maybe the web,
  maybe a database, who knows. You don't. And you don't know how it is
  done. You only know the 'API' specification of the class:

  * public InfoSearcher(String secretCode) constructs the InfoSearcher object.
    A secret code that somehow - you don't know how - specifies the limits of
    the information search is given as parameter. Observe that the class 
    InfoSearcher only has this single constructor.

  * public String whatIs(String who) returns the value of some info that is
    attached to the parameter.

  * public boolean deleteInformation(String who) deletes the information
    related to the character string given as parameter.

  * public String toString() returns a String representation of the 
    InfoSearcher object as its value. Before trying it you don't know what it
    is like, but you can trust that the representation is useful.

  Program a sub-class TeleIDdata for this class with the 'API':

  * public TeleIDdata(String operator, String secretCode) constructs an
    TeleIDdata object with the given code.

  * public TeleIDdata(String operator) constructs the object TeleIDdata with
    the code "007".

  * public String operator() returns the name of the operator as its value.

  * public String whatIs(String who) returns the tele-id data of the parameter
    as String as its value. Here, "tele-id" data means the String we get if
    we remove all letter characters from the String returned by the whatIs
    method of the InfoSearch class. To identify letters, you can use the
    boolean class method Character.isLetter(char ch) in the Character class.
    It is true when the parameter is a letter, false otherwise.

  * public boolean deleteInfo(String who) deletes the information related to
    the character string given as parameter.

  * public String toString() returns a String representation of the TeleIDdata
    object as its value. The representation consists of the operator's name
    followed by the String given by the InfoSearch class.

  Do not program anything but the necessary parts. Illustrate the use of the
  class you have programmed with a small main program.
                                                                    (16 points)

2. Explain the following concepts in Java programming briefly and precisely.
   Do not refer to other questions in your answer, because each question is
   checked by different people! The maximum length of the answer is one 
   paper, i.e. 4 pages.

         1. superclass and subclass
         2. abstract class
         3. interface
         4. polymorfism
                                                                   (16 points)


3. Create the program CountWords with which you can analyse the number of words
  in a text file. First, the program asks for the name of the file. Then it
  asks for the words to be analysed, i.e. which words to count. The 'words'
  may be any non-empty Strings. The primary job of the program is to compute
  and output the number of given words in the given file.

  The program must be prepared to handle errors and exceptions. The error
  messages must be illustrative.

  Hints:

  * You may, and it is useful (but you are not obliged!) to employ the 
    HashMap class. If you do not use it, you may assume that there are no
    more than 50 of the words.

  * If you employ the HashMap class, you may have use of the following
    partial API description:
                
    o public HashMap() creates the empty Hashtable object, which maps
      a K-type object as a V-type object.

    o public V put(K key, V value) attaches the association key-->value to the
      HashMap object; if another association is already attached to the key,
      the method replaces it with the new one and returns the old meaning as
      its value; if no key is found, the method returns the value null.

    o public V get(Object key) returns the value attached to the key as its
      value; if no key is found, the method returns the value null

    o public boolean containsKey(Object key) true if key is among the keys,
      otherwise false

    o public V remove(Object key) deletes the key and its value, i.e. the
      association key-->value is deleted, it returns the value the deleted
      value as its value; it returns null if the key is not found

    o public String toString() gives a string representation of the HashMap
      object, the components are modified with their own toString
      methods.

  * HashMap, which maps Strings to integers, is of the type 
    HashMap

  * When writing out the result, you can confine yourself to the String 
    representation that HashMap's own toString produces, see example
    below. If you do not use HashMap, any clear printing will do.

  * Example of using the program (the user's text is in italics):

     Which file should be analysed? (an empty String interrupts the program)
     flora.txt
     Cannot find the file floora.txt!
     Which file should be analysed? (an empty String interrupts the program)
     fauna.txt
     Give word to be analysed (an empty String ends the input)
     cat
     Give word to be analysed (an empty String ends the input)
     mouse
     Give word to be analysed (an empty String ends the input)
     dog
     Give word to be analysed (an empty String ends the input)

     Analysis of file fauna.txt:
     {mouse=124, cat=35, dog=235}

                                                                   (18 points)



          Good luck with the exam and Merry Christmas!