COP3337 Section U05 Spring 2017 A Program for Hw #1 =================== I. The Person class ------------------- // a person has a name an an age, the get and set methods, and // the toString and equals methods public class Person { // the fields private String name; private int age; // construct an object with name n and age ag public Person(String n, int ag) { name = n; age = ag; } // the get methods // return the name public String getName() { return name; } // return the age public int getAge() { return age; } // the set methods // set the name to n public void setName(String n) { name = n; } // set the age to ag public void setAge(int ag) { age = ag; } // the toString method public String toString() { return getClass().getSimpleName() + "[name = " + name + "][age = " + age + "]"; } // 2 persons are equal if other is not null, the 2 objects belong to // the same class and they have the same name and age public boolean equals(Person other) { // check for null if (other == null) return false; // check if they have the same class if ( getClass() != other.getClass()) return false; // check that the fields are equal return name.equals(other.name) && age == other.age; } } II. The Employee Class ---------------------- // the class Employee has 3 fields, company, position, and boss // a constructor, and the methods getCompany, getPosition, // getBoss, toString, equals and getCEO public class Employee extends Person { // the fields private String company; private String position; private Employee boss; // null if the CEO // construct an employee with name n, age ag, // company co, position pos, and boss b public Employee(String n, int ag, String co, String pos, Employee b) { super(n,ag); // form the super class company = co; // set the fields of the subclass position = pos; boss = b; } // return the company public String getCompany() { return company; } // return the position public String getPosition() { return position; } // return the boss public Employee getBoss() { return boss; } // return the CEO public Employee getCEO() { // if current's boss is not null check his boss Employee current = this; while (current.boss != null) current = current.boss; return current; } // toString returns the name, the age, the company, // the position, and the name of the boss, or the word none // if this is the CEO. Use super.toString() public String toString() { // check if this is the CEO if (boss != null) return super.toString() + "[company = " + company + "][position = " + position + "][boss = " + boss.getName() + "]"; else return super.toString() + "[company = " + company + "][position = " + position + "][boss = none]"; } // two employee are equal if they have the same name, age, company and // position. Use super.equals and beware that other can be null. public boolean equals(Employee other) { if (other == null) return false; return super.equals(other) && company.equals(other.company) && position.equals(other.position); } } III. The Driver --------------- // the driver for COP3337 Hw1 public class Spring16cop3337Testhw1 { public static void main(String[] args) { System.out.println("Have Some Pizza"); System.out.println("===============\n\n"); // a list of employees Employee papaBill = new Employee("Papa Bill", 76, "Papa Bill's Pizza", "owner", null); Employee mark = new Employee("Mark", 53, "Papa Bill's Pizza", "manager", papaBill); Employee karen = new Employee("Karen", 72, "Papa Bill's Pizza", "cashier", mark); Employee susan = new Employee("Susan", 40, "Papa Bill's Pizza", "server", mark); Employee tomato = new Employee("Tomato", 40, "Papa Bill's Pizza", "baker", mark); Employee naph = new Employee("Naph", 50, "Papa Bill's Pizza", "driver", papaBill); Employee jay = new Employee("Jay", 65, "Jay's Pizza", "owner", null); Employee susan2 = new Employee("Susan", 40, "Jay's Pizza", "server", jay); // print the employees Employee[] group = { mark, susan, susan2, papaBill, jay, naph, karen, tomato}; System.out.println("The pizza persons:"); System.out.println("------------------"); for (Employee e: group) System.out.println(e.toString()); // find the CEO's try { System.out.println("\nJay's CEO is " + jay.getCEO() + "."); } catch (NullPointerException e) { System.out.println("Oh, no! You did not find Jay's boss."); } try { System.out.println("Tomato's CEO is " + tomato.getCEO() + "."); } catch (NullPointerException e) { System.out.println("Oh, no! You did not find Tomato's boss."); } // declare some pizza lovers Person bill2 = new Person("Papa Bill", 76); Person mark2 = new Person("Mark", 46); Person mark3 = new Person("Mark", 46); Person mark4 = new Person("Mark", 53); Employee tomatito = new Employee("Tomato", 40, "Papa Bill's Pizza", "baker", mark); Employee tomate = new Employee("Tomato", 40, "Papa Bill's Pizza", "baker", papaBill); Employee veggie = new Employee("Tomato", 40, "Papa Bill's Pizza", "driver", mark); Person anotherTomato = new Person("Tomato",40); Person retired = null; System.out.println("The pizza lovers:"); System.out.println("-----------------"); Person[] eaters = { mark4, mark2, bill2, mark3}; for (Person p : eaters) System.out.println(p); // test getBoss System.out.println("\nJay's boss is " + jay.getBoss()); System.out.println("Mark's boss is " + mark.getBoss()); System.out.println("Tomato's boss is " + tomato.getBoss() + "."); // test equals System.out.println("\npapaBill.equals(bill2) is " + papaBill.equals(bill2)); System.out.println("susan.equals(susan2) is " + susan.equals(susan2)); System.out.println("mark2.equals(mark3) is " + mark2.equals(mark3)); System.out.println("mark2.equals(mark4) is " + mark2.equals(mark4)); System.out.println("mark2.equals(mark4) is " + mark2.equals(mark4)); try { System.out.println("tomato.equals(tomatito) is " + tomato.equals(tomatito)); } catch(NullPointerException e) { System.out.println("Oh no! You got a null pointer exception."); } try { System.out.println("tomato.equals(tomate) is " + tomato.equals(tomate)); } catch(NullPointerException e) { System.out.println("Oh no! You got a null pointer exception."); } try { System.out.println("anotherTomato.equals(retired) is " + anotherTomato.equals(retired)); } catch(NullPointerException e) { System.out.println("Oh no! You got a null pointer exception."); } } } IV. The Output: --------------- run: Have Some Pizza =============== The pizza persons: ------------------ Employee[name = Mark][age = 53][company = Papa Bill's Pizza][position = manager][boss = Papa Bill] Employee[name = Susan][age = 40][company = Papa Bill's Pizza][position = server][boss = Mark] Employee[name = Susan][age = 40][company = Jay's Pizza][position = server][boss = Jay] Employee[name = Papa Bill][age = 76][company = Papa Bill's Pizza][position = owner][boss = none] Employee[name = Jay][age = 65][company = Jay's Pizza][position = owner][boss = none] Employee[name = Naph][age = 50][company = Papa Bill's Pizza][position = driver][boss = Papa Bill] Employee[name = Karen][age = 72][company = Papa Bill's Pizza][position = cashier][boss = Mark] Employee[name = Tomato][age = 40][company = Papa Bill's Pizza][position = baker][boss = Mark] Jay's CEO is Employee[name = Jay][age = 65][company = Jay's Pizza][position = owner][boss = none]. Tomato's CEO is Employee[name = Papa Bill][age = 76][company = Papa Bill's Pizza][position = owner][boss = none]. The pizza lovers: ----------------- Person[name = Mark][age = 53] Person[name = Mark][age = 46] Person[name = Papa Bill][age = 76] Person[name = Mark][age = 46] Jay's boss is null Mark's boss is Employee[name = Papa Bill][age = 76][company = Papa Bill's Pizza][position = owner][boss = none] Tomato's boss is Employee[name = Mark][age = 53][company = Papa Bill's Pizza][position = manager][boss = Papa Bill]. papaBill.equals(bill2) is false susan.equals(susan2) is false mark2.equals(mark3) is true mark2.equals(mark4) is false mark2.equals(mark4) is false tomato.equals(tomatito) is true tomato.equals(tomate) is true anotherTomato.equals(retired) is false BUILD SUCCESSFUL (total time: 1 second)