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

581325-0 Programming in Java, exam 12 December 2005/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. The class Point has been written:
    public class Point {
      private static int count=0;
      private int x=0, y=0;
      private final int me;
      public Point(int x, int y) {
        this.x = x; this.y = y;
        ++count; me=count;
      }
      public int who() {return me;}
      public void set(int x, int y) {
        this.x = x; this.y = y;
      }
      public String toString() {
        return "("+ who() + ")(" + x+"," + y + ")";
      }
    }
    
    Program a subclass of class Point, the class ColourPoint, which is as the Point, but it has an extra feature, a double-valued colour field. The ColourPoint-objects are constructed by: Each ColourPoint-object can be accessed by methods:

    Program only the needed elements to the class ColourPoint. You are not allowed to make changes in the class Point.

                                                               (17 pistettä)
    

  2. "Why on earth an ordinary programmer should know anything about abstract classes and interfaces?" This is what your friend worries after listening to your enthusiasm over the finer points with Java. Otherwise, your friend knows the Java language pretty well. He/she understands clearly the subclasses, superclasses and inheritance, but those two concepts and their usage in programming are unknown to him/her. Write an explanation including enlightening programming examples (maximum length 1 paper, ie c. 4 pages). When evaluating the answer, we will take the intended reader into consideration. This means that you shouldn't write for the evaluator, who already knows these concepts, but for that friend of yours. In your answer to this question, do not refer to your other answers, because one person evaluates only one question.
                                                                (19 points)
    

  3. Make a program that implements a language translation servive: 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 one word. But you have to take care of missing file, and also errors in creating and using the file.

    When the program has created a "dictionary" of words, then it offers the translation servive: 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.

                                                            (17 pistettä)
    
Good luck with the exam & Merry Christmas!