Implement the above hierarchy for a university with Java classes and their appro
ID: 3769427 • Letter: I
Question
Implement the above hierarchy for a university with Java classes and their appropriate relationship. You can go three levels deep in the hierarchy (community member, employee, student, alumnus, faculty, staff). Use your best judgment of instance variables and methods. The minimum requirements for the assignment are as follows:
All classes with no errors and submitted as a project archive
A driver class that drives the use of the classes and prints out appropriate information about the entities
Use of constructors
Use of “super” keyword
Use of “this” keyword
Use of “tostring” method
Exception handling to show use of throws exception from a method
Try-catch-block to catch exceptions
Use of at least 15 instance variables and appropriately included in the correct position within the hierarchy
Use of appropriate formatting to print output
Use the java date class for processing dates (birth date, joining/anniversary date etc.)
Use of interfaces (if any)
Display polymorphic handling of classes using array (demo as well as examples in book)
Explanation / Answer
//CommunityMember.java
public abstract class CommunityMember
{
//instance variables
private String firstName;
private String lastName;
//Constructor
public CommunityMember(String firstName, String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
//Returns first name
public String getFName()
{
return firstName;
}
//Returns last name
public String getLName()
{
return lastName;
}
@Override
public String toString() {
return "First Name : "+firstName+
"Last Name : "+lastName;
}
}
-----------------------------------------------------------------
//Student.java
public class Student extends CommunityMember
{
//instance variables
private int ID;
private double gradeAvg;
//Constructor
public Student(String firstName, String lastName, int ID, double gradeAvg)
throws Exception {
super(firstName, lastName);
//Calling setter methods
setID(ID);
setAverage(gradeAvg);
}
//Set id number
public void setID(int ID) throws Exception
{
if(ID<0 || ID >9999999)
throw new Exception("Invalid ID number");
else
this.ID=ID;
}
//Set average grade points
public void setAverage(double gradeAvg) throws Exception
{
if(gradeAvg<0.0 || gradeAvg>4.0)
throw new Exception("Invalid grade average number");
else
this.gradeAvg=gradeAvg;
}
//Returns id
public int getID()
{
return ID;
}
//Returns grade average
public double gradeAvg()
{
return gradeAvg;
}
//Override toString metod
@Override
public String toString() {
return super.toString()+"Student ID : "+ID+" Grade Average : "+gradeAvg;
}
}
-----------------------------------------------------------------
//Employee.java
public class Employee extends CommunityMember
{
//instance variables
private int ID;
private double salary;
//Employee
public Employee(String firstName, String lastName,int id, double salary)
throws Exception
{
super(firstName, lastName);
//Setter methods
setID(ID);
setSalary(salary);
}
//Set method to set ID
public void setID(int ID) throws Exception
{
if(ID<0 || ID >9999999)
throw new Exception("Invalid Employee ID number");
else
this.ID=ID;
}
//Set method to set salary
public void setSalary(double salary) throws Exception
{
if(salary<0 || salary>100000)
throw new Exception("Invalid salary number");
else
this.salary=salary;
}
//Returns ID
public int getID()
{
return ID;
}
//Returns salary
public double gradeSalary()
{
return salary;
}
//Override toString metod
@Override
public String toString() {
return super.toString()+ "Employee ID : "+ID+" Salary : "+salary;
}
}
-----------------------------------------------------------------
//Alumnus.java
public class Alumnus extends CommunityMember
{
//Set gender type
private String gender;
//Constructor to set first,last and gender type
public Alumnus(String firstName, String lastName,
String gender)
{
super(firstName, lastName);
this.gender=gender;
}
//Override toString metod
@Override
public String toString() {
return super.toString()+" Gender : "+gender;
}
}
-----------------------------------------------------------------
//Faculty.java
import java.util.Date;
public class Faculty extends Employee
{
//instance variables
private String subject;
private Date dateOfJoin;
//Constructor
public Faculty(String firstName, String lastName, int id, double salary,
String subject, Date dateOfJoin)
throws Exception
{
super(firstName, lastName, id, salary);
this.subject=subject;
this.dateOfJoin=dateOfJoin;
}
//Override toString metod
@Override
public String toString() {
return super.toString()+" Subject : "
+subject+" Date of Join : "+dateOfJoin;
}
}
-----------------------------------------------------------------
//Staff.java
public class Staff extends Employee
{
//instance variable
private String department;
//Constructor
public Staff(String firstName, String lastName, int id, double salary,
String department)
throws Exception
{
super(firstName, lastName, id, salary);
this.department=department;
}
//Override toString
@Override
public String toString() {
return super.toString()+" Department : "+department;
}
}
-----------------------------------------------------------------
/*Driver program to test the polymorphic property */
//CollegeDriver.java
import java.util.Date;
public class CollegeDriver
{
public static void main(String[] args) throws Exception
{
//Declare an array of CommunityMember
CommunityMember[] communitMember=new CommunityMember[5];
communitMember[0]=new Student("Michell", "Johnson", 1212, 4.0);
communitMember[1]=new Employee("Hellen", "Keller", 1323, 5500);
communitMember[2]=new Alumnus("Kunal", "Malhotra", "Male");
communitMember[3]=new Faculty("BN", "Achari", 4515, 45000,
"Computer Scient", new Date(1988, 5, 25));
communitMember[4]=new Staff("Mahn", "Singh", 1000, 5200, "Non-Teaching");
//Print the objects of student
System.out.println("Student Details");
System.out.println(communitMember[0].toString());
System.out.println("Employee Details");
//Print the objects of Alumini
System.out.println(communitMember[1].toString());
System.out.println("Alumnus Details");
//Print the objects of Faculty
System.out.println(communitMember[2].toString());
System.out.println("Faculty Details");
//Print the objects of Staff
System.out.println(communitMember[3].toString());
System.out.println("Staff Details");
//Print the objects of Staff
System.out.println(communitMember[4].toString());
}
}
---------------------------------------------------------------------
Sample Output:
Student Details
First Name : MichellLast Name : JohnsonStudent ID : 1212 Grade Average : 4.0
Employee Details
First Name : HellenLast Name : KellerEmployee ID : 0 Salary : 5500.0
Alumnus Details
First Name : KunalLast Name : Malhotra Gender : Male
Faculty Details
First Name : BNLast Name : AchariEmployee ID : 0 Salary : 45000.0 Subject : Computer Scient Date of Join : Mon Jun 25 00:00:00 IST 3888
Staff Details
First Name : MahnLast Name : SinghEmployee ID : 0 Salary : 5200.0 Department : Non-Teaching