581326-3 Programming in Java, exam 24 Juni 2004/AW (Avoin yliopisto) 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. You are allowed to notes at this exam. The notes can fill one A4 sheet of paper at most. 1. 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) ... a) 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. b) 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 (18 points) 2. "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) 3. 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! a) 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. b) 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! (17 points) Good luck with the exam & Warm Summer!