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

581325-0 Programming in Java, exam 10 December 2007/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 PointOfPlane has been programmed:
    public class PointOfPlane {
      private static int count=0; // counter of points constructed
      private final int me;       // the unique identity of a point
      private double x, y;        // the coordinates
      public PointOfPlane(double x, double y) {
        this.x = x; this.y = y;
        ++count; me=count;
      }
      public int who() {return me;}
      public void set(double x, double y) {
        this.x = x; this.y = y;
      }
      public String toString() {
        return "("+ me + ")(" + x+"," + y + ")";
      }
    }
    
    Program a subclass of PointOfPlane, PointOfSpace, which adds the third dimension z, to the inherited two dimensions x and y. PointOfSpace-objects are constructed by: PointOfSpace-objects are to be accessed by methods:

    Program only the necessary elements into the class PointOfSpace. You are not allowed to make any changes in the class PointOfPlane.

                                                                            (17 points)
    

  2. Explain shortly but clearly:

    1. loading a class and creating an object
    2. class method and instance method
    3. superclass and subclass
    4. polymorfism
                                                                            (16 points)
    

  3. 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 is unknown. Design and program by yourself, how the program ends.

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

                                                                            (17 points)
    

Good Luck & Merry Christmas!