Create a class named Person and its two subclasses named Student and Employee. T
ID: 3698306 • Letter: C
Question
Create a class named Person and its two subclasses named Student and Employee. Then create Faculty and Staff as subclasses of Employee. A Person has a name, address, phone number, and email address. Each of these will be String objects. A Student has a class status for a freshman, 2 for a sophomore, 3 for a junior, or 4 for a senior). An Employee has an office (String), salaryfint). and a date hired(String). A Faculty Member has office bJM&(String) and a rank (1, 2, 3, or 4). A Staff member has a title. Each class will have a method to display the name of the class and the person's name. For a Student, the method will also return the class status, for an Employee, the salary, for a faculty member, the rank, and for a Staff member, the title. Write a test program that Creates a Person, Student, Employee, Faculty Member, and a Staff Member. Sets values for all properties for a particular object (e.g. for a Student you will give the student a name, address, phone number, email address, and class status) Prints a string representation for each object, invoke their method. Notes: Give all instance variables protected visibility to allow access without setters and getters Use the default constructor for all classes Hard code the data in your programs e.g. Sam Bradford";Explanation / Answer
testDemo.java
import java.util.Date;//packgae for date functions
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author CHEGG VINOD
*/
class Person {//super class
private String name;
private String address;//person variables
private int phone_number;
private String email_address;
public Person() {//default constructor
}
public Person(String newName, String newAddress, int newPhone_number, String newEmail){//constructor with arguments
name = newName;
address = newAddress;
phone_number = newPhone_number;
email_address = newEmail;
System.out.println("personname="+newName+","+"person address,"+newAddress+"person emailid=,"+newEmail);
}
public void setName(String newName){//getter and setter methods
name = newName;
}
public String getName(){
return name;
}
public void setAddress(String newAddress){
address = newAddress;
}
public String getAddress(){
return address;
}
public void setPhone(int newPhone_number){
phone_number = newPhone_number;
}
public int getPhone(){
return phone_number;
}
public void setEmail(String newEmail){
email_address = newEmail;
}
public String getEmail(){
return email_address;
}
public String toString(){
return "Name :"+getName();
}
}
class Student extends Person {//student class is the sub class fr person class
public String class_status;
public Student(String name, String address, int phone, String email, String classStatus) {
super(name, address, phone, email);
class_status = classStatus;
System.out.println("Student name="+name+",student address="+address+",student phone="+phone+"email="+email+"classStatus,"+class_status);
}
public String toString(){
return "Student status: " +class_status;
}
}
class Employee extends Person{//employee is the sub class for person
private String office;
private double salary;
private Date hire;
public Employee() {//default constructor
}
public Employee(String name, String address, int phone, String email){//constructor with arguments
super(name, address, phone, email);
}
public Employee(String office, double salary, Date hire){//constructor with arguments
this.office = office;
this.salary = salary;
this.hire = hire;
}
public void setOffice(String office){//setter and getter methods
this.office = office;
}
public String getOffice(){
return this.office;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return this.salary;
}}
class Faculty extends Employee {//faculty class is the sub class for employee
String officeHours;
int rank;
public Faculty(){}//default constructor
public Faculty(String name, String address, int phone, String email,String officeHrs, int rnk) {
super(name, address, phone, email);
officeHours=officeHrs;
rank=rnk;
System.out.println("Faculty name="+name+",Faculty phone number="+phone+",faculty email="+email+",faculty officehrs="+officeHrs+",faculty rank="+rank);
}
public String getOfficeHours() {//getter and setter methods
return officeHours;
}
public void setOfficeHours(String officeHours) {
this.officeHours = officeHours;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String toString(){
return "Office hrs " + officeHours;
}
}
class Staff extends Employee {//staff is the sub class for employee class
private String title;
public Staff(String name, String address, int phone, String email,String ttle) {
super(name, address, phone, email);
title=ttle;
System.out.println("Staff name="+name+",staff phone no ,"+phone+",staff email="+email+",staff title="+ttle);
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
public String toString(){
return "Title :" + title;
}
}
public class testDemo {//driver class or main method
public static void main(String[] args) {
Person person = new Person("vinod", "hyderabad", 55512,
"vinod@yaho.com");//calling constructors
Person student = new Student("henry", "pentStreet",
651212, "henry@abc.com", "junior");
Person employee = new Employee("Tom", "c Street",
40999, "tom@xyz.com");
Person faculty = new Faculty("Johnson", "ParkAvenue",
92333, "john@abcxyz.com","8",2);
Person staff = new Staff("JackBox", "JumpStreet", 707112,
"jackbox@jack.com","It Department");
System.out.println("person name="+person.toString() + " ");
System.out.println("student status="+student.toString() + " ");
System.out.println("faculty office hrs="+faculty.toString() + " ");
System.out.println("staff tilte="+staff.toString() + " ");
}
}
output
run:
personname=vinod,person address,hyderabadperson emailid=,vinod@yaho.com
personname=henry,person address,pentStreetperson emailid=,henry@abc.com
Student name=henry,student address=pentStreet,student phone=651212email=henry@abc.comclassStatus,junior
personname=Tom,person address,c Streetperson emailid=,tom@xyz.com
personname=Johnson,person address,ParkAvenueperson emailid=,john@abcxyz.com
Faculty name=Johnson,Faculty phone number=92333,faculty email=john@abcxyz.com,faculty officehrs=8,faculty rank=2
personname=JackBox,person address,JumpStreetperson emailid=,jackbox@jack.com
Staff name=JackBox,staff phone no ,707112,staff email=jackbox@jack.com,staff title=It Department
person name=Name :vinod
student status=Student status: junior
faculty office hrs=Office hrs 8
staff tilte=Title :It Department
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.