University of Helsinki/Department of Computer Science/Advanced course in programming/© 2010 Arto Wikla. Advanced course in programming, exam 13th Dec 2010/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 source material is allowed during this exam. 1. You have access to the class IntegerSet with the flowing "API": * public IntegerSet(int capacity) creates the empty object IntegerSet that is, by default, prepared for the set size given as parameter * public boolean add(int element) adds an element if it was not already there; it returns true if it was really added, false if the element was already in the set * public boolean remove(int element) removes an element from the set if it happened to be there; it returns true if it was really removed, false if the element was not in the set in the first place * public boolean belongs(int element) returns true if the element belongs to the set and false if the element does not belong to the set * public int size() size of the set (i.e. number of elements in the set) * public int[] toIntArray() returns an array of integers with exactly all the elements of the set * the method public String toString() produces a String representation of the set, like {-3, 1, 76} We do not know anything else about the structure or implementation of the class IntegerSet! Write a subclass IntegerSetPlus for the class IntegerSet, with the following "API": * public IntegerSetPlus() creates the empty object IntegerSetPlus that is prepared for 100-element sets by default * public IntegerSetPlus(int capacity) creates the empty object IntegerSetPlus that is prepared for set size given as parameter by default * public IntegerSetPlus compound(IntegerSetPlus b) returns as its value the set that contains all the elements of the 'this' set and set b * public IntegerSetPlus section(IntegerSetPlus b) returns as its value the set that contains exactly all the elements that belong both to the 'this' set and set b * public IntegerSetPlus extraction(IntegerSetPlus b) returns as its value a set that contains all the elements of the 'this' set that do not belong to set b Illustrate the use of all the operations of the class IntegerSetPlus, including inherited ones, with a separate main program. (12 points) 2. Give brief and concise explanations of the following concepts connected with Java programming. Do not refer to other exam questions in your answers, since all the sections are graded by different people! The maximum allowed length of your answer is one paper, i.e. 4 pages. 1. superclass and subclass 2. abstract class and interface 3. polymorphism (12 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. Use the class HashMap in your solution: * public HashMap() creates the empty object HashMap that maps objects of type K to objects of type V * public V put(K key, V value) adds to the object HashMap the association key --> value; if the key already had an association, the method replaces it with the new one and returns as its value the old meaning; if there was no key, the method returns the value null * public V get(Object key) returns the value assiciated to the key; if the key doesn't exist, the method returns null * public Boolean containsKey(Object key) true if key is among the keys, otherwise false * public V remove(Object key) removes the key key and its value, in other words, the association key --> value is removed; returns the removed value; returns null if no key is found * public String toString() creates a string presentation of the object HashMap, the components are modified with their own toString() methods Hints: * 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. * Example of using the program: 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} (12 points) Good Luck with the Exam & Merry Christmas!