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

581325-0 Programming in Java, exam 17 December 2003/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 the exam, but they may not be more than one A4 sheet of paper.

  1. Please explain the following concepts briefly but exactly (the whole answer must fit on one sheet of paper):

    1. recursion
    2. abstract class
    3. interface
    4. polymorphism
                                                               (12 points)
    

  2. The Germ is a very simple but very individual creature. Each germ has a unique order number given at birth, the so-called individuality, which can never change, and neither can anyone else ever get the same individuality.

    The germ's genotype consists of an int array with five elements. The germs procreate only by coupling. The genotype of a child is determined by the genotype of its parents according to the following formula: childGenotype[i] = (fatherGenotype[i] + motherGenotype[i])%17, when i = 0, 1, 2, 3, 4.

    1. Model the germ as the class Germ, where all the fields are naturally private and with public tools (constructor and accessors):
      • public Germ(int[] genotype), if the array has wrong size, the genotype will be an array with all elements set to 13
      • public int[] getGenotype()
      • public Germ mate(Germ anOther)
      • public String toString(), the individuality and the genotype must be shown

    2. The FluGerm is a special germ. In addition to a germ's features, it has the features of name of the flu (a String value) and infectiousness (a double value). When two FluGerm objects couple, their offspring gets as its name a catenation of the parents' names (i.e. the character strings added to each other). The infectiousness of the child is the average of the infectiousness of the parents multiplied by 17. Implement the FluGerm as a subclass of the Germ. Design reasonable constructors and accessors for the class.
                                                                (15 points)
    

  3. [This question differs a little from the Finnish version, because of different books.] "Why should an ordinary programmer know something about classes, static and non-static variables and methods?", your friend cries when listening to your stories from the course Programming in Java. Your friend has completed the course Introduction to Programming, and has even written the word static into his program many times, though without understanding why. Help your friend understand what it all means and why he should understand it. Write an explanation (maximum length 1 sheet of paper). Keep in mind that examples are an aid to learning. When your answer is graded, the intended reader will be taken into consideration. This means that you should not write for the person who grades the paper, who already knows the subject, but to that friend of yours. In your answer, do not refer to other answers in this exam, because all answers are checked by separate people.
                                                               (14 points)
    
    

  4. You have the class Inputfile at your disposal for reading text files. The class has the constructor Inputfile(String name). The file name is given as parameter. The following accessor is used to read the lines:
      public String readLine()
    
    The end of a file is indicated with a null value.

    You also have the class Outputfile at your disposal to write text files. The class has the constructor Outputfile(String name). The file name is given as parameter. The following accessor is used to write the lines:

      public void writeLine(String line)
    

    Both the creation of both files and the reading and writing operations may cause the exception Exception.

    Write interactive program CopyChristmasLines, which starts by asking for the names of two files, the input file and the output file. Then the program makes a copy of the input file. The copy only contains copies of the input file lines where one or more of the words "Christmas", "tree", "Santa" or "porridge" appear. The other file name that was asked for, the output file, is the name of the copied file.

    The program CopyChristmasLines has to handle any Exception exceptions possibly caused by creating and using the file in a sensible and user-friendly way.

                                                         (12 points)
    
Good luck with the exam & Merry Christmas!