(The Person, Student, Employee, Faculty, and Staff classes) Design a class named
ID: 646083 • Letter: #
Question
(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, and address. A student has a class status(freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, and salary. A faculty member has office hours. A staff member has a title.
Override the toString method in each class to display the class name and available information of the person.
Explanation / Answer
package demo;
import java.util.ArrayList;
import java.util.List;
abstract class Person {
String name;
String address;
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getClass().getName();
}
}
class Employee extends Person{
String office;
String salary;
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getClass().getName();
}
public Employee(String a)
{
System.out.println(a);
}
}
class Student extends Person{
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getClass().getName();
}
public Student(String b)
{
System.out.println(b);
}
}
class Faculty extends Employee{
public Faculty(String a) {
super(a);
// TODO Auto-generated constructor stub
}
int officeHours;
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getClass().getName();
}
}
class Staff extends Employee{
public Staff(String a) {
super(a);
// TODO Auto-generated constructor stub
}
int officelittle;
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getClass().getName();
}
}
public class Test
{
public static void main(String[] args)
{
Person s1=new Student("s1 is initialized");
Person s2=new Student("s2 is intialized");
Employee faculty=new Faculty("faculty instantiated");
Employee staff=new Staff("Staff instantiated");
List<Object> all=new ArrayList<Object>();
all.add(s1);
all.add(s2);
all.add(faculty);
all.add(staff);
for (int i = 0; i < all.size(); i++) {
System.out.println(all.get(i));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.