In: Computer Science
Code needed in Java
The purpose of the application is to generate some reports about
the people of the university. So a user of the application should
be able to view information about the people in the university and
generate summaries. As in implementation these are separated into
two major categories, employee and students, we will be
implementing these classes.
The system should have three classes: Person,
Employee and Student.Employees
and students are subclasses of persons and therefore, they inherit
some fields and methods from the superclass Person. What Employee
class has as an extra is a field called job which is a
String value that indicates the position of the employee at the
university. The possible values are ‘Faculty’ and ‘Staff’ and the
rest of the input should be rejected. Another significant field is
UIN (you may assume it to be an integer).Students also
have a similar structure. A field called level, gives
information regarding the student. Possible values are
‘Undergraduate’ and ‘Graduate’ and the rest of the input should be
rejected. Also A number should be implemented (you may
assume A number to be a string).
Once you create these classes write your test class with main
method.
The output of such an application would look like:
Person’s first name is Triss, last name is Merigold. She
is 25 years old.
Person’s first name is Sigismund, last name is Dijkstra. He
is 37 years old. His UIN is 793942 and serves the university as a
staff.
Person’s first name is Keira, last name is Metz. She is 19
years old. Her A-number is A021318 and she is an undergraduate
student
// Test.java
class Person
{
String firstName;
String lastName;
int age;
String gender;
Person(String firstName, String lastName, int age, String gender)
{
gender = gender.toLowerCase();
if(gender != "male" && gender != "female")
throw new IllegalArgumentException("gender should be either male or female.");
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
}
public String toString()
{
String out = "Person's first name is " + this.firstName + ", last name is " + this.lastName + ".";
if(this.gender == "male")
out += " He ";
else
out += " She ";
out += "is " + this.age + " years old.";
return out;
}
}
class Employee extends Person
{
int UIN;
String job;
Employee(String firstName, String lastName, int age, String gender, int UIN , String job)
{
super(firstName, lastName, age, gender);
if(job.toLowerCase() != "faculty" && job.toLowerCase() != "staff")
throw new IllegalArgumentException("job should be either faculty or staff.");
this.UIN = UIN;
this.job = job;
}
public String toString()
{
String out = super.toString();
if(this.gender == "male")
out += " His ";
else
out += " Her ";
out += "UIN is " + this.UIN + " and serves the university as a " + this.job + ".";
return out;
}
}
class Student extends Person
{
String A_number;
String level;
Student(String firstName, String lastName, int age, String gender, String A_number, String level)
{
super(firstName, lastName, age, gender);
if(level.toLowerCase() != "graduate" && level.toLowerCase() != "undergraduate")
throw new IllegalArgumentException("level should be either graduate or undergraduate.");
this.A_number = A_number;
this.level = level;
}
public String toString()
{
String out = super.toString();
if(this.gender == "male")
out += " His ";
else
out += " Her ";
out += "A-number is " + this.A_number + " and";
if(this.gender == "male")
out += " he ";
else
out += " she ";
out += "is an " + this.level + " student.";
return out;
}
}
public class Test
{
public static void main(String args[])
{
try
{
Person p = new Person("Triss", "Merigold", 25, "female");
Employee e = new Employee("Sigismund", "Dijkstra", 37, "male", 793942, "staff");
Student s = new Student("Keira", "Metz", 19, "female", "A021318", "undergraduate");
System.out.println(p.toString());
System.out.println(e.toString());
System.out.println(s.toString());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output