// CS U213 Spring 2007 // Lecture 9: January 29, 2007 /* ;; A Boss is (make-spu String String Number [Listof Employee]) (define-struct sup (name unit tasks peons)) ;; A Worker is (make-worker String Number)) (define-struct worker (name tasks)) ;; An Employee is one of ;; -- Boss ;; -- Worker ;; A [Listof Employee] is one of ;; -- empty ;; -- (cons Employee [Listof Employee]) +-----------+ | Emp |<-----------------------------+ +-----------+ | | | | +-----------+ | | | | | /_\ | | | +-----------------------------------+ | | | | +----------------+ +------------------+ | | Worker | | Boss | | +----------------+ +------------------+ | |String name | | String name | | |int tasks | | String unit | | +----------------+ | int tasks | | +--------+ ListofEmp peons | | | +------------------+ | | | v | +--------------+ | | ListofEmp |<------------------------+--+ +--------------+ | | +--------------+ | | | | | | | | /_\ | | | | | +--------------------------------+ | | v v | | +-----------------+ +-------------------+ | | | MTListofEmp | | ConsListofEmp | | | +-----------------+ +-------------------+ | | +-----------------+ | Emp first +---+ | | ListofEmp rest +------+ +-------------------+ */ interface Emp { // count all Employees int countAll(); // count subordinates int countSubs(); } class Worker implements Emp { String name; int tasks; Worker(String name, int tasks){ this.name = name; this.tasks = tasks; } // count all Employees int countAll() { return 1; } // count subordinates int countSubs() { return 0; } } class Boss implements Emp { String name; String unit; int tasks; ListofEmp peons; Boss(String name, String unit, int tasks, ListofEmp peons){ this.name = name; this.unit = unit; this.tasks = tasks; this.peons = peons; } // count all subordinates int countSubs(){ return this.peons.countAll(); } // count all Employees int countAll(){ return 1 + this.countSubs(); } } interface ListofEmp { // count all employees in the list int countAll(); } class MTListofEmp implements ListofEmp { MTListofEmp() {} // count all employees in the list int countAll(){ return 0; } } class ConsListofEmp implements ListofEmp { Emp first; ListofEmp rest; ConsListofEmp(Emp first, ListofEmp rest){ this.first = first; this.rest = rest; } // count all employees in the list int countAll() { return this.first.countAll() + this.rest.countAll(); } } class Examples { Examples() {} Emp wkA = new Worker("A",3); Emp wkB = new Worker("B",5); Emp wkC = new Worker("C",6); Emp wkD = new Worker("D",4); Emp wkE = new Worker("E",5); Emp wkF = new Worker("F",2); Emp wkG = new Worker("G",8); Emp wkH = new Worker("H",6); ListofEmp mtlist = new MTListofEmp(); ListofEmp grpAlist = new ConsListofEmp(this.wkC,this.mtlist); Emp mike = new Boss("Mike", "Group A", 10, this.grpAlist); ListofEmp secAlist = new ConsListofEmp(this.mike, new ConsListofEmp(this.wkD, new ConsListofEmp(this.wkE,this.mtlist))); Emp jack = new Boss("Mike", "Section A", 25, this.secAlist); ListofEmp secBlist = new ConsListofEmp(this.wkF, new ConsListofEmp(this.wkG, this.mtlist)); Emp jenn = new Boss("Jenn", "Section B", 15, this.secBlist); ListofEmp secClist = new ConsListofEmp(this.wkH,this.mtlist); Emp pat = new Boss("Pat", "Section C", 20, this.secClist); ListofEmp secDlist = new ConsListofEmp(this.wkB, this.mtlist); Emp pete = new Boss("Pete", "Section D", 10, this.secDlist); ListofEmp operList = new ConsListofEmp(this.jack, new ConsListofEmp(this.jenn, new ConsListofEmp(this.pat, this.mtlist))); Emp dave = new Boss("Dave","Operations", 70, this.operList); ListofEmp financeList = new ConsListofEmp(this.pete, new ConsListofEmp(this.wkA, this.mtlist)); Emp anne = new Boss("Anne", "Finance", 20, this.financeList); ListofEmp ceoList = new ConsListofEmp(this.dave, new ConsListofEmp(this.anne,this.mtlist)); Emp meg = new Boss("Meg","CEO", 100, this.ceoList); boolean test1 = check this.anne.countSubs() expect 3; boolean test2 = check this.dave.countSubs() expect 10; boolean t1 = check this.secAlist.countAll() expect 4; boolean t2 = check this.mtlist.countAll() expect 0; boolean t3 = check this.financeList.countAll() expect 3; } /* With the preceding examples draw a company structure diagram. Try to capture all the information from the examples in your diagram. +-----------+ | Meg (CEO)| | 100| +---------+-+ | +----------------------+---------------------------------------------------------------------+ | | | | +------------------+ +------+--------+ | Dave (Operations)| | Anne (Finance)| | 70| | 20| +-------+----------+ +------+--------+ | | | | +------------+---------------------------------------+-----------------------+ +---+--------------+ | | | | | +-----------------+ +--------+--------+ +--------+-------+ +---+---+ +----+------------+ | Jack (Section A)| | Jenn (Section B)| | Pat (Section C)| | A | | Pete (Section D)| | 25| | 15| | 20| | 3| | 10| +--------+--------+ +--------+--------+ +--------+-------+ +-------+ +-----+-----------+ | | | | | | | | | | | +---+--+ +------+------------------+----------+ +----+----+ +---+---+ | B | | | | | | | H | | 5| +---------------+ +------+ +---+--+ +--+---+ +------+ | 6| +------+ | Mike (Group A)| | D | | E | | F | | G | +-------+ | 10| | 4| | 2| | 8| | 6| +------+--------+ +------+ +------+ +------+ +------+ | | +------+--+ | C | | 6| +---------+ Some questions that you might want to answer for our company example are: (a) How many subordinates does a Boss have? (b) How many tasks does a Boss delegate to subordinates? (c) How many direct tasks does a Boss undertake? (d) How many level of subordinates does a Boss have? We can design methods that can answer these questions. We'll start with (a) from the list above. 1) Problem analysis: We need to count all the subordinates. Only Boss has subordinates. 2) Purpose Statement and Header // count all subordinates int countSubs(){ ... } 3) Examples: anne.countSubs() => 3 dave.countSubs() => 10 4) Template int countSubs() { ... this.name ... // string ... this.unit ... // string ... this.tasks ... // int ... this.peons ... // ListofEmp } We need to count all subordinates. So we are interested in this.peons. We need to count all the elements in a ListofEmp. But a ListofEmp can contain Workers or Bosses again. WE NEED A HELPER FUNCTION THAT COUNTS THE SUBORDINATES INSIDE A ListofEmp. We'll call our helper function countAll. So again from step 1 1) Problem statement Count all the subordinates 2) Purpose statement and Header Add to ListofEmp, MTListofEmp, ConsListofEmp. We also need to add this to Emp, Worker and Boss. Since the elements of the list are all Emps and we need to find each of their subordinates, each Emp has to be able to report back its total number of subordinates. // count all elements in the list workers // bosses and their subordinates. int countAll(){ ... } 3) Examples: secAlist.countAll() => 4 mtList.countAll() => 0 financeList.countAll() => 3 4) Template MTListofEmp : int countAll() { ... } ConsListofEmp : int countAll() { ... this.first ... //Emp ... this.rest ... //ListofEmp ... this.rest.countAll() ... // int } 5) Method Body MTListofEmp : int countAll() { return 0; } ConsListofEmp : int countAll() { return this.first.countAll() + this.rest.countAll(); } Worker: int countAll(){ return 1; } Boss: int countAll(){ return 1 + this.countSubs();} 5) Method Body int countSubs() { return this.peons.countAll(); } We'll write a table with a interface/class as a column. In each column we will write the methods that we designed Emp | Worker | Boss | | // count all employees | int countAll(){ | // how many subordinates int countAll(); | return 1; } | int countSubs(){ | | this.peons.countAll();} | | | | int countAll(){ | | return 1 + this.countSubs(); | | } -----------------------+----------------------+------------------------------------- ListofEmp | MTListofEmp | ConsListofEmp //count all peons and | int countAll(){ | int countAll() { //Bosses | return 0; | return first.countAll() + int countAll() | } | rest.countAll(); | | } | | | | | |