The objective of this programming assignment is to experience the use of inherit
ID: 3552370 • Letter: T
Question
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
Explanation / Answer
Person.java
public class Person {
private String name;
private String address;
public Person(String name, String address) {
this.name = name;
this.address = address;
}
public void changeName(String newName) {
name = newName;
}
public void changeAddress(String newAddress) {
name = newAddress;
}
public String getInfo() {
return " Name: " + name + " Address: " + address;
}
}
Employee.java
public class Employee extends Person {
private int employeeID;
private double salary;
private String assignment;
public Employee(String name, String address, int employeeId, double salary,
String assignment) {
super(name, address);
this.employeeID = employeeId;
this.salary = salary;
this.assignment = assignment;
}
public void changeSalary(int newSalary) {
salary = newSalary;
}
public void changeAssignment(String newAssignment) {
assignment = newAssignment;
}
public String getInfo() {
return super.getInfo() + " EmployeeId: " + employeeID
+ " Salary: " + salary + " Assignment: " + assignment;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.