Solution for Quiz II

Problem I

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 which 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 < es.length; i++) 
    total += es[i].getSalary();
    return total / es.length;
}


Problem II

You are given the following class hierarchy:
abstract class Room {
    abstract void accept (HouseVisitor houseVisitor);
}

class BedRoom extends Room {
    void accept (HouseVisitor houseVisitor) {
        houseVisitor.visitBedRoom(this);
    }
}

class LivingRoom extends Room {
    void accept (HouseVisitor houseVisitor) {
        houseVisitor.visitLivingRoom(this);
    }
}

class BathRoom extends Room {
    void accept (HouseVisitor houseVisitor) {
        houseVisitor.visitBathRoom(this);
    }
}
Answer the following questions:


Problem III

Consider the following assembly program:
0       load 51
1       compare 50
2       jumplt 7
3       load 50
4       subtract 51
5       store 50
6       jump 0
7       out 50
8       halt
50      5
51      3       
The right column gives either instructions or data that is stored in the memory location in the left column. For example, the instruction jump 0 is stored at memory location 6, and the number 5 is stored at memory location 50. Assuming the program counter is initialized with the address of the first instruction (zero) and that the flags of the arithmetic and logic unit are initialized to the value false, trace the execution of the program by showing: after each instruction is executed. After the last instruction write the value that the program prints. Running the duckMachine with the appropriate visitor, we get:
LOAD 51
        PC=1    ACC=3   GT/EQ/LT=false/false/false
        M[50]=5         M[51]=3

COMPARE 50
        PC=2    ACC=3   GT/EQ/LT=true/false/false
        M[50]=5         M[51]=3

JUMPLT 7
        PC=3    ACC=3   GT/EQ/LT=true/false/false
        M[50]=5         M[51]=3

LOAD 50
        PC=4    ACC=5   GT/EQ/LT=true/false/false
        M[50]=5         M[51]=3

SUBTRACT 51
        PC=5    ACC=2   GT/EQ/LT=true/false/false
        M[50]=5         M[51]=3

STORE 50
        PC=6    ACC=2   GT/EQ/LT=true/false/false
        M[50]=2         M[51]=3

JUMP 0
        PC=0    ACC=2   GT/EQ/LT=true/false/false
        M[50]=2         M[51]=3

LOAD 51
        PC=1    ACC=3   GT/EQ/LT=true/false/false
        M[50]=2         M[51]=3

COMPARE 50
        PC=2    ACC=3   GT/EQ/LT=false/false/true
        M[50]=2         M[51]=3

JUMPLT 7
        PC=7    ACC=3   GT/EQ/LT=false/false/true
        M[50]=2         M[51]=3

OUT 50
        PC=8    ACC=3   GT/EQ/LT=false/false/true
        M[50]=2         M[51]=3
HALT


Extra Credit

The assembly program is an iterative loop for the following Java program:
    int remainder (int a, int b) {
        if (a >= b) return remainder(a-b, b);
        else return a;
    }


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

sabry@cs.uoregon.edu