CS 2102 - Bterm 09

Homework 3 - Complex Class Hierarchies

Due: Tuesday, November 10 at 5pm


Acknowledgement: This assignment is a modified version of an assignment designed by Dr. Viera Proulx.

Assignment Goals


Assignment

In this assignment you will design data and methods for information that represents a hierarchy of employees at a company. In DrScheme, we would define the data this way:
;; A Boss is (make-boss String String Number list-of-Employee)
(define-struct boss (name unit tasks peons))

;; A Worker is (make-worker String Number))
(define-struct worker (name tasks))

;; An Employee is one of 
;; -- Boss
;; -- Worker 

;; A list-of-Employee is one of 
;; -- empty
;; -- (cons Employee list-of-Employee)

Here's a class diagram that represents the same information:

                             +-----------+
                             |   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    +------+
                                                +-------------------+
And finally, here is an example of data (in ProfessorJ) representing the hierarchy of employees at a certain company:
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("Jack", "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.wkA,                                                  
    new ConsListofEmp(this.pete, 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);
}

Problems

Use the Design Recipe for all methods, including any helpers.
  1. Draw a company hierarchy chart (showing who reports to who) that represents the information in the given example. You won't be submitting your drawing, but if you come for office hour help we will ask you to show us your drawing.

  2. Design the method countAll() that will count all people supervised by this employee. Include self in the count.

  3. Please note the correction added on November 9, 11:15am:

    Design the method allUnit() that produces a list of all subordinates of this worker employee. Include self in the list. (Hint: how would you solve this problem if you were programming your solution in DrScheme?)

  4. Design the method isBoss() that consumes a name and determines whether this employee or one of its subordinates is a boss with the given name.

  5. All employees are to be ranked as follows: a worker (someone who is not the boss of anyone) has rank 1. The rank of a boss is defined as one more than the number of levels of subordinates. For our example, Mike has rank 2 and Dave has rank 4.

    Design the method rank() that determines the rank of this employee.

  6. Design the method sortByRank() that uses this list of employees to produce a list of employees sorted by rank. Ties may be listed in any order. (Hint: remember how we did sorting in DrScheme?)

What to Turn In

Using web-based turnin, turn in a single file containing all code and documentation for this assignment. Follow the naming conventions when naming your file. Both partners' names and wpi login names should appear in a comment at the top of the file.