Chapter 11 Question 2 (Person, Student, Employee, Faculty and Staff categories) (Person, Student, Employee, Faculty and Staff calss)

Chapter 11 Question 2 (Person, Student, Employee, Faculty and Staff categories) (Person, Student, Employee, Faculty and Staff calss)

  • 11.2 (Person, Student, Employee, Faculty and Staff) Design a class named Person and its two subclasses named Student and Employee. The Employee class has subclasses: Faculty and Staff. Everyone has a name, address, phone number, and email address. Students have class status (freshman, sophomore, junior or senior). Define these states as constants. An employee involves office, salary, and date of hire. Use the MyDate class defined in the programming exercise 10.14 to create an object for the hire date. Teachers have office hours and levels. Staff members have titles. Rewrite the toString method in each class to display the corresponding class name and person's name.
    Draw UML diagrams of these classes and implement these classes. Write a test program to create Person, Studet, Employee, Faculty and Staff, and call their toString method.
    11.2(Person、Student、Employee、Faculty and Staff calss)Design a class named person and two subclasses named student and employee. Employee class has subclasses: Faculty class faculty and staff class. Everyone has a name, address, phone number and email address. Students have class status (freshmen, sophomores, juniors or seniors). Define these states as constants. An employee involves the office, salary and date of appointment. Use the mydate class defined in programming exercise 10.14 to create an object for the engagement date. Teachers have office hours and grades. The staff have titles. Override the toString method in each class to display the corresponding class name and person name.
    Draw UML diagrams of these classes and implement them. Write a test program, create person, student, employee, faculty and staff, and call their toString method.
  • Reference Code:
package chapter11;

import java.util.Date;

public class Code_02 {
    
    
    public static void main(String[] args) {
    
    
        Person person = new Person("jxh","杏园","17858990800","[email protected]");
        System.out.println(person.toString());
        Student student = new Student("cxy","杏园","11111","22222",1);
        System.out.println(student.toString());
        Employee employee = new Employee("d","杏园","11111","22222","sss",1000,new Date());
        System.out.println(employee.toString());
        Faculty faculty = new Faculty("e","杏园","11111","22222","sss",1000,new Date(),new Date(),1);
        System.out.println(faculty.toString());
        Staff staff = new Staff("f","杏园","11111","22222","sss",1000,new Date(),"经理");
        System.out.println(staff.toString());
    }
}
class Person{
    
    
    private String name;
    private String address;
    private String phoneNumber;
    private String email_address;
    Person(String name,String address,String phoneNumber,String email_address){
    
    
        this.name = name;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.email_address = email_address;
    }
    public String getThisName(){
    
    
        return this.name;
    }
    public String toString(){
    
    
        return ("Person " + this.name);
    }
}
class Student extends Person{
    
    
    final static int freshman = 1;
    final static int senior = 2;
    final static int junior = 3;
    final static int graduate = 4;
    private int studengStatus = 0;
    Student(String name,String address,String phoneNumber,String email_address,int studengStatus){
    
    
        super(name,address,phoneNumber,email_address);
        this.studengStatus = studengStatus;
    }
    @Override
    public String toString(){
    
    
        return ("Student " + this.getThisName());
    }
}
class Employee extends Person{
    
    
    private String office;
    private double salary;
    private Date acceptDate;

    Employee(String name,String address,String phoneNumber,String email_address,String office,double salary,Date acceptDate){
    
    
        super(name,address,phoneNumber,email_address);
        this.office = office;
        this.salary = salary;
        this.acceptDate = acceptDate;
    }
    @Override
    public String toString(){
    
    
        return ("Employee " + this.getThisName());
    }
}
class Faculty extends Employee{
    
    
    private Date workDate;
    private int level;
    Faculty(String name,String address,String phoneNumber,String email_address,String office,double salary,Date acceptDate,Date workDate,int level){
    
    
        super(name,address,phoneNumber,email_address,office,salary,acceptDate);
        this.level = level;
        this.workDate = workDate;
    }
    @Override
    public String toString(){
    
    
        return ("Faculty " + this.getThisName());
    }
}
class Staff extends Employee{
    
    
    private String title;
    Staff(String name,String address,String phoneNumber,String email_address,String office,double salary,Date acceptDate,String title){
    
    
        super(name,address,phoneNumber,email_address,office,salary,acceptDate);
        this.title = title;
    }
    @Override
    public String toString(){
    
    
        return ("Staff " + this.getThisName());
    }
}
  • The results show that:
Person jxh
Student cxy
Employee d
Faculty e
Staff f

Process finished with exit code 0

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/jxh1025_/article/details/109303756