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

581325-0 Programming in Java, exam 14 December 2004/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 Tale consists of one or several String text lines. Each tale has an unambiguous number code that can never change, and no other tale can ever have the same code number. Once a tale has been created, its contents can never be changed.

    1. Implement a tale as the class Tale, where all the data structures are naturally private, and with the public tools (constructor and method):
      • public Tale(String[] lines) constructs the Tale object and its unambiguous code number
      • public int getTaleCode()
      • public int getTaleLength(), ie, the number of lines
      • public String getTaleLine(int i) returns the tale line with the line number i
      • public void printTale() prints the whole tale with numbered lines
      • public Tale concatenate(Tale other) returns a new Tale object, where the tale consists of a this-tale, to which has been catenated the other tale that is given as parameter.

    2. AnnotatedTale is like a Tale with the difference that an annotated tale can have a textual comment added to any line. There does not have to be a comment on every line. Not even necessarily to any line! Implement AnnotatedTale as a subclass to the class Tale. Reprogram only the necessary parts:
      • public AnnotatedTale(String[] lines) constructs an AnnotatedTale object, where none of the lines contains any comments.
      • public void addComment(String comment, int line) adds the comment given to the line given, any possible old comment is overwritten by the new one
      • public String getComment(int rivi) returns the comment on the i:th line of the tale (or null if there is no comment for the i:th line)
      • public void printComments() prints all non-empty comments with their line numbers, this is called the comment list
      • public AnnotatedTale concatenate(AnnotatedTale second) returns a new AnnotatedTale object, where the tale consists of this-tale to which has been catenated the other tale that is given as parameter. Of course, a new annotated tale has the comments of the old tales, as well!
    NB! Do not forget to take error situations into account. It is enough to take care of invalid line parameters! This time it is not needed to take care of the case parameter arrays are null. Describe how you take the errors into account.
                                                                (20 points)
    
  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.
                                                                (18 points)
    

  3. 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.

    Both the constructing of the files and the read operation may throw the exception Exception.

    Write interactive program WhichOneIsMoreChristmasLike, which starts by asking for the names of two input files. Then the program finds out, which one of the two input files contains more lines, where there are one or more of the following Chrismas words: "Christmas", "tree", "Santa" or "porridge"

    Finally the program prints, which one of the two input files contained more of those Chrismas words. It is of course possible that they contain the same amount of those lines, too.

    The program WhichOneIsMoreChristmasLike has to handle any Exception-exception possibly caused by constructing and reading the input files in a sensible and user-friendly way.

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