Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

%3Cp%3Eprogramming101%3A%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0ANeed%20Help%20with%20s

ID: 3552400 • Letter: #

Question

%3Cp%3Eprogramming101%3A%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0ANeed%20Help%20with%20some%20stuff%3A%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AInheritance%20and%20Polymorphism%3Cbr%20%2F%3E%0AThe%20objective%20of%20this%20programming%20assignment%20is%20to%20experience%20the%0Ause%20of%20inheritance%20in%20Java%20and%20to%20see%20how%20polymorphism%20works%20in%0AJava.%3Cbr%20%2F%3E%0AThe%20assignment%20involves%20writing%20three%20classes%2C%20plus%20a%20test%20class.%0AAll%20four%20classes%20are%20in%20the%20same%20project.%20The%20base%20class%20is%20a%0APerson%20class%20which%20contains%20a%20couple%20of%20attributes%20and%20methods%0Acommon%20to%20all%20persons.%20The%20first%20derived%20class%20is%20an%20Employee%20class%0Awhich%20adds%20employee%20information%20to%20a%20Person.%20The%20second%20derived%0Aclass%20is%20a%20GroupManager%20class%20which%20adds%20work%20group%20information%20to%0Aan%20Employee.%20The%20test%20program%20will%20be%20structured%20to%20include%20a%0Amethod%20which%20accepts%20a%20base%20class%20reference%20and%20demonstrates%0Apolymorphic%20behavior.%3Cbr%20%2F%3E%0ANOTE%3A%20None%20of%20the%20first%20three%20classes%20below%20do%20ANY%20user%20input%20or%0Aconsole%20output!%20Console%20output%20is%20only%20done%20in%20the%20display%20method%0Aof%20the%20test%20program!%3Cbr%20%2F%3E%0ANOTE%3A%20ALL%20the%20member%20variables%20of%20these%20classes%20MUST%20BE%0APRIVATE!%3Cbr%20%2F%3E%0ANOTE%3A%20Only%20the%20main%20method%20in%20the%20test%20class%20and%20the%20display%20method%0Ain%20the%20test%20class%20can%20be%20static%20methods.%3Cbr%20%2F%3E%0ADevelopment%20Strategy%3A%20Start%20with%20the%20Person%20class.%20Implement%20that%0Afirst.%20Then%20write%20some%20test%20code%20in%20your%20test%20program%20to%20check%20that%0Ait%20works.%20Then%20go%20on%20to%20the%20Employee%20class.%20When%20that%20is%20written%0Aadd%20test%20code%20for%20that.%20Finally%20implement%20the%20GroupManager%20class%0Aand%20then%20add%20test%20code%20for%20that.%3Cbr%20%2F%3E%0AThe%20details%20of%20the%20three%20classes%20to%20be%20implemented%20are%20as%0Afollows%3A%3Cbr%20%2F%3E%0AThe%20Person%20class%20contains%20the%20name%20and%20address%20of%20a%20person.%20An%0Aexplicit%20value%20constructor%20must%20be%20provided%20to%20set%20both%20of%20these%0Avalues.%20There%20is%20a%20changeName%20and%20a%20changeAddress%20method%20used%20to%0Amodify%20the%20name%20or%20the%20address%20value.%20There%20must%20be%20ONE%20getInfo%0Amethod%20that%20returns%20one%20string%20containing%20both%20the%20name%20and%0Aaddress.%20The%20string%20returned%20should%20be%20formatted%20to%20appear%20as%20shown%0Abelow%3A%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0A%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Name%3A%20Davy%20Jones%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Address%3A%20New%20York%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0APrivate%20Member%20Variables%3A%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bname%2C%20address%3Cbr%20%2F%3E%0APublic%20Methods%3A%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20one%20constructor%20%E2%80%93%20takes%202%20String%0Aparameters%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20changeName%20%E2%80%93takes%201%20String%20parameter%20%E2%80%93%20no%0Areturned%20value%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20changeAddress%20%E2%80%93takes%201%20String%20parameter%20%E2%80%93%20no%0Areturned%20value%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20getInfo%20%E2%80%93%20takes%20no%20parameters%20%E2%80%93%20returns%20a%0AString%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AYour%20class%20can%20NOT%20contain%20any%20other%20member%20variables%20or%0Amethods!%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AThe%20Employee%20class%20inherits%20from%20the%20Person%20class.%20This%20class%20adds%0Aattributes%20for%20an%20employee%20ID%20(int)%2C%26nbsp%3B%20annual%20salary%20(double)%2C%0Aand%20the%20current%20assignment%20(String).%20An%20explicit%20value%20constructor%0Amust%20be%20provided%20to%20set%20the%20two%20values%20of%20the%20base%20class%20plus%20these%0Athree%20values.%20There%20is%20a%20changeSalary%20and%20a%20changeAssignment%20method%0Aused%20to%20modify%20the%20annual%20salary%20or%20the%20current%20assignment.%20The%0Abase%20class%20getInfo%20method%20must%20be%20overridden%20to%20include%20all%20the%0Abase%20class%20person%20information%20plus%20the%20employee%20information.%20The%0Astring%20returned%20should%20be%20formatted%20to%20appear%20as%20shown%20below%3A%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Name%3A%20Davy%20Jones%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Address%3A%20New%20York%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20EmployeeID%3A%20103%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20AnnualSalary%3A%2034567.00%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20CurrentAssignment%3A%0AWelder%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0APrivate%20Member%20Variables%3A%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3BemployeeID%2C%20salary%2C%20assignment%3Cbr%20%2F%3E%0APublic%20Methods%3A%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20one%20constructor%20%E2%80%93%20takes%203%20String%2C%201%20int%2C%20and%201%0Adouble%20parameter%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20changeSalary%20%E2%80%93takes%201%20double%20parameter%20%E2%80%93%20no%0Areturned%20value%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20changeAssignment%20%E2%80%93takes%201%20String%20parameter%20%E2%80%93%20no%0Areturned%20value%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20getInfo%20%E2%80%93%20takes%20no%20parameters%20%E2%80%93%20returns%20a%0AString%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AYour%20class%20can%20NOT%20contain%20any%20other%20member%20variables%20or%0Amethods!%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AThe%20GroupManager%20class%20inherits%20from%20the%20Employee%20class.%20This%20class%0Aadds%20attributes%20for%20a%20group%20name%20(String)%2C%26nbsp%3B%20group%20size%20(int)%2C%0Aand%20a%20list%20of%20employee%20ids%20(int%20array)%20belonging%20to%20the%20group.%20An%0Aexplicit%20value%20constructor%20must%20be%20provided%20that%20accepts%20name%2C%0Aaddress%2C%20employee%20ID%2C%20salary%2C%20group%20name%20and%20group%20size%0Aparameters.%26nbsp%3B%20The%20constructor%20will%20create%20an%20integer%20array%20the%0Asize%20specified%20in%20the%20group%20size%20parameter.%20It%20will%20also%20set%20the%0Agroup%20size%20member%20variable%20to%200%20indicating%20there%20are%20initially%20no%0Aemployee%20ids%20stored%20in%20the%20array.%20There%20is%20an%20addMember%20method%20used%0Ato%20add%20employee%20ids%20to%20the%20list%20of%20employee%20ids%20belonging%20to%20the%0Agroup.%20This%20method%20adds%20the%20employee%20id%20to%20the%20array%20and%20increments%0Athe%20group%20size%20(unless%20the%20list%20is%20full).%20The%20base%20class%20getInfo%0Amethod%20must%20be%20overridden%20to%20include%20all%20the%20base%20class%20employee%0Ainformation%20plus%20all%20the%20group%20information.%20Group%20information%0Ashould%20include%20the%20group%20name%2C%20size%2C%20and%20the%20list%20of%20employee%20ids%0Abelonging%20to%20the%20group.%20The%20string%20returned%20should%20be%20formatted%20to%0Aappear%20as%20shown%20below%3A%3Cbr%20%2F%3E%0AName%3A%20Monty%20Jones%3Cbr%20%2F%3E%0AAddress%3A%20Dallas%3Cbr%20%2F%3E%0AEmployeeID%3A%20104%3Cbr%20%2F%3E%0AAnnualSalary%3A%2054321.00%3Cbr%20%2F%3E%0ACurrentAssignment%3A%20Group%20Manager%3Cbr%20%2F%3E%0AGroup%20Name%3A%20FactoryGroup%3Cbr%20%2F%3E%0AGroup%20Size%3A%203%3Cbr%20%2F%3E%0AMember%201%3A%20101%3Cbr%20%2F%3E%0AMember%202%3A%20102%3Cbr%20%2F%3E%0AMember%203%3A%20103%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0APrivate%20Member%20Variables%3A%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3BgroupName%2C%20groupSize%2C%20eidList%3Cbr%20%2F%3E%0APublic%20Methods%3A%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20one%20constructor%20%E2%80%93%20takes%203%20String%2C%202%20int%2C%20and%201%0Adouble%20parameters%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20addMember%20%E2%80%93takes%201%20int%20parameter%20%E2%80%93%20returns%0ABoolean%20%E2%80%93%20false%20if%20list%20is%20full%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20getInfo%20%E2%80%93%20takes%20no%20parameters%20%E2%80%93%20returns%20a%0AString%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AYour%20class%20can%20NOT%20contain%20any%20other%20member%20variables%20or%0Amethods!%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AThe%20test%20program%E2%80%99s%20main%20method%20needs%20to%20create%20a%20couple%20(at%20least%0A2)%20Employee%20objects%20and%20a%20GroupManager%20object.%20The%20test%20program%0Adoes%20not%20need%20to%20be%20interactive%2C%20so%20user%20input%20is%20not%20required.%20The%0Atest%20program%20should%20add%20the%20employee%20ids%20used%20to%20create%20the%0AEmployee%20objects%20to%20the%20GroupManager%20object.%20The%20test%20program%20MUST%0Acontain%20a%20display%20method%20(a%20static%20method)%20which%20takes%20ONE%20Person%0Aobject%20reference%20as%20a%20parameter.%20The%20display%20method%20should%20use%20the%0AgetInfo%20method%20to%20get%20the%20all%20the%20information%20about%20the%20object%0Apassed%20in%20and%20output%20that%20information.%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AThe%20main%20method%20should%20pass%20the%20Employee%20objects%20and%20the%0AGroupManager%20object%20to%20the%20display%20method.%20The%20output%20seen%0Ademonstrates%20polymorphic%20behavior%2C%20that%20is%20the%20base%20class%20Person%0Areference%20to%20an%20Employee%20object%20accesses%20Employee%20information%2C%20and%0Athe%20base%20class%20Person%20reference%20to%20a%20GroupManager%20object%20accesses%0AGroupManager%20information.%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%3C%2Fp%3E%0A

