Rice University Comp 212 - Intermediate Programming

Spring 2007

Lecture #20: Discussion of Solutions to Exam 1


Today's Menu

Question 1:

Question 2

Question 3:

Question 4:

package company;
import company.listFW.*;
public class Employee {
    private static int _ID = 100;
    private static int _Count = 0;
    
    private int _empID;
    private String _name;
    private AState _state;
    
    public final static Employee CompanyOwner = new Employee();
    private Employee() {
        this("Owner");
        _state = Owner.Singleton;
    }
    
    public Employee(String name) {
        _ID++;
        _Count++;
        _name = name;
        _empID = _ID;
        _state = new Worker();
    }
    
    public String getName() {
        return _name;
    }
    
    public int getID() {
        return _empID;
    }
    
    public static int getTotal() {
        return _Count;
    }
    
    public void promote(Employee newBoss, IList newTeam) {
        _state.promote(this, newBoss, newTeam);
    }
    
    public IList getAllWorkers(IListFactory f) {
        return _state.getAllWorkers(this, f);
    }
    
    protected void finalize() {
        _Count--;
    }
    
    
    void setState(AState s) {
        this._state = s;
    }
    
    AState getState() {
        return(_state);
    }
    
// Methods needed for the system to be operational:
//    public IList getSubordinates() {
//        return _state.getSubordinates(this);
//    }
//    public Employee getBoss() {
//        return _state.getBoss(this);
//    }
//    
//    public void setBoss(Employee boss) {
//        _state.setBoss(this, boss);
//    }
//    
//    
//    public IList addSubordinate(Employee sub) {
//        return _state.addSubordinate(this, sub);
//    }
//    
}
package company;
import company.listFW.*;
abstract class AState {
    abstract void promote(Employee emp, Employee newBoss, IList newTeam);    
    abstract IList getAllWorkers(Employee emp, IListFactory f);
    
// Methods needed for the system to be operational:
//    abstract Employee getBoss(Employee emp);    
//    abstract void setBoss(Employee emp, Employee boss);   
//    abstract IList getSubordinates(Employee emp);    
//    abstract IList addSubordinate(Employee emp, Employee sub);
}
package company;
import company.listFW.factory.*;
import company.listFW.*;
import company.listFW.visitor.*;
/**
 * There is only one owner: Singleton pattern.
 */
class Owner extends AState {
    
    private static IList _subordinates = CompositeListFactory.Singleton.makeEmptyList();
    final static Owner Singleton = new Owner();   
    private Owner() {
    }
    
    void promote(Employee emp, Employee newBoss, IList newTeam) {
        throw new IllegalArgumentException("Cannot promote owner!");
    }
    
    IList getAllWorkers(Employee emp, IListFactory f) {
        return (IList)_subordinates.execute(GetIndians.Singleton, f);
    }
    
// Methods needed for the system to be operational:
//    Employee getBoss(Employee emp) {
//        // to do: code goes here.
//        return (null);
//    }
//    
//
//    void setBoss(Employee emp, Employee boss) {
//    }
//    
//
//    IList getSubordinates(Employee emp) {
//        // to do: code goes here.
//        return (null);
//    }
//    
//    IList addSubordinate(Employee emp, Employee sub) {
//        // to do: code goes here.
//        return (null);
//    }
}
package company;
import company.listFW.*;
import company.listFW.visitor.*;
class Manager extends AState {
    private IList _subordinates;
    private Employee _boss;
    
    Manager(IList team, Employee boss) {
        _subordinates = team;
        _boss = boss;
    }
    
    void promote(Employee emp, Employee newBoss, IList newTeam) {
        _boss = newBoss;
        // some code to release the old team...
        // not required as part of the exam question.
        _subordinates = newTeam;
    }
    
    IList getAllWorkers(Employee emp, IListFactory f) {
        return (IList)_subordinates.execute(GetIndians.Singleton, f);
    }
    
 
// Methods needed for the system to be operational:
//    Employee getBoss(Employee emp) {
//        // to do: code goes here.
//        return (null);
//    }
//    
//
//    void setBoss(Employee emp, Employee boss) {
//    }
//    
//
//    IList getSubordinates(Employee emp) {
//        // to do: code goes here.
//        return (null);
//    }
//    
//
//    IList addSubordinate(Employee emp, Employee sub) {
//        // to do: code goes here.
//        return (null);
//    }
}
package company;
import company.listFW.*;
class Worker extends AState {
    private Employee _boss;
    
    void promote(Employee emp, Employee newBoss, IList newTeam) {
       emp.setState(new Manager(newTeam, newBoss));  // STATE CHANGE!      
    }
    
    IList getAllWorkers(Employee emp, IListFactory f) {
        return f.makeEmptyList();
    }
    
// Methods needed for the system to be operational:
//    Employee getBoss(Employee emp) {
//        // to do: code goes here.
//        return (null);
//    }
//    
//
//    void setBoss(Employee emp, Employee boss) {
//    }
//    
//
//    IList getSubordinates(Employee emp) {
//        // to do: code goes here.
//        return (null);
//    }
//    
//    IList addSubordinate(Employee emp, Employee sub) {
//        // to do: code goes here.
//        return (null);
//    }
}

 

 


Last Revised Friday, 23-Feb-2007 12:59:53 CST 02/23/2007