Assignment 2, due Aug 28

Solutions

Part of the homework for CS:2820, Fall 2015
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Background: Remember this little function from homework 1?
    function f( i )
       if i < 3
          return i
       else
          return f( i - 1 ) + f( i - 2 ) + f( i - 3 )
    

    A Question: Write a Java program that computes and outputs all successive members of this sequence up to the point that the values exceed 1000, one number per line. The sequence begins 0, 1, 2, 3, 6, 11, 20, and here is the modified hello world code from the lecture on Aug 27:

    public class HelloWorld {
        public static void main(String[] args) { // it starts here
            int i = 0;
            int j = 1;
            int k = 0;
            System.out.println("Hello, World");
            while (true) {
                System.out.println(""+i);
                k = i + j;
                i = j;
                j = k;
            }
        }
    }
    

    The above code contains leftovers from the trivial hello-world program (for example, the class name itself and the first line of output. A more appropriate class name might be something like sequence?

    Easily legible handwritten code is acceptable, but your code will be graded on cosmetic issues such as clean indenting and spacing, sensible variable names, lack of inappropriate comments and presence of appropriate comments. This is a trivial program, do not over document it, but please, do claim authorship.

    You need not run your code, but of course, if you run it, you will gain some assurance of its correctness. (2.0 points)

    // Sequence.java
    /**
     * Code to compute the sequence 0, 1, 2, 3, 6, 11, 20 ...
     * @author Douglas Jones
     */
    public class Sequence {
        public static void main(String[] args) {
            int h = 0;   // current member of sequence
            int i = 1;   // successor of h
            int j = 2;   // succsssor of i
            int k;       // temporary
            while (true) {
                System.out.println("" + h);
                if (h > 1000) break;   // loop exit
                k = h + i + j;         // compute next member of sequence
                h = i;
                i = j;
                j = k;
            }
        }
    }
    

    A very narrow reading of "up to the point that the values exceed 1000" suggests that the first value exceeding 1000 should be printed. The above code does that, but the presence of a typo in the assignment makes this less than obvious so terminating with the highest value less than 1000 shouldn't be cause for trouble.

  2. A short question: What is the difference between a static method and a static field of a class. (0.5 points)

    A "smart alek" answer would be that one is a method and the other is a field of a class, but this conveys nothing interesting.

    A more interesting observation is that static fields are variables with a global lifetime -- they are allocated with the program is started and deallocated when the program terminates. All methods have similar lifetime (our programs don't generally contain dynamically created code), so discussion of lifetime doesn't pertain to static methods.

    The fact that a method is declared static determines what variables it can reference. Specifically, it prevents it from referencing the fields of an object of the current class. It can only reference static variables in the class. That means, in effect, that declaring a method static takes it outside the central concepts of object-oriented programming. Static variables are not subject to similar restrictions.

  3. A short question: What is the difference between /**comments**/ and /*comments*/? (0.5 points)

    From the point of view of the java compiler, nothing, but the javadoc program extracts the contents of double-star comments and uses them to construct documentation files.