Explanation / Answer

/*you may also download the code from....http://www.2shared.com/file/sWQDa3GV/Employee.html*/ public class Employee { String name; String department; double hourlyRate; Employee(String name, String department, double hourlyRate) { this.name = name; this.department = department; this.hourlyRate = hourlyRate; } public void setDepartment(String department) { this.department = department; } public void setHourlyRate(double hourlyRate) { this.hourlyRate = hourlyRate; } public String getNameAndDepartment() { return name + " " + department; } double weeklyPay(int numOfHourWorked) { if (numOfHourWorked < 40) { return (numOfHourWorked * hourlyRate); } else return (40 * hourlyRate); } } class UnionEmployee extends Employee { double dues; UnionEmployee(String name, String department, double hourlyRate, double dues) { super(name, department, hourlyRate); this.dues = dues; } public void setDues(double dues) { this.dues = dues; } double weeklyPay(int numOfHourWorked) { if (numOfHourWorked <= 40) { return (super.weeklyPay(numOfHourWorked)); } else return ((super.weeklyPay(40) + ((numOfHourWorked - 40) * hourlyRate * 1.5)) - dues); } } class CommissionEmployee extends Employee { double commisionRate; double salesAmount; CommissionEmployee(String name, String department, double hourlyRate) { super(name, department, hourlyRate); } public void setCommisionRate(double commisionRate) { this.commisionRate = commisionRate; } public void setSalesAmount(double salesAmount) { this.salesAmount = salesAmount; } double weeklyPay(int numOfHourWorked) { return (super.weeklyPay(numOfHourWorked) + (commisionRate * salesAmount)); } } class TestEmployee { UnionEmployee uEmp = new UnionEmployee(null, null, 0, 0); CommissionEmployee cEmp = new CommissionEmployee(null, null, 0); Employee emp = new Employee(null, null, 0); void display(Employee emp, int numOfHourWorked) { System.out .println("Name and department :" + emp.getNameAndDepartment()); System.out.println("Weekly pay of employee :" + emp.weeklyPay(numOfHourWorked)); } void display(UnionEmployee uEmp, CommissionEmployee cEmp, int numOfHourWorked) { System.out.println("Weekly Pay for UnionEmployee" + uEmp.weeklyPay(numOfHourWorked)); System.out.println("Weekly Pay for UnionEmployee" + cEmp.weeklyPay(numOfHourWorked)); } }