In: Computer Science
this question has been answered incorrectly several times can someone answer it correctly and show me some examples of a run.
AGAIN SOMEONE SENT AN ANSWER THAT IS INCORRECT CAN SOMEONE ELSE PLEASE GET ME THE PROPER ANSWER.
Design and implement a class named Person and its two subclasses named Student and Employee.
Make Faculty and Staff subclasses of Employee.
A person has a name,address, phone number, and email address.
A student has a class status (year 1,year 2,year 3,or year 4).
An employee has an office, salary, and date hired.
Use the Date class from JavaAPI 8 to create an object for date hired.
A faculty member has office hours and a rank.
A staff member has a title.
Override the toString method in each class to display the class name and the person’s name.Design and implement all 5classes.
Write a test program that creates a Person,Student,Employee,Faculty, and Staff, and iinvokes their toString()methods
UPDATED!!
Java program:
import java.time.LocalDate;
import java.time.Month;
public class program{
public static class Person{
public String name;
public String address;
public int phone_num;
public String email;
//cempty onstructor
Person(){
}
//parameterized constructor
public Person(String name, String address, String email, int phone){
this.name = name;
this.address = address;
this.email = email;
this.phone_num = phone;
}
@Override
public String toString(){
return "Class name: Person\nName: " + name + "\nAddress: " + address + "\nEmail: " + email + "\nPhone: " + String.valueOf(phone_num);
}
}
public static class Student extends Person{
public String status;
//constructor
public Student( String name, String address, String email, int phone, String status){
//calling constructor of parent class
super(name, address, email, phone);
this.status = status;
}
@Override
public String toString(){
return "Class name: Person\nName: " + name + "\nStatus: "+ status+ "\nAddress: " + address + "\nEmail: " + email + "\nPhone: " + String.valueOf(phone_num);
}
}
public static class Employee extends Person{
public String office;
public int salary;
public LocalDate date_of_hiring;
Employee(){
}
//constructor
public Employee(String name, String address, String email, int phone, String office, int salary, LocalDate date){
//calling constructor of parent class
super(name, address, email, phone);
this.office = office;
this.salary = salary;
this.date_of_hiring = date;
}
@Override
public String toString(){
return "Class name: Person\nName: " + name + "\nAddress: " + address + "\nEmail: " + email + "\nPhone: " + String.valueOf(phone_num) + "\nOffice: " + office + "\nSalary: " + String.valueOf(salary) + "\nHiring Date: " + String.valueOf(date_of_hiring);
}
}
public static class Faculty extends Employee{
public int office_hours;
public String rank;
//constructor
Faculty(String name, String address, String email, int phone, String office, int salary, LocalDate date,int office_hrs, String rank){
//calling constructor of parent class
super(name, address, email, phone, office, salary, date);
this.office_hours = office_hrs;
this.rank = rank;
}
@Override
public String toString(){
return "Class name: Person\nName: " + name + "\nAddress: " + address + "\nEmail: " + email + "\nPhone: " + String.valueOf(phone_num) + "\nOffice: " + office + "\nSalary: " + String.valueOf(salary) + "\nHiring Date: " + String.valueOf(date_of_hiring) + "\nOffice hours: " + office_hours + "\nRank: " + rank;
}
}
public static class Staff extends Employee{
public String title;
//constructor
Staff(String name, String address, String email, int phone, String office, int salary, LocalDate date, String title){
//calling constructor of parent class
super(name, address, email, phone, office, salary, date);
this.title = title;
}
@Override
public String toString(){
return "Class name: Person\nName: " + name + "\nAddress: " + address + "\nEmail: " + email + "\nPhone: " + String.valueOf(phone_num) + "\nOffice: " + office + "\nSalary: " + String.valueOf(salary) + "\nHiring Date: " + String.valueOf(date_of_hiring) + "\nTitle: "+ title;
}
}
//main method
public static void main(String[] args) {
//making a date object
LocalDate date = LocalDate.of(2000, Month.OCTOBER, 17);
//make objects
Person p = new Person("Amit Rawat", "maya nagar, OMP", "[email protected]", 838374384);
Student s = new Student("Mohit Singh", "abc nagar, ogp city", "[email protected]",838374384, "year 1");
Employee emp = new Employee("Nester troop", "sjds nagar, sdsd city", "[email protected]",838374384, "IT", 12000, date);
Faculty f = new Faculty("Lightning thunder", "sjds nagar, sdsd city", "[email protected]",99943944, "BFA", 120000, date, 8, "Head Manager");
Staff st = new Staff("macwueen", "sjds nagar, sdsd city", "[email protected]",838374384, "IT", 10000, date, "Clerk");
//invoking the toString methods of each class using their objects
System.out.println("\nPerson object\n" + p.toString() + "\n");
System.out.println("\nStudent object\n" + s.toString() + "\n");
System.out.println("\nEmployee object\n" + emp.toString() + "\n");
System.out.println("\nFaculty object\n" + f.toString() + "\n");
System.out.println("\nStaff object\n" + st.toString() + "\n");
}
}
Output:
if it helps you, do upvote as it motivates us a lot!