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

581325-0 Programming in Java, exam 18 December 2001/AW

Write the name of the course, the date of the exam, your name, personal number and your signature at the top of each paper. Write each answer on a separate paper!

  1. "I can't stand all this talk of 'abstract data types' and 'encapsulation'!" This is what your friend cried after listening to your enthusiasm over a good programming style. Your friend does know how to program - algorithms should be created, mehtods should be called (though only the main program's 'little helpers'), even parameters are dispatched - but one of the central features of object programming only causes him the frustration of ignorance. Help your friend understand what it is all about. Write an explanation with enlightening program examples (one exam paper at the most). When your answer is evaluated, the intended reader is taken into account, so don't write this for the evaluator, who already knows this stuff, but write for that friend of yours!
                                                                (15 points)
    

  2. A germ is a simple but very individual being. Each germ has an unambiguous number given at birth, its so-called individuality, which can never change and can never be given to anyone else.

    The germ's genome is a String object. Germs reproduce only by mating. The child gets the genome of one of its parents as such. The likelihood is the same for each parent's genome. The choice of genome can be made with the expression if (Math.random()<0.5) ...

    1. Model the germ as the class Germ, where all the fields are naturally private, and which has the following public tools (constructor and accessor):
      • public Germ(String genome)
      • public String whatIsGenome()
      • public Germ mate(other Germ)
      • public String toString(), the output takes the form (individuality): genome, see example.

    2. A DoubleGerm is the relative of a germ. Its differs from the germ only in its mating method. The child of a DoubleGerm gets a catenation of its parents' genome character strings for its genome, i.e. the character strings representing the genome are united. Implement DoubleGerm as an subclass of Germ. Reprogram only the needed parts that need reprogramming. New fields are not needed, for example.

    Example: (this is exactly how to output: the object's order number belongs to the output, the third output can, of course, be eiter "(3): Mary" tai "(3): Mark")

        Germ daddy = new Germ("Mark");
        Germ mommy = new Germ("Mary");
        Germ child = daddy.mate(mommy);
        System.out.println(daddy);  // output: (1): Mark
        System.out.println(mommy);  // output: (2): Mary
        System.out.println(child);  // output: (3): Mary
    
        DoubleGerm geezer  = new DoubleGerm("Charlie"); // number 4
        DoubleGerm biddy  = new DoubleGerm("Carrie");   // numero 5
        DoubleGerm cub = biddy.mate(geezer);
        System.out.println(cub); // output: (6): CarrieCharlie
                                                                (14 points)
    

  3. The heading of a certain class constructor is: public Creature(Innards parameter). .
    How do you create an instance of the class Creature, when Innards is
      a)  public abstract class Innards {
            public void doThisandThat();
          }
      b)  public interface Innards {
            public void doThisandThat();
          }
                                                                (10 points)
    

  4. You have the class Inputfile. The class has the constructor Inputfile(String name). The file name is given as parameter. The rows are read with the instance method public String readRow(). When the file ends, the method gives the value null. Exceptions are not handled in this class, so the constructor and accessor caller must handle deviations itself!

    • Implement the class Integerfilter with the help of the class Inputfile. The class Integerfilter has the constructor Integerfilter(String fileName).

      With the help of an Integerfilter object, the integers in a text file can be read so that all other signs are ignored, they are just passed by. (Any unbroken character string is seen as an integer, all the signs ignored.)

      The numbers in the file are read with the accessor public int readNextNumber(), that returns the following integer in the file. When there are no more integers in the file, the method returns -1.

      Exceptions are handled in the class Integerfilter so that the user of its instances is not bothered with them; if the creation of the object Inputfile fails, the method readNextNumber() returns -1 from the start. If the execution of the method readRow() leads to an exception, the method readNextNumber() returns -1. For a user of the object Integerfilter, a broken or missing file looks like file with no integers. A file that is broken in the process just looks like it ends.

      NB! The Integerfilter only has to handle the exceptions caused by Inputfile on the most general level, as an Exception.

      NB! If you know how, use the method Integer.parseInt(String m).

    • For the testing of the method Integerfilter, create a program that asks the user for a file name and computes the average of integers in the file. If the file is
          asdf10 asd4
          ö2xcv?+das-9asd,
      for example, the program outputs the average 6.25.

    NB! You can use the class Inputfile, for heaven's sake, do not start programming it now!

                                                                (14 points)
    
Good Luck with the Exam & Merry Christmas!