Helsingin yliopisto / Tietojenkäsittelytieteen laitos / 581258-1 Johdatus ohjelmointiin
Copyright © 1999 Arto Wikla. The use of this material is only allowed for studying purposes of private persons. Any other use of this material, such as commercial or other courses, is forbidden.


581258-1 Introduction to Programming: 2. exam 3.5.1999/AW

This is an English version of the examination, not a translation of Finnish questions. One of the questions is different.

Write on top of every answering paper the name of the course, the date of the examination, your name, your date of birth and your signature.

You are allowed to use the tools of the class Read (in Finnish Lue) when writing your programs.

  1. (This question is different from Finnish version)
    Explain shortly and precisely what is the difference between static and non-static variables and methods.
                                                                  (6 points)
    
    
  2. Write the method int toInt(String string), whose return value is the value of the integer provided as a string parameter. It is an error if the provided string contains anything else but numerical characters; in this case, the method returns -1.

    There exist tools in Java for translating strings to numerical values, but this exercise should be solved on your own: move the string in to a char[] array, inspect the validity of the array there, then calculate its numeric value.

    Hints:

                                                                  (6 points)
    
    
  3. One of your friends, taking part to this course, has not totally understood the idea of what does it mean when a method is "overloaded", "overrided" and "inherited". He/she hasn't even got the idea, why he/she should understand these concepts. Help your friend and write him/her an explanation of the matter. Remember that good examples are important in learning. Your explanation will be evaluated from the point of view of your friend. So do not write to your teacher, he/she already knows it!
  4.                                                               (6 points)
    
    
  5. You are given a class InpFile for reading input files. The class has a constructor InpFile(String name). The parameter is the name of the file.

    The lines are read by method

       public String read()
    

    When the file ends, the method returns value null.

    The class takes care of all the possible exeptions. When an error occurs, the class gives an error message and exits the program.

    By using this class, write a program AreTheFilesSimiliar for comparing two files. The names of the files are given to the program as command-line arguments. The program compares the two files line by line, and prints all different lines to the screen. An example of the output:

    1>cat is walking<
    2>rat is walking<
    1>1235123<
    2>1234123<
    1>abc<
    2>abc  <
    2>  the second<
    2>was  <
    2>longer!<
    
    Thus the lines differing from each other are printed between either characters "1>" and "<" or "2>" and "<", depending on where the line in question comes from: from the first file or from the second file. If either file ends before the other, the extra lines of the longer file are printed. The program does not print anything else.

    In the example above the files had three different lines and the second file seems to have been three lines longer than the first.

    The program doesn't have to take care of the exceptions, the class InpFile takes care of them.

                                                                  (7 points)
    
    










Happy Summer!