Wednesday 26 October 2016

Chapter 11 Exercise 2, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

11.2 (The Person, Student, Employee, Faculty, and Staff classes) Design 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 (freshman,
sophomore, junior, or senior). Define the status as a constant. An employee has
an office, salary, and date hired. Use the MyDate class defined in Programming
Exercise 10.14 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.
Draw the UML diagram for the classes and implement them. Write a test program
that creates a Person, Student, Employee, Faculty, and Staff, and
invokes their toString() methods.

public class Person {

    protected String name;
    protected String address;
    protected String phoneNumber;
    protected String email;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Name: " + getName() + " Class: " + this.getClass().getName();
    }
}
public class Student extends Person {

    public static final String FRESHMAN = "Freshman";
    public static final String SOPHOMORE = "Sophomore";
    public static final String JUNIOR = "Junior";
    public static final String SENIOR = "Senior";

    protected String status;

    public Student(String name) {
        super(name);
    }

    public Student(String name, String status) {
        super(name);
        this.status = status;
    }

    @Override
    public String toString() {
        return "Name: " + getName() + " Class: " + this.getClass().getName();
    }
}
public class Employee extends Person {

    protected double salary;
    protected String office;
    protected MyDate dateHired;

    public Employee(String name) {
        this(name, 0, "none", new MyDate());
    }

    public Employee(String name, double salary, String office, MyDate dateHired) {
        super(name);
        this.salary = salary;
        this.office = office;
        this.dateHired = dateHired;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getOffice() {
        return office;
    }

    public void setOffice(String office) {
        this.office = office;
    }

    public MyDate getDateHired() {
        return dateHired;
    }

    public void setDateHired(MyDate dateHired) {
        this.dateHired = dateHired;
    }

    @Override
    public String toString() {
        return "Name: " + getName() + " Class: " + this.getClass().getName();
    }
}
public class Faculty extends Employee {

    public static String LECTURER = "Lecturer";
    public static String ASSISTANT_PROFESSOR = "Assistant Professor";
    public static String ASSOCIATE_PROFESSOR = "Associate Professor";
    public static String PROFESSOR = "Professor";

    protected String officeHours;
    protected String rank;

    public Faculty(String name) {
        this(name, "9-5PM", "Employee");
    }

    public Faculty(String name, String officeHours, String rank) {
        super(name);
        this.officeHours = officeHours;
        this.rank = rank;
    }

    public String getOfficeHours() {
        return officeHours;
    }

    public void setOfficeHours(String officeHours) {
        this.officeHours = officeHours;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Name: " + getName() + " Class: " + this.getClass().getName();
    }
}
public class Staff extends Employee {

    protected String title;

    public Staff(String name) {
        this(name, "none");

    }

    public Staff(String name, String title) {
        super(name);
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "Name: " + getName() + " Class: " + this.getClass().getName();
    }
}
public class Exercise_02 {

    public static void main(String[] args) {

        Person person = new Person("person");
        Student student = new Student("student");
        Employee employee = new Employee("employee");
        Faculty faculty = new Faculty("faculty");
        Staff staff = new Staff("staff");

        System.out.println(person.toString());
        System.out.println(student.toString());
        System.out.println(employee.toString());
        System.out.println(faculty.toString());
        System.out.println(staff.toString());

    }


}

8 comments :

  1. MyDate class? This class is missing

    ReplyDelete
    Replies
    1. Here is the "MyDate" Class's Code:


      import java.util.Calendar;
      import java.util.GregorianCalendar;

      public class MyDate {

      private int year;
      private int month;
      private int day;

      public MyDate() {

      GregorianCalendar cal = new GregorianCalendar();
      year = cal.get(Calendar.YEAR);
      month = cal.get(Calendar.MONTH);
      day = cal.get(Calendar.DAY_OF_MONTH);

      }

      public MyDate(long elapsedTime) {
      GregorianCalendar cal = new GregorianCalendar();
      cal.setTimeInMillis(elapsedTime);
      year = cal.get(Calendar.YEAR);
      month = cal.get(Calendar.MONTH);
      day = cal.get(Calendar.DAY_OF_MONTH);
      }

      public MyDate(int year, int month, int day) {
      this.year = year;
      this.month = month;
      this.day = day;
      }

      public int getYear() {
      return year;
      }

      public void setYear(int year) {
      this.year = year;
      }

      public int getMonth() {
      return month;
      }

      public void setMonth(int month) {
      this.month = month;
      }

      public int getDay() {
      return day;
      }

      public void setDay(int day) {
      this.day = day;
      }

      public void setDate(long elapsedTime) {
      GregorianCalendar cal = new GregorianCalendar();
      cal.setTimeInMillis(elapsedTime);
      year = cal.get(Calendar.YEAR);
      month = cal.get(Calendar.MONTH);
      day = cal.get(Calendar.DAY_OF_MONTH);
      }
      }

      Delete
    2. Not missing but mentioned

      Delete
  2. hi there can you show us the print out after the program is run . i just want to make sure that i am getting the right results from the program
    my toString print this:
    Name: PersonClass: Assigment4and5.Person
    Name: StudentClass: Assigment4and5.Student
    Name: Employee Class: Assigment4and5.Employee
    Name: Faculty Class: Assigment4and5.Faculty
    Name: Staff Class: Assigment4and5.Staff


    ReplyDelete
  3. Cannot find symbol MaData
    Where is the problem

    ReplyDelete
  4. Design 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 (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Define MyDate represent Date in mon dd and yyy format. 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.

    ReplyDelete
  5. Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee.

    (The Person, Student, Employee, Faculty, and Staff classes)

    A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and 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.

    Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.
    Can anyone please solve this problem

    ReplyDelete