Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - Employee Class


CSCI 431 HW 1 Assigned: Monday January 28, 2013 Due: Monday February 4, 2013 In this assignment, you are to write a Java class Employee which emulates the concept of an employee at a company. An Employee object must contain the following private instance fields: String name; // The name of the employee int id; // The employee’s ID number (0-999999) Employee boss; // The employee’s boss (also an object of type // Employee) Notice that one of the fields of the class Employee is a reference to an object of the same class Employee. We refer to this as a linked data structure. In particular, the above class turns out to be isomorphic to a “singly linked list.” This class should, at the least, also include the following methods: public Employee() // Creates a default employee “John Doe” // with id = 0 and boss = null. public Employee(String name, int id) // Creates an employee with supplied name // and id number. boss is null reference. public Employee(String name, int id, Employee boss) // Creates an employee with supplied // name, id number, and boss public String getName() // Returns the name of an employee public void setName(String name) // Sets the name of an employee public int getID() // Returns the ID number of an employee public void setID(int num) // Sets ID number of an employee to num public Employee getBoss() // Returns a reference to an employee’s // boss public void setBoss(Employee emp) // Sets an employee’s boss to the // employee referenced by emp. public String toString() // Returns a string that describes an // Employee object for purposes of // writing to the screen. public boolean isBossOf(Employee emp) // Return true if and only if the // implicit employee is the direct or // indirect boss (i.e. the boss of the // boss of the boss …) of emp. public static boolean isBossOf(Employee a, Employee b) // Returns true if and only if employee // a is the direct or indirect boss of // employee b. public static void main(String[] args) // This main function will create a // linked data structure of the // organizational tree shown below and // then uses a look to print out the // chain of command starting at “John // Smith” and working up As an observation, this class only lets you work your way from the bottom up. In other words, from a reference to any given person, you can see that person’s boss; however, you cannot see his/her subordinates. As an additional exercise (not for credit), can you modify this class so you can access an employee’s subordinates. Remember, while an employee can only have one boss; he/she can have multiple subordinates. Attachments Tags Clarifications


Java - Employee Question - Solution


Attached is the solution to the Java - Employee Question including source and comments. It isn't clear what is needed for the output, so if you need something different just send me a message. Attachments (3 K) Download Preview
emp){ Employee tempBoss = emp.getBoss(); //keep looping until no boss while(tempBoss != null){ //validate boss against id and name if(tempBoss.getId() == && tempBoss.getName().equals({ //have a boss match return true; } //no match, keep looping tempBoss = tempBoss.getBoss(); }

Purchase Answer