University of Helsinki / Department of Computer Science / Advanced course in programming / Copyright © 2009 Arto Wikla. Advanced course in programming, exam 14th Dec 2009/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. (16 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 (18 points) 3. Write the program Dictionary that offers dictionary services: First, the program reads the word pairs word-translation from a text file that is given as command line parameter. Each word is on its own line in the text file, and each odd-numbered line presents a word in the original language. The even-numbered line following each odd line is the translation of the previous word. After learning the dictionary, the program reacts to each word the user writes as input (the word in the source language) by giving the translation of the word or announcing that there is no translation on-screen. When a user writes an empty input line, the program stops executing. You may consider single input lines as such to be 'words', but otherwise the program must be prepared for mistakes and handle exceptions. 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 (16 points) Good Luck with the Exam & Merry Christmas!