%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Assembly Language (20 points) Assume that the values of the variables v, w, x, y, and z are stored in memory locations 200, 201, 202, 203, and 204, respectively, and that all locations greater than 204 are free. Translate the following operations into their assembly language equivalents using the machine instructions on page ... 200 v 201 w 202 x 203 y 204 z * Set v to the value of (x-y)+z. load 202 %% load x subtract 203 %% subtract y add 204 %% add z store 200 %% store in v * Set v to the value of (w+x) - (y+z). load 203 %% load y add 204 %% add z store 205 %% store in temp load 201 %% load w add 202 %% add x substract 205 %% subtract temp store 200 %% store in v * If (v >= (w+2)) then set x to the value of y else set x to the value z. load 201 %% load w store 205 %% store w in temp inc 205 inc 205 %% temp = w+2 load 200 %% load v compare 205 %% compare w jumpgt LABEL load 203 store 202 %% x = y jump DONE LABEL: load 204 store 202 %% x = z DONE: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Streams (15 points) 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.) 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; } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Design Patterns (20 points) Write class definitions that reflect the following relationships: * A Square is a Shape. * A Circle is a Shape. * An AreaOp is a ShapeOp. * A PerimOp is a ShapeOp. Every Shape object should have a method accept that takes a ShapeOp argument. Every ShapeOp object should have two methods: one that works on Square objects and one that works on Circle objects. Your code should be written such that the following program works as specified: class TestVisitor { public static void main (String[] args) { Shape s = new Square (3.2); Shape c = new Circle (2.9); ShapeOp perim = new PerimOp(); ShapeOp area = new AreaOp(); System.out.println(s.accept(area)); // prints 10.240000000000002 System.out.println(s.accept(perim)); // prints 12.8 System.out.println(c.accept(area)); // prints 26.420794216690158 System.out.println(c.accept(perim)); // prints 18.2212373908208 } } abstract class Shape { abstract double accept (ShapeOp op); } class Square extends Shape { double s; Square (double s) { this.s = s; } double accept (ShapeOp op) { return op.square(this); } } class Circle extends Shape { double r; Circle (double r) { this.r = r; } double accept (ShapeOp op) { return op.circle(this); } } abstract class ShapeOp { abstract double square (Square s); abstract double circle (Circle r); } class AreaOp extends ShapeOp { double square (Square s) { return s.s*s.s; } double circle (Circle r) { return Math.PI*r.r*r.r; } } class PerimOp extends ShapeOp { double square (Square s) { return s.s*4; } double circle (Circle r) { return 2*Math.PI*r.r; } } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Program Execution (15 points) Given the following declarations: interface Colorable { void setColor (byte r, byte g, byte b); } class Point { int x, y; } class ColoredPoint extends Point implements Colorable { byte r, g, b; public void setColor (byte rv, byte gv, byte bv) { r = rv; g = gv; b = bv; } } Find all compilation errors in the following code: Point p = new ColoredPoint(); // OK Colorable c = null; // OK c = p; // Point does not implement Colorable p.setColor((byte)0,(byte)0,(byte)0); // Point does not have a setColor ColoredPoint cp = new ColoredPoint(); // OK p = cp; // OK c = cp; // OK c = new Colorable(); // Cannot instantiate an interface cp = p; // Cannot cast Point to ColoredPoint %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Polymorphism (15 points) You are given the following class hierarchy: abstract class Employee { abstract double getSalary (); } abstract class Faculty extends Employee {} class Professor extends Faculty { double getSalary () { return 5.3; } } class Instructor extends Faculty { double getSalary () { return 4.1; } } abstract class Staff extends Employee {} class TempStaff extends Staff { double getSalary () { return 2.6; } } class PermanentStaff extends Staff { double getSalary () { return 3.4; } } Write a method that takes an array of employees, and returns the average of their salaries: double averageSalary (Employee[] es) { double total = 0.0; for (int i=0; i