Midterm Solution

Problem I

A human memory is much like the duckMachine memory. It has the following interface:
interface HumanMemoryI {
    int MINIMUM_SIZE = 1000;
    String fetch (int address);
    void store (int address, String information);
}
A human memory must contain at least 1000 locations; the fetch and store operations are guaranteed to work correctly when invoked with these locations. We know that humans have much bigger memories. Hence, it is possible to invoke the fetch and store operations with locations greater than or equal to 1000. We also know that humans generally pretend to know more than they actually do. Hence, a human memory will never admit to failure by throwing an exception. Instead, when invoked with a location greater than or equal to 1000 or even with a location less than 0, a human memory might work correctly or it might make something up. Write a class HumanMemory that implements the interface HumanMemoryI following the guidelines in the above paragraph.

Solution

class HumanMemory implements HumanMemoryI {
    private String[] info;

    HumanMemory () { 
	info = new String[MINIMUM_SIZE];
	for (int i=0; i < MINIMUM_SIZE; i++) info[i] = "";
    }

    public String fetch (int address) {
	try { 
	    return info[address];
	}
	catch (ArrayIndexOutOfBoundsException e) {
	    return "abc";
	}
    }

    public void store (int address, String s) {
	try {
	    info[address] = s;
	}
	catch (ArrayIndexOutOfBoundsException e) {
	}
    }
}

Problem II

You are given the following two interfaces:
interface Investor {
    // Returns the amount of money that the investor has.
    double getAssets ();

    // Increases the amount of money by (getAssets() * apr / 100).
    void invest (double apr); 

    // Decreases the amount of money by (getAssets() * rate / 100).
    void paytaxes (double rate);
}

interface Charitable {
    // Returns the amount of money that the charitable person has.
    double getAssets ();

    // Decreases the amount of money by (amount).
    void donate (int amount);

    // Decreases the amount of money by (getAssets() * rate / 100).
    void paytaxes (double rate);
}
The first interface describes persons that are investors; the second interface describes persons that are charitable.

Problem III

Write the implementation of the method getMax below. The method should read tokens from its StreamTokenizer argument until it reaches the end of the stream, and then return the largest positive number it has read. If the method reads a token that is not a number, it simply discards it. (The documentation for the StreamTokenizer class is included at the end of the exam.)

Solution

    double getMax (StreamTokenizer st) throws IOException {
	double current_max = 0.0;
	
	while (st.nextToken () != st.TT_EOF) {
	    if (st.ttype == st.TT_NUMBER) {
		double newnum = st.nval;
		current_max = current_max > newnum ? current_max : newnum;
	    }
	}
	return current_max;
    }

Problem IV

Print the result of evaluating the following program:
abstract class A {}
class B extends A {}
class C extends A {}

class D extends Exception {}
class E extends D {}
class F extends D {}
class G extends Exception {}

class Simple {
    public static void main (String[] args) throws G {
        A x = new B();
        m1 (x);
    }

    static void m1 (A x) throws G {
        try {
            m2(x);
            m3(0);
            m4();
        }
        catch (D y) {
            m5(y);
        }
    }

    static void m2 (A x) {
        if (x instanceof B) System.out.println("B");
        else if (x instanceof C) System.out.println("C");
        else System.out.println("A");
    }

    static void m3 (int i) throws D {
        if (i > 0) System.out.println("Positive");
        else if (i < 0) System.out.println("Negative");
        else throw new E();
    }

    static void m4 () { System.out.println ("M4"); }

    static void m5 (D y) throws G { 
        if (y instanceof E) System.out.println("E");
        else if (y instanceof F) System.out.println("F");
        else throw new G();
    }
}

Solution

B
E

Extra Credit

In the following program, write next to each println statement either:
class A {
  int x, y;
  A () { x=1; y=2; }
  int getx () { return x; }
}

class B extends A {
  int x, z;
  B () { super(); x=3; y=4; z=5; }
  int getx () { return x; }
  int getz () { return z; }
}

class Prob1 {
  public static void main (String[] args) {
    A o1 = new A();
    A o2 = new B();
    B o3 = new B();

    System.out.println(o1.x);      // ------------------------------
    System.out.println(o1.getx()); // ------------------------------
    System.out.println(o1.y);      // ------------------------------
    System.out.println(o1.getz()); // ------------------------------

    System.out.println(o2.x);      // ------------------------------
    System.out.println(o2.getx()); // ------------------------------
    System.out.println(o2.y);      // ------------------------------
    System.out.println(o2.getz()); // ------------------------------

    System.out.println(o3.x);      // ------------------------------
    System.out.println(o3.getx()); // ------------------------------
    System.out.println(o3.y);      // ------------------------------
    System.out.println(o3.getz()); // ------------------------------
  }
}

Solution

class A {
  int x, y;
  A () { x=1; y=2; }
  int getx () { return x; }
}

class B extends A {
  int x, z;
  B () { super(); x=3; y=4; z=5; }
  int getx () { return x; }
  int getz () { return z; }
}

class Prob1 {
  public static void main (String[] args) {
    A o1 = new A();
    A o2 = new B();
    B o3 = new B();

    System.out.println(o1.x);      // ---1
    System.out.println(o1.getx()); // ---1
    System.out.println(o1.y);      // ---2
    System.out.println(o1.getz()); // ---Does not compile

    System.out.println(o2.x);      // ---1
    System.out.println(o2.getx()); // ---3
    System.out.println(o2.y);      // ---4
    System.out.println(o2.getz()); // ---Does not compile

    System.out.println(o3.x);      // ---3
    System.out.println(o3.getx()); // ---3
    System.out.println(o3.y);      // ---4
    System.out.println(o3.getz()); // ---5
  }
}


Visited times since March 23, 1999 (or the last crash).

sabry@cs.uoregon.edu