[ Answers ]
Name: _______________________________
Student ID: _______________________________

Total points: 45
  1. Explain what the JDK and JRE are and contrast them. [3]

    Java Runtime Environment
    what you need to run (other people's) Java programs: the Java virtual machine (command line: java).
    Java Development Environment
    what you need to compile (make your own) Java programs: javac compiler.


  2. Explain and contrast method overloading with method overriding. [4]

    overloading
    multiple versions of the same method (same name) but with different number/types of parameters.
    overriding
    a method defined in a subclass overrides a method of the same name in the superclass.


  3. What is an abstract class? [2]

    An abstract class is not meant to be instantiated directly, but rather subclassed.

  4. What is jar? What is it used for? [2]

    The Java Archiver utility packages one or more Java files (classes, source files, packages, etc.) into a single jar file, akin to ZIP/WinZIP or Unix tar.

  5. Describe and contrast the eight Java primitive types. [8]

    bool
    boolean type, just true and false
    char
    a Unicode character
    byte
    an integer represented with 1 byte
    short
    an integer represented with 2 bytes
    int
    an integer represented with 4 bytes
    long
    an integer represented with 8 bytes
    float
    a real value represented with 4 bytes
    double
    a real value represented with 8 bytes


  6. Describe and contrast the four options we have in Java for access modifiers on attributes/methods. [8]

    private
    only methods in this class can access
    (no modifier)
    methods in this class and this package can access
    protected
    also subclasses can access
    public
    anyone can access


  7. Tell me everything you know about Java packages. [5]

    Packages are a way to organize related Java classes. By default, classes in the same directory are considered part of the same package. In addition, you may specify that a class belongs to a certain package by using a package declaration at the top of the file. All entities (variables, methods) that are not marked protected or private are visible to other classes in the same package. Upon compilation of a Java package, the Java compiler (when run with the -d switch) creates subdirectories for the package; dots in the package name are translated into slashes. E.g., the package ca.twu.cmpt167 becomes the subdirectory ca/twu/cmpt167.

  8. Define simple Java classes and sketch a UML class diagram that formalizes the following set of statements: [8]

    class Date { int year, month, day; }
    class Person { Date birthDate; String name; }
    class Student extends Person { float GPA; }
    class Employee extends Person { }
    


  9. Recall that the n-th Fibonacci number is defined as fib(n) = fib(n-2) + fib(n-1). We define fib(0) = fib(1) = 1. The traditional recursive solution is highly inefficient, as it recalculates fib(0) many times. Write a complete Java class to calculate Fibonacci numbers, using hinting (caching) -- record previously-calculated Fibonacci numbers in an array. Docstrings and comments are not necessary but may garner partial credit. [10]

    See the full Java source code.
    public class Fibonacci {
    	long[] cache;			// pre-calculated Fibs
    	final int DEFAULT_SIZE = 100;	// default arg to constructor
    
    	private void clearCache() {
    		cache[0] = cache[1] = 1;
    		for (int i = 2; i lt; cache.length; i++) {
    			cache[i] = 0;
    		}
    	}
    
    	Fibonacci( int size) {
    		if (size lt; 2) size = 2;
    		cache = new long[size];  
    		clearCache();
    	}
    
    	long fib( int n ) {
    		if (n > cache.length) return 0;
    		if (cache[n] != 0) return cache[n];
    		cache[n] = fib(n-2) + fib(n-1);
    		return cache[n];
    	}
    
    	public static void main( String args[] ) {
    		Fibonacci f = new Fibonacci(51);
    		System.out.println("The 50th Fibonacci number is " + f.fib(50));
    	}
    }