Helsingin yliopisto / Tietojenkäsittelytieteen laitos / Java-ohjelmointi / Copyright © 2006 Arto Wikla.

581325-0 Programming in Java, exam 11 December 2006/AW

Please write the name of the course, the date of the exam, and your name, personal number and signature on each paper. Write each answer on a separate paper! Contrary to standard procedure, it is allowed to bring notes to this exam, but they may not be more than one A4 sheet of paper.

  1. A germ is a simple but very individual being. Each germ has an unambiguous number given at birth, its so-called individuality, which can never change and can never be given to anyone else.

    The germ's genome is a String object. Germs reproduce only by mating. The child gets the genome of one of its parents as such. The likelihood is the same for each parent's genome. The choice of genome can be made with the expression if (Math.random()<0.5) ...

    Model the germ as the class Germ, where all the fields are naturally private, and which has the following public tools (constructor and accessor):

    Example: (this is exactly how to output: the object's order number belongs to the output, the third output can, of course, be eiter "(3): Mary" or "(3): Mark")

        Germ daddy = new Germ("Mark");
        Germ mommy = new Germ("Mary");
        Germ child = daddy.mate(mommy);
        System.out.println(daddy);  // output: (1): Mark
    
        System.out.println(mommy);  // output: (2): Mary
        System.out.println(child);  // output: (3): Mary
                                                                            (12 points)
    

  2. A DoubleGerm is the relative of a germ. Its differs from the germ only in its mating method. The child of a DoubleGerm gets a catenation of its parents' genome character strings for its genome, i.e. the character strings representing the genome are united. Implement DoubleGerm as an subclass of Germ. Reprogram only the needed parts that need reprogramming. New fields are not needed, for example. Of course you may solve this problem even, if you do not program the previous one!

    Example: (this is exactly how to output: the object's order number belongs to the output)

        DoubleGerm geezer  = new DoubleGerm("Charlie"); // number 1
        DoubleGerm biddy  = new DoubleGerm("Carrie");   // numero 2
        DoubleGerm cub = biddy.mate(geezer);
        System.out.println(cub); // output: (3): CarrieCharlie
                                                                            (12 points)
    
    

  3. "I can't stand all this talk of 'abstract data types' and 'encapsulation'!" This is what your friend cried after listening to your enthusiasm over a good programming style. Your friend does know how to program - algorithms should be created, mehtods should be called (though only the main program's 'little helpers'), even parameters are dispatched - but one of the central features of object programming only causes him the frustration of ignorance. Help your friend understand what it is all about. Write an explanation with enlightening program examples (one exam paper at the most). When your answer is evaluated, the intended reader is taken into account, so don't write this for the evaluator, who already knows this stuff, but write for that friend of yours!
                                                                            (15 points)
    
    

  4. Make a program that implements a language translation service: First the program reads a textfile, where on line 1 there is a word in the original language, its translation is on line 2, on line 3 there is a word in the original language, its translation is on line 4, etc. So, all the words of the original language are on odd numbered lines, and every translation is on the following even numbered line. If the number of lines in the file is odd, the word on the last line is not taken in account. You may assume that each line really contains exactly one word. The name of the textfile is given as a command line parameter. If some word in the original language is not unique, the latest translation is the one selected. You have to take care of missing input file, and also the errors in creating and using this file.

    When the program has created a "dictionary" of words, then it offers the translation service: When the user writes a word in the original language, the program either gives the translation or informs that the asked word was unknown. Design and program self, how the program ends.

    If you wish, you are allowed to use (but it is not obligatory!) to use an instance of the class Hashtable<K,V> as the data structure. If you do not use it, you may assume that there are not more than 10000 words in the input file. If you decide to use the Hashtable<K,V>, this part of its API might be useful:

                                                                            (14 points)
    

Good luck with the exam & Merry Christmas!