Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Person Class Hierarchy
This example has most of what we have covered so far with
inheritance and also exploits polymorphism
(The constructors are not pictured in the diagram)
1
public abstract class Person implements Comparable
{
protected String lastName;
protected String firstName;
public Person()
{
lastName = "";
firstName = "";
}
public Person(String last, String first)
{
lastName = last;
firstName = first;
}
public String getName()
{
return lastName+firstName;
}
public int compareTo(Object o) // from the Comparable interface
{
String name = lastName + firstName;
String name1 = ((Person)o).lastName+((Person)o).firstName;
return name.compareTo(name1);
//uses compareTo() in the String class
}
public boolean equals(Object o) // override equals(..) inherited from Object
{
String name = lastName + firstName;
String name1 = ((Person)o).lastName+((Person)o).firstName;
return name.equals(name1); //uses equals() in the String class
}
public String toString() // override toString() inheritedfrom Object
{
return lastName+", "+firstName;
}
}
2
public abstract class Employee extends Person
{
protected String department;
public Employee()
{
super(); // call the default constructor of Person
department = "";
}
public Employee(String last, String first, String department)
{
super(last, first); // call 2-argument constructor of Person
this.department = department;
}
public String toString()
{
return super.toString() + " Department: "+ department;
//calls toString() of the parent (Person)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
public abstract class Student extends Person
{
protected String major;
protected double gpa;
public Student()
{
super();// call the default constructor of Person
major = "";
gpa = 0.0;
}
public Student(String last, String first, String major, double gpa)
{
super(last, first); // call 2-argument constructor of Person
this.major = major;
this.gpa = gpa;
}
public String toString()
{
return super.toString()+ " Major: " + major+" GPA: "+gpa;
//calls toString() of the parent (Person)
}
}
3
public class UndergradStudent extends Student // NOT abstract
{
protected int year;
public final String CODE = "u"; //constant
public UndergradStudent()
{
super(); // calls default constructor of Student
year = 2014;
}
public UndergradStudent(String last, String first, String major,
double gpa, int year)
{
super(last, first, major, gpa); // calls 4-arg constructor of Student
this.year = year;
}
public String toString()
{
return super.toString()+ " Class: "+ year;
//calls toString() of the parent (Student)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class GradStudent extends Student // NOT abstract
{
protected String advisor;
public final String CODE = "g"; //constant
public GradStudent()
{
super();// calls default constructor of Student
advisor = "";
}
public GradStudent(String last, String first, String major, double gpa,
String advisor)
{
super(last, first, major, gpa); // calls 4-arg constructor of Student
this.advisor = advisor;
}
public String toString()
{
return super.toString()+ " Advisor: ";
//calls toString() of the parent (Student)
}
}
4
public class Administration extends Employee // NOT abstract
{
protected String position;
public final String CODE = "a";
public Administration()
{
super(); // calls default constructor of Employee
position = "";
}
public Administration(String last, String first, String department, String position)
{
super(last, first, department); // calls the 3-arg constructor of Employee
this.position = position;
}
public String toString()
{
return super.toString() + " Department: "+ department+ " Position: "+ position;
//calls toString() of the parent (Employee)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Faculty extends Employee // NOT abstract
{
protected String rank;
public final String CODE = "f";
public Faculty()
{
super();// calls default constructor of Employee
rank = "";
}
public Faculty(String last, String first, String department, String rank)
{
super(last, first, department); // calls the 3-arg constructor of Employee
this.rank = rank;
}
public String toString()
{
return super.toString() + " Department: "+ department+ " Rank: "+ rank;
//calls toString() of the parent (Employee)
}
}
5
import java.util.*;
import java.io.*;
public class CollegeData
{
private Person[] p = null; // can hold any object in the hierachy
private int numPerson;
// number of items in p
private String code = "";
public CollegeData() throws IOException // default constructor
{
//The default constructor reads the data from the file "college.txt"
// and stores the data in the array p
// The first character of every record indicates whether the
// record is data for an Administrator, Faculty, Ungergrad or Grad Student
File f = new File("college.txt");
if (!f.exists())
{
System.out.println("File college.txt does not exist");
System.exit(0);
}
Scanner input = new Scanner(f);
p = new Person[100];
numPerson = 0;
while(input.hasNext())
{
code = input.next();
// read the first letter (code) in each person's record
if (code.equals("a")) // "a" is code for Administrator
{
// read the data and instantiate and Administration object
String last = input.next();
String first = input.next();
String department = input.next();
String position = input.next();
p[numPerson] = new Administration(last, first, department, position);
}
else if (code.equals("f")) // Faculty --"f" is code for Faculty
{
// read the data and instantiate a Faculty aobject
String last = input.next();
String first = input.next();
String department = input.next();
String rank = input.next();
p[numPerson] = new Faculty(last, first, department, rank);
}
6
else if (code.equals("g")) //GradStudent
{
// read the data and instantiate a GradStudent object
String last = input.next();
String first = input.next();
String major = input.next();
double gpa = input.nextDouble();
String advisor = input.next();
p[numPerson] = new GradStudent(last, first, major,gpa, advisor);
}
else // Undergrad -- last code is "u"
{
// read the data and instantiate a UndergradStudent object
String last = input.next();
String first = input.next();
String major = input.next();
double gpa = input.nextDouble();
int year = input.nextInt();
p[numPerson] = new UndergradStudent(last, first, major, gpa, year);
}
numPerson++;
}
SelectionSort.sort(p,numPerson);
// sort the array p using the static method in SelectionSort
// sorting is based on lastName+FirstName
// since compareTo(...) uses that criterion to compare objects
}
7
public String menu()
{
// Asks the user for the class of Persons to display
// returns the user's choice
String code= "";
Scanner input = new Scanner(System.in);
System.out .println("Enter one of the following codes");
System.out.println("P --> All persons at the college");
System.out.println("E --> All employees at the college");
System.out.println("S --> All students at the college");
System.out.println("A --> All administrators at the college");
System.out.println("F --> All faculty at the college");
System.out.println("G --> All grad students at the college");
System.out.println("U --> All employees at the college");
boolean validCode = false;
while( !validCode) // stay in loop until a valid code is entered
{
code = input.next();
if (!"PESAFGU".contains(code))
System.out.println("Invalid code. Re-enter P E S A F G or U:");
else
validCode = true;
}
return code;
}
public void display(String code)
{
// Accepts the code of the user and
// traverses the array printing thise records\
// that match the code entered by the uses
for(int i = 0; i < numPerson; i++)
{
if (code.equals("P") || // everyone
code.equals("E") && p[i] instanceof Employee || //Admins and Faculty
code.equals("S") && p[i] instanceof Student || //undergrads and grads
code.equals("G") && p[i] instanceof GradStudent || // just grads
code.equals("U") && p[i] instanceof UndergradStudent || // undergrads
code.equals("A") && p[i] instanceof Administration || // administrators
code.equals("E") && p[i] instanceof Faculty) // just faculty
System.out.println(p[i]);
// calls p[i].toString()
}
}
8
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(System.in);
String answer = "";
CollegeData data = new CollegeData();
do
{
String choice = data.menu();
data.display(choice);
System.out.print("\n\nRun again? Enter Y or y for yes; any char for no: ");
answer = input.next();
} while( answer.equals("y") || answer.equals("Y"));
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is a sample input file: college.txt
The first character on each line denotes they type of object or record -f --> Faculty
a --> Administration
u --> UndergradStusent
g--> GradStudent
f Simpson Homer Math Assistant-Professor
f Krabapple Edna English Professor
f Bouvier Patty Health Associate-Professor
f Beardly Jasper Spelling Instructor
f Amadopolis Ari Classics Assistant-Professor
u Simpson Bart Philosophy 2.1 2015
u Simpson Lisa Math 4.0 2017
u Simpson Maggie History 2.7 2018
u Muntz Nelson PhyEd 2.0 2015
u Bouvier Selma Physics 2.1 2014
g Flanders Ned Art 3.2 Itchy
g Simpson Maggie Cosmotology 3.2 Scratchy
g Barton Wendel Biology 3.1 Blinky
g Hermann Herman Military-Science 2.7 Krusty
g Gumble Barney Chemistry 1.7 Krusty
a Bailey Mary Academics VP
a Borton Wendell Finance Bursar
a Chalmers Gary Academics Dean
a Lovejoy Timothy Campus-Ministry Director
a Brockman Kent Student-Affairs Housing-Director
9