Q2. Implement the following inheritance hierarchy: a. Class Person has two field
ID: 3680307 • Letter: Q
Question
Q2. Implement the following inheritance hierarchy: a. Class Person has two fields, name and civil ld, constructor, getter and setter methods and toString method, which returns a String containing the Person information. b. Class Student which is a sub-class of the Person class. Class Student has two fields, College and GPA, constructor (should calls the constructor of the super class), getter and setter methods and toString method (overridden) to return a String containing the student information. Class Faculty which is a sub-class of the Person class. Class Employee has two fields, Department and Salary, constructor (should calls the constructor of the super class), getter and setter methods and toString method (overridden) to return a String containing the Employee information. c. d. Write an application that creates objects of your classes and outputs each object's information.Explanation / Answer
public class TestPerson {
public static void main(String[] args) {
Person person = new Person("John Doe", "415-555-1212");
Person student = new Student("Sara", "28911111", "engineering", 3.00);
Person employee = new Employee("Tom Jones", "777 B Street", "40-88-889-999", "tj@xyz.com");
Person faculty = new Faculty("Jill Johnson", "92-52-22-3-333", "computer engineering", 2000.00);
System.out.println(person.toString() + " ");
System.out.println(student.toString() + " ");
System.out.println(employee.toString() + " ");
System.out.println(faculty.toString() + " ");
}
}
-------------------------
public class Person {
private String name;
private String civil_Id;
public Person() {
}
public Person(String newName, String newcivil_Id){
name = newName;
civil_Id = newcivil_Id;
}
public void setName(String newName){
name = newName;
}
public String getName(){
return name;
}
public void setCivilID(String newcivil_Id){
civil_Id = newcivil_Id;
}
public String getCivilID(){
return civil_Id;
}
public String toString(){
return "Name: " +getName()+ "Civil ID: " +getCivil_ID;
}
}
-------------------------------------
public class Student extends Person{
private String myCollege; // Student college
private double myGPA; // grade point average
// constructor
public Student(String name, int id, String college, double gpa){
// use the super class' constructor
super(name, id);
// initialize what's new to Student
myCollege = college;
myGPA = gpa;
}
public String getcollege(){
return myCollege;
}
public double getGPA(){
return myGPA;
}
public void setcollege(String college){
myCollege = college;
}
public void setGPA(double gpa){
myGPA = gpa;
}
// overrides the toString method in the parent class
public String toString(){
return super.toString() + ", student college: " + myCollege + ", GPA: " + myGPA;
}
}
-------------------------------------
import java.util.Date;
public class Employee extends Person{
private String department;
private double salary;
public Employee() {
}
public Employee(String name, String civil_id, String department, double salary){
super(name, civil_id);
}
public Employee(String department, double salary, Date hire){
this.department = department;
this.salary = salary;
}
public void setdepartment(String department){
this.department = department;
}
public String getdepartment(){
return this.department;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return this.salary;
}
public String toString(){
return "Name: " + super.getName()+ "Civil ID: " +super.getCivil_id();
}
}
---------------
public class Faculty extends Employee {
public Faculty(String name, String civil_id, String department, double salary) {
super(name, civil_id);
}
public String toString(){
return "Office " + super.getdepartment()+ "salary: " +super.getSalary()+ "KD";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.