Java Programming Help. Question: Design a clas named Person and its two subclass
ID: 3566840 • Letter: J
Question
Java Programming Help.
Question:
Design a clas named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name , address, phone number, and email address (all Strings ). A Student has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable . An Employee has an office number , salary (both ints ), and a date hired. Use the MyDate class defined below to create an object for date hired: class MyDate{ private String date; //date in the form mm/dd/yy public MyDate(String date){ this.date = date; } public String getDate(){ return date; } } A Faculty object has office hours and a rank (both Strings ), while a Staff object has a title (as a String ). For the Student, Faculty, and Staff classes, create toString methods that store information about the object (in the format shown in the examples below). Test your classes in a Driver class (within the same file) that asks the user what type of object they'd to create as well as what information they'd like it to have. The program then uses the object 's toString method to print information about that object .
Explanation / Answer
class Person
{
private String name;
private String address;
private String phoneNum;
private String email;
public Person()
{
}
public Person(String name, String address, String phoneNum, String email) {
this.name = name;
this.address = address;
this.phoneNum = phoneNum;
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
}
// Student Class
class Student extends Person
{
private final String status;
public Student(String name, String address, String phoneNum, String email, String status)
{
super(name, address, phoneNum, email);
this.status = status;
}
public String getStatus() {
return status;
}
@Override
public String toString()
{
return "Name : "+getName()+" Address : "+getAddress()+" Phone Number : "+getPhoneNum()+" Email : "+getEmail()+" Status : "+getStatus();
}
}
// Employee Class
class Employee extends Person
{
private int officeNum;
private int salary;
private MyDate hired;
public Employee()
{
}
public Employee(String name, String address, String phoneNum, String email, int officeNum, int salary, MyDate hired)
{
super(name, address, phoneNum, email);
this.officeNum = officeNum;
this.salary = salary;
this.hired = hired;
}
public MyDate getHired() {
return hired;
}
public void setHired(MyDate hired) {
this.hired = hired;
}
public int getOfficeNum() {
return officeNum;
}
public void setOfficeNum(int officeNum) {
this.officeNum = officeNum;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
// MyDate CLass
class MyDate
{
private String date; //date in the form mm/dd/yy
public MyDate(String date)
{
this.date = date;
}
public String getDate()
{
return date;
}
}
// Faculty Class
class Faculty extends Employee
{
private String offHours;
private String ranks;
public Faculty(String name, String address, String phoneNum, String email, int officeNum, int salary, MyDate hired, String offHours, String ranks)
{
super(name, address, phoneNum, email, officeNum, salary, hired);
this.offHours = offHours;
this.ranks = ranks;
}
public String getOffHours() {
return offHours;
}
public void setOffHours(String offHours) {
this.offHours = offHours;
}
public String getRanks() {
return ranks;
}
public void setRanks(String ranks) {
this.ranks = ranks;
}
@Override
public String toString()
{
String temp = "";
temp += "Name : "+getName()+" Address : "+getAddress()+" Phone Number : "+getPhoneNum()+" Email : "+getEmail();
temp += " Office Number : "+getOfficeNum()+" Salary : "+getSalary()+" Hired Date : "+getHired()+" Office Hours : "+getOffHours()+" Ranks : "+getRanks();
return temp;
}
}
// Staff Class
class Staff extends Employee
{
private String title;
public Staff(String name, String address, String phoneNum, String email, int officeNum, int salary, MyDate hired, String title)
{
super(name, address, phoneNum, email, officeNum, salary, hired);
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString()
{
String temp = "";
temp += "Name : "+getName()+" Address : "+getAddress()+" Phone Number : "+getPhoneNum()+" Email : "+getEmail();
temp += " Office Number : "+getOfficeNum()+" Salary : "+getSalary()+" Hired Date : "+getHired()+" Title : "+getTitle();
return temp;
}
}
// Driver Class
public class Driver2
{
public static void main(String args[])
{
Student st = new Student("Nagendra ", "Andhra ", "9901234567 ","abc@gmail.com","senior");
//printing student information
System.out.println(st.toString());
System.out.println();
Faculty fac = new Faculty("john ","UP","1234567891 ", "john@gmail.com",89012, 25000,new MyDate("10/24/12"),"10","50");
// printing faculty information
System.out.println(fac.toString());
System.out.println();
Staff staff = new Staff("Peter ", "Delhi ", "9991234567"," peterxyz@gmail.com ",12345,10000,new MyDate("5/10/11"),"Supervisor");
// printing staff information
System.out.println(staff.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.