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; }
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:
interface HouseVisitor { void visitBedRoom (BedRoom r); void visitLivingRoom (LivingRoom r); void visitBathRoom(BathRoom r); }
class House { private Room[] rooms; House (Room[] rooms) { this.rooms = rooms; } void accept (HouseVisitor houseVisitor) { for (int i=0; i < rooms.length; i++) { rooms[i].accept(houseVisitor); } } }Write a method that takes a house argument and counts the number of bedrooms in that house. Note that you cannot access the private instance variable rooms in the house. You must instead use a visitor to count the bedrooms:
int countBedRooms (House house) { BedRoomCounter brc = new BedRoomCounter(); house.accept(brc); return brc.count; } class BedRoomCounter implements HouseVisitor { int count = 0; public void visitBedRoom (BedRoom r) { count++; } public void visitLivingRoom (LivingRoom r) {} public void visitBathRoom (BathRoom r) {} }
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 3The 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:
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
int remainder (int a, int b) { if (a >= b) return remainder(a-b, b); else return a; }
sabry@cs.uoregon.edu