COP 3337 Section U05 Spring 2017 Homework # 1 ============ Due: Wednesday, February 1st, 2017 I. Write the bodies of the methods and of the constructors that have { // you write it } as their bodies. Use the header comments to write the code. // 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; } // return the simple name of the class, the name, and the age public String toString() { // you write it } // 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) { // you write it } } // 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) { // you write it } // 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() { // you write it } // 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() { // you write it } // two employee are equal if belong to the same class, have the same name, // age, company and position. Use super.equals and beware that // other can be null. public boolean equals(Employee other) { // you write it } } II. Use the driver below to test your code. public class Hw1 { // a driver for Employee public static void main(String[] args) { System.out.println("Welcome to Barfy's"); System.out.println("==================\n\n"); // form a list of employee Employee jay = new Employee("Jay", 65, "Barfy's", "boss", null); Employee bill = new Employee("Bill", 40, "Barfy's", "manager", jay); Employee bill2 = new Employee("Bill", 40, "Barfy's", "cook", bill); Employee john = new Employee("John", 35, "Barfy's", "manager", jay); Employee mary = new Employee("Mary", 40, "Barfy's", "cook", john); Employee susan = new Employee("Susan", 25, "Barfy's", "server", bill); Employee pepito = new Employee("Pepito", 40, "Barfy's", "server", john); Employee mark = new Employee("Mark",50, "Barfy's", "cashier", john); Employee rita = new Employee("Rita", 29, "Barfy's", "cashier", bill); // barfy's stuff Employee[] staff = { jay, bill, bill2, john, mary, susan, pepito, mark, rita}; // print the staff System.out.println("\nBarfy's Staff"); for (Employee current : staff) System.out.println(current.toString()); System.out.println("\nThe CEO of Rita's company is " + rita.getCEO().getName() + "."); // check some equalities Person bill3 = new Person("Bill", 40); Person bill4; System.out.println("bill.equals(bill2) is " + bill.equals(bill2)); System.out.println("bill3.equals(bill) is " + bill3.equals(bill)); } } You should get the output listed below. run: Welcome to Barfy's ================== Barfy's Staff Employee[name = Jay][age = 65][company = Barfy's][position = boss][boss = none] Employee[name = Bill][age = 40][company = Barfy's][position = manager][boss = Jay] Employee[name = Bill][age = 40][company = Barfy's][position = cook][boss = Bill] Employee[name = John][age = 35][company = Barfy's][position = manager][boss = Jay] Employee[name = Mary][age = 40][company = Barfy's][position = cook][boss = John] Employee[name = Susan][age = 25][company = Barfy's][position = server][boss = Bill] Employee[name = Pepito][age = 40][company = Barfy's][position = server][boss = John] Employee[name = Mark][age = 50][company = Barfy's][position = cashier][boss = John] Employee[name = Rita][age = 29][company = Barfy's][position = cashier][boss = Bill] The CEO of Rita's company is Jay. bill.equals(bill2) is false bill3.equals(bill) is false BUILD SUCCESSFUL (total time: 0 seconds) III. Some Style Guidelines --------------------- 1. Start the program with the author block below. /* COURSE : COP 3337 * Section : U05 * Semester : Spring 2017 * Instructor : Alex Pelin * Author : (your name) * Assignment #: 1 * Description : Insert a 2-3 line description of the assignment * * * */ 2. The names of classes and of the variables are nouns, and the names of the methods are verbs. The variables and the methods use only lower case letters except for the first letter of the words of a compound word. The constants have only capital letters and the words are separated by underscores. 3. Put header comments to describe what the method does and end comments to describe the variables. 4. You may use Netbeans or Eclipse to develop your program. 5. You must email the completed classes Person and Employee in a text file. You can either copy and paste the code onto the body of the email message or copy it onto a .txt file and mail the file as an attachment to pelina@cis.fiu.edu by the end of the due date (February 1st, 2017). Do not send zipped or word files. 5. Indent the bodies of classes, methods, if's, else's, switch, loops. 6. Avoid deep indentation. Try not to use many embedded ifs by using the boolean connectors &&, ||, and !. 7. If the indentation level is 5 or higher, then indent by 3 or even 2 spaces. 8. Avoid wrap around lines by breaking up the strings with +, using extra assignments,and choosing shorter names for variables. 9. Indent the continuation lines (print statements, method and class headers, etc). 10. Align { with the statement above and } with the matching { and statement below. 11. Do not use "magic constants" in assignments or declarations. Instead of data = new Object[10]; you should have data = new Object[SIZE]; where SIZE is an int constant. SIZE is static if data is a field of the class. 12. You may use shorter names and standard abbreviations, but in this case you should write an end comment describing what the variable does. Example, rad = r; // rad is the radius v = 4 * Math.PI * r * r *r / 4; // v is the volume of the sphere 13. If one variable can do the job, do not use extra variables. 14. Obey the specs. Do not change the names of the class, or the signature of the methods. Also, do not use other classes, like ArrayList, Arrays, LinkedLists, etc unless you clear it with me. 15. If the specs specify the names of the class fields, use those names in your methods. 16. For each class and method put a header comment that describes what the method does.