Public interface Person { //Returns the email address of the person public Strin
ID: 3859786 • Letter: P
Question
Public interface Person { //Returns the email address of the person public String getEmail (): //Returns the name of the person public String getName(): //Returns a description of the person. public String getDescription(): } Create a Student class that implements the Person interface. As well as stores the students name. email, and course grade (e.g. A, B, C) in a member variable. The grade should be accessible via a get Grade method. For the implementation of getDescription return a message alone with the grade as follows "C grade student" Create a Lecturer class that implements the Person interface. This class should also store the subject that the lecturer teaches. Add a getSubiect method, and implement getDescription so that it returns a suitable message, e.g. "Teaches Java". Create a third class, Employee that implements the interface. This should also store the name of the department the Employee works in (available via getDepartment). Again, getDescription should return a suitable message. Create a class called PersonViewerTest. Implement a main method that a. Creates a PersonViewer, object using the provided classes. b. Creates instances of the Lecturer, Employee and Student classes and invokes the view method of the on each of them. Create a subclass of the PersonViewer object that has the following modifications: a. Overrides the view method, and uses the instanceof test to determine the actual type of the object (e.g. Employee), and then casts it appropriately. b. Create three methods called viewPerson, that vary by their input parameters, i.e. create one that accepts an Employee another that accepts a Lecturer and a third method that accepts a Student. c. Implement these methods so they write out to the console all information available about the objects. E.g. for an Employee write out its name.Explanation / Answer
public interface Person
{
public String getEmail();
public String getName();
public String getDescription();
}
class Student implements Person
{
private String name,email,courseGrade,description;
public Student(String name,String email,String courseGrade)
{
this.name = name;
this.email = email;
this.courseGrade = courseGrade;
}
public String getEmail()
{
return email;
}
public String getName()
{
return name;
}
public String getDescription()
{
return courseGrade+" grade Student";
}
}
class Lecturer implements Person
{
private String name,email,subject;
public Lecturer(String name,String email,String subject)
{
this.name = name;
this.email = email;
this.subject = subject;
}
public String getEmail()
{
return email;
}
public String getName()
{
return name;
}
public String getDescription()
{
return " Teaches "+subject;
}
}
class Employee implements Person
{
private String name,email,departmentName;
public Employee(String name,String email,String departmentName)
{
this.name = name;
this.email = email;
this.departmentName = departmentName;
}
public String getEmail()
{
return email;
}
public String getName()
{
return name;
}
public String getDescription()
{
return " Works in "+departmentName +" department";
}
}
class PersonViewer // class defines 3 methods for 3 diferent type of objects
{
public void viewPerson(Student s)
{
System.out.println(" Student Name : "+s.getName()+" Email : "+s.getEmail() +" Description : "+s.getDescription());
}
public void viewPerson(Employee e)
{
System.out.println(" Employee Name : "+e.getName()+" Email : "+e.getEmail() +" Description : "+e.getDescription());
}
public void viewPerson(Lecturer l)
{
System.out.println(" Lecturer Name : "+l.getName()+" Email : "+l.getEmail() +" Description : "+l.getDescription());
}
}
class PersonViewerTest
{
public static void main (String[] args)
{
PersonViewer pv = new PersonViewer();
Student s = new Student("Mathew Jones","mathew@email.com","A");
pv.viewPerson(s);
if(s instanceof Student)//check the object 's class with instanceOf
System.out.println(" s is instance of Student class");
Lecturer l = new Lecturer("James Hawking","james@email.com","Physics");
pv.viewPerson(l);
if(l instanceof Lecturer)
System.out.println(" l is instance of Lecturer class");
Employee e = new Employee("Chris Gosling","chris@email.com","Accounts");
pv.viewPerson(e);
if(e instanceof Employee)
System.out.println(" e is instance of Employee class");
}
}
output:
Student Name : Mathew Jones Email : mathew@email.com
Description : A grade Student
s is instance of Student class
Lecturer Name : James Hawking Email : james@email.com
Description :
Teaches Physics
l is instance of Lecturer class
Employee Name : Chris Gosling Email : chris@email.com
Description :
Works in Accounts department
e is instance of Employee class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.