COMP 274 Week 2
Inheritance and Polymorphism
The objective of this programming assignment is to experience the use of inheritance in Java and to see how polymorphism works in Java.
The assignment involves writing three classes, plus a test class. All four classes are in the same project. The base class is a Person class which contains a couple of attributes and methods common to all persons. The first derived class is an Employee class which adds employee information to a Person. The second derived class is a GroupManager class which adds work group information to an Employee. The test program will be structured to include a method which accepts a base class reference and demonstrates polymorphic behavior.
NOTE: None of the first three classes below do ANY user input or console output! Console output is only done in the display method of the test program!
NOTE: ALL the member variables of these classes MUST BE PRIVATE!
NOTE: Only the main method in the test class and the display method in the test class can be static methods.
Development Strategy: Start with the Person class. Implement that first. Then write some test code in your test program to check that it works. Then go on to the Employee class. When that is written add test code for that. Finally implement the GroupManager class and then add test code for that.
The details of the three classes to be implemented are as follows:
1. The Person class contains the name and address of a person. An explicit value constructor must be provided to set both of these values. There is a changeName and a changeAddress method used to modify the name or the address value. There must be ONE getInfo method that returns one string containing both the name and address. The string returned should be formatted to appear as shown below:
Name: Davy Jones
Address: New York
Private Member Variables:
name, address
Public Methods:
one constructor – takes 2 String parameters
changeName –takes 1 String parameter – no returned value
changeAddress –takes 1 String parameter – no returned value
getInfo – takes no parameters – returns a String
Your class can NOT contain any other member variables or methods!
2. The Employee class inherits from the Person class. This class adds attributes for an employee ID (int), annual salary (double), and the current assignment (String). An explicit value constructor must be provided to set the two values of the base class plus these three values. There is a changeSalary and a changeAssignment method used to modify the annual salary or the current assignment. The base class getInfo method must be overridden to include all the base class person information plus the employee information. The string returned should be formatted to appear as shown below:
Name: Davy Jones
Address: New York
EmployeeID: 103
AnnualSalary: 34567.00
CurrentAssignment: Welder
Private Member Variables:
employeeID, salary, assignment
Public Methods:
one constructor – takes 3 String, 1 int, and 1 double parameter
changeSalary –takes 1 double parameter – no returned value
changeAssignment –takes 1 String parameter – no returned value
getInfo – takes no parameters – returns a String
Your class can NOT contain any other member variables or methods!
3. The GroupManager class inherits from the Employee class. This class adds attributes for a group name (String), group size (int), and a list of employee ids (int array) belonging to the group. An explicit value constructor must be provided that accepts name, address, employee ID, salary, group name and group size parameters. The constructor will create an integer array the size specified in the group size parameter. It will also set the group size member variable to 0 indicating there are initially no employee ids stored in the array. There is an addMember method used to add employee ids to the list of employee ids belonging to the group. This method adds the employee id to the array and increments the group size (unless the list is full). The base class getInfo method must be overridden to include all the base class employee information plus all the group information. Group information should include the group name, size, and the list of employee ids belonging to the group. The string returned should be formatted to appear as shown below:
Name: Monty Jones
Address: Dallas
EmployeeID: 104
AnnualSalary: 54321.00
CurrentAssignment: Group Manager
Group Name: FactoryGroup
Group Size: 3
Member 1: 101
Member 2: 102
Member 3: 103
Private Member Variables:
groupName, groupSize, eidList
Public Methods:
one constructor – takes 3 String, 2 int, and 1 double parameters
addMember –takes 1 int parameter – returns Boolean – false if list is full
getInfo – takes no parameters – returns a String
Your class can NOT contain any other member variables or methods!
4. The test program’s main method needs to create a couple (at least 2) Employee objects and a GroupManager object. The test program does not need to be interactive, so user input is not required. The test program should add the employee ids used to create the Employee objects to the GroupManager object. The test program MUST contain a display method (a static method) which takes ONE Person object reference as a parameter. The display method should use the getInfo method to get the all the information about the object passed in and output that information.
The main method should pass the Employee objects and the GroupManager object to the display method. The output seen demonstrates polymorphic behavior, that is the base class Person reference to an Employee object accesses Employee information, and the base class Person reference to a GroupManager object accesses GroupManager information.
Take screen shots of the output of the program. Paste the screen shots and your source code for all your classes into a Word document.