University of Helsinki / Department of Computer Science / Programming in Java / Copyright © 2002 Arto Wikla

581325-0 Programming in Java, exam 18 Dec 2002/AW

Write the name of the course, the date of the exam, and your own name, personal number and signature at the top of each paper. Write each answer on a separate paper!
As an exception, you are allowed to notes at this exam. The notes can fill one A4 sheet of paper at most.

  1. “I am so fed up with all this talk about ‘abstract classes’ and ‘interfaces’!” This is what your friend cries out after listening to your enthusiasm over the finer points with Java. Otherwise, your friend knows the Java language pretty well – he even knows classes and class hierarchies – but those tools only make him annoyed in his ignorance. Help your friend understand what it is all about, and what those tools can be used for. 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 this, but for that friend of yours.
                                                            (17 points)
  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 Tarina(String[] lines) creates the Tale object and its unambiguous code number
      • public int whatIsTaleCode()
      • public int whatIsTaleLength(), ie, the number of lines
      • public String whatIsTaleLine(int i) returns the tale line with the line number i
      • public void outputTale() outputs the whole tale with numbered lines
      • public Tale add(Tale second) returns another Tale object, where the tale consists of a this tale, to which has been added the other tale given as parameter.
    2. Annotated Tale 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 Annotated Tale as a subclass to the class Tale. Reprogram only the necessary parts:
      • public AnnotatedTale(String[] lines) creates the object AnnotatedTale, 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 whatIsComment(int rivi) returns the comment on the i:th line of the tale (or null if there is no comment for the line)
      • public void outputComments() outputs all non-empty comments with their line numbers, this is called the comment list
      • public AnnotatedTale add(AnnotatedTale second) returns a new AnnotatedTale object where the tale consists of the this tale with the other tale given as parameter added on. Of course, a new annotated tale is given the comments of old tales, as well!

NB! Do not forget to take error situations into account.

                                                            (18 points)
  1. You have at your disposal the class CharacterFile to read files consisting of char values. The class has the constructor CharacterFile(String filename). The name of the input file is given as parameter. The characters can be read with the instance method public char readCharacter(). The method announces the end of a file by returning a "null character" ie the character constant '\u0000'. Thus, when the character returned by the method is equal to the constant '\u0000', the file has ended. The method readCharacter announces the end of a line by returning the character '\n'.

The class CharacterFile does not catch exceptions! Thus, the caller of the constructor and the method has to catch the exceptions theirself!

    1. Implement the class LineFile with the help of the class CharacterFile. The class LineFile has the constructor LineFile(String filename)

With the help of the LineFile object, we can read the file line by line. Each line is a String object without the line end character ‘\n’ that we have in the characterfile.

The lines in a LineFile are read with the method public String readLine(), which returns the following line in the file.

When the file ends, readLine returns null.

Errors must be caught in the class LineFile so that the user of its instances does not have to worry about them. If you fail to create the CharacterFile object, the readLine() method returns null. If the call of the readCharacter() method throws and exception, the readLine() method returns null For users of the Linefile object, a broken or missing file will look like a file with no lines. A file that breaks suddenly just looks like it ends.

NB! It is enough if LineFile catches the exceptions thrown by CharacterFile on the most general level, as Java’s Exceptions.

    1. Program the LineFile with the help of an application that counts the number of file lines given by the user.

NB! The class CharacterFile already exists, so, in Heaven’s name, do not start programming that!

                                                            (18 points)
 

Good luck with the exam & Merry Yule!