COP 3337 Sections U05 Spring 2017 A PRACTICE HOMEWORK =================== This is an example of how you should write the homeworks. It has 5 parts, the statement of the problem, the homework rules, the program, a driver, and the output of the driver. I. The homework: ---------------- The public class Employee has the private fields name, age, and position. Its constructor has the signature public Employee( String, int, String) and puts the first string into name, the integer into age, and the second string into position. The class also has the methods public void setName( String) public void setAge( int ) public void setPosition( String) public String toString() public boolean equals( Employee) The first method changes the name to the value of the parameter. The second method sets the age, and the third one changes the position to the value of the parameter. The method toString() returns the string Employee[name = ...][age = ...][position = ...] where the triple dots stand for the values of the fields. The method equals returns true if all 3 fields are equal and false if one or more fields differ. We ignore the capitalization of the strings. The public class HourlyEmployee is a subclass of Employee. It has 2 extra fields, private double rate; // the hourly rate private double time; // the hours worked this week Its constructor has the signature public HourlyEmployee(String, int, String, double) and assigns the first string to name, the int to age, the second string to position, and the double to rate. It also initializes time to zero. The class has the methods public double getPaid() public void work( double) public void getARaise(double) public String toString() The first method returns the weekly pay. The worker gets payed double for overtime. The method work adds the parameter to time. getARaise adds the value of the parameter to rate. The method toString() returns the string HourlyEmployee[name = ...][age = ...][position = ...][rate = ...] where the triple dots stand for the values of the fields. The public class Manager is a subclass of Employee. It has 1 extra field, private double salary; // the monthly salary Its constructor has the signature public Manager(String, int, double) and assigns the first string to name, the int to age, and the double to salary. It also sets position to manager. The class has the methods public double getPaid() public void getARaise(double) public String toString() The first method returns the monthly pay. getARaise adds the value of the parameter to salary. The method toString() returns the string Manager[name = ...][age = ...][position = manager][salary = ... ] where the triple dots stand for the values of the fields. Write the 3 classes and I will supply a test program. II. Programming Styele Rules: ----------------------------- 1. Use the author block for the Main class. If you do not have to write the Main class include the block in any of classes that you write. You will be penalized if you do not include a description of the homework. The author block is shown below. /* COURSE : COP 3337 * Section : U05 * Semester : Spring 2017 * Instructor : Alex Pelin * Author : (write your name) * Assignment #: (the homework number) * Due date : (the due date, not the submission date) * Description : Insert a 2-3 line description of the assignment * * * */ 2. Indent the bodies of classes, methods, if's, else's, switch, loops, try, and catch. 3. Avoid deep indentation. Try not to use many embedded ifs by using the boolean connectors &&, ||, and !. 4. If the indentation level is 5 or higher, then indent by 3 or even 2 spaces. 5. Avoid wrap around lines by breaking up the strings with +, using extra assignments,and choosing shorter names for variables. 6. Indent the continuation lines (print statements, method and class headers, etc). 7. Align { with the statement above and } with the matching } and statement below. 8. 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. 9. The names of the classes start with a capital letter and are nouns. If the name is a composed noun like Homework2Driver capitalize the first letter of each word. Do not use undescores. You may use abbreviations like Hw2Driver. 10. The names of the methods are verbs and follow the rules of the variables. For example, getRadius, printArray, getCircleArea, setName, findAvg are good method names. 11. The names of the variables are nouns. All letters are lower case except the first letter of the words in a compound noun, except the first one. So, write circleArea, cylinderVolume, arraySize. 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. The constants have only capital letters. If it is a compound name like FREE_TRANSACTIONS use one underscore to separate the words. 14. If one variable can do the job, do not use extra variables. 15. 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. 16. If the specs specify the names of the class fields, use those names in your methods. 17. For each class and method put a header comment that describes what the method does. 18. Mail the homeworks to me (pelina@cis.fiu.edu) as a text file. You may include the program within the text of the email message, or as an attachment. Do not send me zipped files or Word files. 19. The student is responsible for correcting the syntax errors. III. The Program: ----------------- A. The Employee Class: // this class has 3 fields, the name, the age and the position // of the employee as well as the get and set methods, toString and equals public class Employee { // the fields private String name; private int age; private String position; // form an object with name n, age ag, and position pos public Employee(String n, int ag, String pos) { name = n; age = ag; position = pos; } // the get methods public String getName() { return name; } public int getAge() { return age; } public String getPosition() { return position; } // the set methods // change the name to n public void setName(String n) { name = n; } // set the age to ag public void setAge(int ag) { age = ag; } // set the position to pos public void setPosition(String pos) { position = pos; } // override toString public String toString() { return getClass().getSimpleName() + "[name = " + name + " ][ age = " + age + " ][ position = " + position + " ]"; } // two employees are equal if they have the same name, age, and position. // ignore the capitalization public boolean equals (Employee e) { // test for null parameter if (e == null) return false; // check that the 3 fields are equal return name.equalsIgnoreCase(e.name) && age == e.age && position.equalsIgnoreCase(e.position); } } // end class B. The HourlyEmployee Class: // this subclass of Employee has 2 extra fields, rate and time that records // the hourly rate and the time worked that week // it also has a constructor, and the methods getPaid(), work(double), // getARaise(double) and toString() public class HourlyEmployee extends Employee { // the fields private double rate; private double time; private static final double REGULAR_HOURS = 40.0; // for getPaid() private static final double OVERTIME_RATE = 2.0; // for getPaid() // the constructor // form an HourlyEmployee object with name n, age ag, position pos, // and rate r public HourlyEmployee(String n, int ag, String pos, double r) { super(n,ag,pos); rate = r; } // return the gross pay public double getPaid() { double pay = time * rate; // see if there is overtime if (time > REGULAR_HOURS) pay +=(OVERTIME_RATE - 1.0) * (time - REGULAR_HOURS)* rate; // reset time and return pay time = 0.0; return pay; } // work adds the parameter hours to time public void work(double hours) { time += hours; } // getARaise adds the parameter raise to rate public void getARaise( double raise) { rate += raise; } // override toString to include the rate public String toString() { return super.toString() + "[ rate = " + rate + " ]"; } } // end class C. The Manager Class: // manager is an extension of Employee // it has a monthly salary, a constructor, an the methods, getPaid, // getAraise and toString() public class Manager extends Employee { // the fields private double salary; // the monthly salary // construct a manager object with name n, age ag, and salary sal public Manager(String n, int ag, double sal) { super(n,ag,"manager"); salary = sal; } // getpaid() returns the monthly salary public double getPaid() { return salary; } // getARaise adds extra to the salary public void getARaise(double extra) { salary += extra; } // override toString to include the salary public String toString() { return super.toString() + "[ salary = " + salary + " ]"; } } // end class IV. The Driver: --------------- // driver for Hw2 public class Spring15COP3337TestHw2 { public static void main(String[] args) { System.out.println("Test Hw2"); System.out.println("========\n\n"); // define the employees final int EMPLOYEE_NUMBER = 6; HourlyEmployee[] resto = new HourlyEmployee[EMPLOYEE_NUMBER]; resto[0] = new HourlyEmployee("Papa Bill", 75, "cashier", 10); resto[1] = new HourlyEmployee("Geoff the Chef", 54, "cook", 20 ); resto[2] = new HourlyEmployee("Ali Baba", 70, "server", 12); resto[3] = new HourlyEmployee("Grandpa Smith", 65, "server", 10); resto[4] = new HourlyEmployee("Joker", 50, "cook", 15); resto[5] = new HourlyEmployee("Shrek", 50, "server", 15); Manager boss = new Manager("Papa Smurf", 65, 2500); // print the employee System.out.println("The Geezer Palace Employees"); System.out.println("The boss is : " + boss.toString()); for (HourlyEmployee e: resto) System.out.println(e.toString()); // the employees work resto[0].work(40); resto[1].work(50); resto[2].work(20); resto[3].work(30); resto[4].work(60); resto[5].work(20); // now they get paid System.out.println("\nThis month Papa Smurf gets " + boss.getPaid()); System.out.println("The weekly pay for the employees is "); for (HourlyEmployee e: resto) System.out.println(e.getName() + " gets " + e.getPaid() + " dollars."); // some get raises System.out.println("\nJoker gets a 5 dollar raise."); resto[4].getARaise(5); System.out.println("On Monday and Tuesday he works 30 hours."); resto[4].work(30); System.out.println("On Wednesday, Thursday and Friday he works 20 hours."); resto[4].work(20); System.out.println("Joker gets " + resto[4].getPaid()); System.out.println("\nPapa Smurf gets a 200 dollar raise"); boss.getARaise(200); System.out.println("Next month he gets " + boss.getPaid()); // test the class Employee System.out.println("\nGrandpa Smith is now 66."); resto[3].setAge(66); System.out.println("resto[3] = " + resto[3].toString()); System.out.println("\nAli Baba changes his name to Carpet Flyer."); resto[2].setName("Carpet Flyer"); System.out.println("resto[2] = " + resto[2].toString()); System.out.println("\nJoker became a cashier."); resto[4].setPosition("cashier"); System.out.println(" resto[4] = " + resto[4].toString()); // test equals Employee a = new Employee("Hagi Bazari", 45, "server"); Employee b = new Employee("Hagi Bazari", 45, "cook"); Employee c = new Employee("Hagi Liu", 45, "server"); Employee d = new Employee("HAGI BAZARI", 45, "SERVER"); Employee e = new Employee("Hagi Bazari", 25, "server"); System.out.println("\na.equals(b) is " + a.equals(b)); System.out.println("\na.equals(c) is " + a.equals(c)); System.out.println("\na.equals(d) is " + a.equals(d)); System.out.println("\na.equals(e) is " + a.equals(e)); } // end method } // end class V. The Output: -------------- run: Test Hw2 ======== The boss is : Manager[name = Papa Smurf ][ age = 65 ][ position = manager ][ salary = 2500.0 ] HourlyEmployee[name = Papa Bill ][ age = 75 ][ position = cashier ][ rate = 10.0 ] HourlyEmployee[name = Geoff the Chef ][ age = 54 ][ position = cook ][ rate = 20.0 ] HourlyEmployee[name = Ali Baba ][ age = 70 ][ position = server ][ rate = 12.0 ] HourlyEmployee[name = Grandpa Smith ][ age = 65 ][ position = server ][ rate = 10.0 ] HourlyEmployee[name = Joker ][ age = 50 ][ position = cook ][ rate = 15.0 ] HourlyEmployee[name = Shrek ][ age = 50 ][ position = server ][ rate = 15.0 ] This month Papa Smurf gets 2500.0 The weekly pay for the employees is Papa Bill gets 400.0 dollars. Geoff the Chef gets 1200.0 dollars. Ali Baba gets 240.0 dollars. Grandpa Smith gets 300.0 dollars. Joker gets 1200.0 dollars. Shrek gets 300.0 dollars. Joker gets a 5 dollar raise. On Monday and Tuesday he works 30 hours. On Wednesday, Thursday and Friday he works 20 hours. Joker gets 1200.0 Papa Smurf gets a 200 dollar raise Next month he gets 2700.0 Grandpa Smith is now 66. resto[3] = HourlyEmployee[name = Grandpa Smith ][ age = 66 ][ position = server ][ rate = 10.0 ] Ali Baba changes his name to Carpet Flyer. resto[2] = HourlyEmployee[name = Carpet Flyer ][ age = 70 ][ position = server ][ rate = 12.0 ] The Geezer Palace Employees Joker became a cashier. resto[4] = HourlyEmployee[name = Joker ][ age = 50 ][ position = cashier ][ rate = 20.0 ] a.equals(b) is false a.equals(c) is false a.equals(d) is true a.equals(e) is false BUILD SUCCESSFUL (total time: 0 seconds)