SlideShare a Scribd company logo
HELP IN JAVA:
Create a main method and use these input files to test that you are able to input the
Undergraduates, put them into an ArrayList, print out the list. Repeat for Graduate, Faculty,
Staff. Do it all in ONE main.
Then- Add a compareTo to each class (and any other code you need) so you can sort all your
ArrayLists and print out the results. Your final output should be all the Undergraduates (sorted),
all the Graduates (sorted), all the Faculty(sorted), and all the Staff (sorted).
To sort: Sort undergrads and grads according to ID. Sort Faculty according to Department and
Staff according to Salary.
I have Lab10.java to keep main in but it is not completed, which is what im seeking help for.
I have Person.java ; Student.java ; Employee.java ; Undergraduate.java ; Graduate.java ;
Faculty.java & Staff.java completed.
Thanks!!!!!!!
Student.java
public class Student extends Person {
private int studentNumber;
public Student(){
super(); //call to constructor of Person
studentNumber=0;
}
public Student(String initialName, int initialStudentNumber){
super(initialName); //call to constructor of Person
studentNumber=initialStudentNumber;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber;
}
public void Reset(String newName, int newNumber){
setName(newName);
this.studentNumber=newNumber;
}
public void WriteOutput(){
super.WriteOutput();
System.out.println("Student Number: "+studentNumber);
}
public boolean Equals(Student other){
return this.hasSameName(other)&&(this.studentNumber==other.studentNumber);
}
}
Person.java
public class Person {
private String name;
public Person(){
name="No name yet";
}
public Person (String initialName){
name=initialName;
}
//refactor-encapsulate field getSetter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void WriteOutput() {
System.out.println("Name: "+name);
}
public boolean hasSameName(Person other){
return this.name.equalsIgnoreCase(other.name);
//if (this.hasSameName(other))
}
}
Solution
// i sorted based on parent if you want implements every class comparable interface .put in every
type of objects in one collection and write compareto() method in every class and implement
what i showed in the code.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class Demo {
public static void main(String[] args) {
Employee e = new Faculty("marksmith", "tcs", new Date(), 25000, 8, 1);
Employee e1 = new Faculty("mark", "tcs", new Date(), 36000, 8, 1);
Employee e2 = new Faculty("smith", "tcs", new Date(), 12000, 8, 1);
Employee e3 = new Staff("davidwarner", "ibm", new Date(), 80000,
"administration");
Employee e4 = new Staff("davidwarner", "ibm", new Date(), 40000,
"registar");
Employee e5 = new Staff("davidwarner", "ibm", new Date(), 5000, "clerk");
List employees = new ArrayList();
employees.add(e);
employees.add(e1);
employees.add(e2);
employees.add(e3);
employees.add(e4);
employees.add(e5);
System.out.println("before sorting according to salary");
for (Employee emp1 : employees) {
System.out.println(emp1);
}
System.out.println("after sorting according to salary");
Collections.sort(employees);
for (Employee emp : employees) {
System.out.println(emp);
}
Student s = new Graduate("bob", 256, "G256");
Student s1 = new Graduate("alice", 25, "G25");
Student s2 = new Graduate("david", 512, "G512");
Student s3 = new UnderGraduate("ram", 5, "U5");
Student s4 = new UnderGraduate("ramesh", 890, "U890");
Student s5 = new UnderGraduate("rajesh", 212, "U212");
List students = new ArrayList();
students.add(s);
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
System.out.println("before sorting");
for (Student stu : students) {
System.out.println(stu);
}
System.out.println("after sorting according to student no");
Collections.sort(students);
for (Student stud : students) {
System.out.println(stud);
}
}
}
*****************************************************************************
*****************************************
import java.util.Date;
public class Employee extends Person implements Comparable {
public String officeName;
public Date dateHired;
public double salary;
/**
* @param initialName
* @param officeName
* @param dateHired
* @param salary
*/
public Employee(String initialName, String officeName, Date dateHired,
double salary) {
super(initialName);
this.officeName = officeName;
this.dateHired = dateHired;
this.salary = salary;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Employee [officeName=" + officeName + ", dateHired="
+ dateHired + ", salary=" + salary + ", name=" + name + "]";
}
@Override
public int compareTo(Employee o) {
// to sort the employees according to salary
return (int) (this.salary - o.salary);
}
}
*****************************************************************************
************************
public class Person {
public String name;
public Person() {
name = "No name yet";
}
public Person(String initialName) {
name = initialName;
}
// refactor-encapsulate field getSetter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void WriteOutput() {
System.out.println("Name: " + name);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
*****************************************************************************
*********************************
import java.util.Date;
public class Faculty extends Employee {
public int officeHours;
public int rank;
/**
* @param initialName
* @param officeName
* @param dateHired
* @param salary
* @param officeHours
* @param rank
*/
public Faculty(String initialName, String officeName, Date dateHired,
double salary, int officeHours, int rank) {
super(initialName, officeName, dateHired, salary);
this.officeHours = officeHours;
this.rank = rank;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Faculty [officeHours=" + officeHours + ", rank=" + rank
+ ", officeName=" + officeName + ", dateHired=" + dateHired
+ ", salary=" + salary + "]";
}
}
*****************************************************************************
**************************************************
import java.util.Date;
public class Staff extends Employee {
public String title;
/**
* @param initialName
* @param officeName
* @param dateHired
* @param salary
* @param title
*/
public Staff(String initialName, String officeName, Date dateHired,
double salary, String title) {
super(initialName, officeName, dateHired, salary);
this.title = title;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Staff [title=" + title + ", officeName=" + officeName
+ ", dateHired=" + dateHired + ", salary=" + salary + ", name="
+ name + "]";
}
}
*****************************************************************************
*****************************************
//actually not required to write hashcode() and equals() methods .for knowing purpose i
implemented those methods
public class Student extends Person implements Comparable {
protected int studentNumber;
public Student() {
studentNumber = 0;
}
public Student(String initialName, int initialStudentNumber) {
super(initialName); // call to constructor of Person
studentNumber = initialStudentNumber;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber;
}
public void Reset(String newName, int newNumber) {
setName(newName);
this.studentNumber = newNumber;
}
public void WriteOutput() {
super.WriteOutput();
System.out.println("Student Number: " + studentNumber);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + studentNumber;
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (studentNumber != other.studentNumber)
return false;
return true;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Student [studentNumber=" + studentNumber + ", name=" + name
+ "]";
}
@Override
public int compareTo(Student student) {
// to sort the students using studentno
return this.studentNumber - student.studentNumber;
}
}
*****************************************************************************
*************************************
public class Graduate extends Student {
protected String gruaduateId;
/**
* @param initialName
* @param initialStudentNumber
* @param gruaduateId
*/
public Graduate(String initialName, int initialStudentNumber,
String gruaduateId) {
super(initialName, initialStudentNumber);
this.gruaduateId = gruaduateId;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Graduate [gruaduateId=" + gruaduateId + ", studentNumber="
+ studentNumber + ", name=" + name + "]";
}
}
*****************************************************************************
****************************
public class UnderGraduate extends Student {
protected String id;
/**
* @param initialName
* @param initialStudentNumber
* @param id
*/
public UnderGraduate(String initialName, int initialStudentNumber, String id) {
super(initialName, initialStudentNumber);
this.id = id;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public String toString() {
return "UnderGraduate [id=" + id + ", studentNumber=" + studentNumber
+ ", name=" + name + "]";
}
}
output
before sorting according to salary
Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=25000.0]
Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=36000.0]
Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=12000.0]
Staff [title=administration, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=80000.0, name=davidwarner]
Staff [title=registar, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=40000.0, name=davidwarner]
Staff [title=clerk, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=5000.0,
name=davidwarner]
after sorting according to salary
Staff [title=clerk, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=5000.0,
name=davidwarner]
Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=12000.0]
Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=25000.0]
Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=36000.0]
Staff [title=registar, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=40000.0, name=davidwarner]
Staff [title=administration, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017,
salary=80000.0, name=davidwarner]
before sorting
Graduate [gruaduateId=G256, studentNumber=256, name=bob]
Graduate [gruaduateId=G25, studentNumber=25, name=alice]
Graduate [gruaduateId=G512, studentNumber=512, name=david]
UnderGraduate [id=U5, studentNumber=5, name=ram]
UnderGraduate [id=U890, studentNumber=890, name=ramesh]
UnderGraduate [id=U212, studentNumber=212, name=rajesh]
after sorting according to student no
UnderGraduate [id=U5, studentNumber=5, name=ram]
Graduate [gruaduateId=G25, studentNumber=25, name=alice]
UnderGraduate [id=U212, studentNumber=212, name=rajesh]
Graduate [gruaduateId=G256, studentNumber=256, name=bob]
Graduate [gruaduateId=G512, studentNumber=512, name=david]
UnderGraduate [id=U890, studentNumber=890, name=ramesh]

More Related Content

PDF
First compile all the classes.But to run the program we have to run .pdf
PDF
public class Person { private String name; private int age;.pdf
PDF
define a class name Employee whose objects are records for employee..pdf
PDF
package employeeType.employee;public abstract class Employee {  .pdf
PDF
Preexisting code, please useMain.javapublic class Main { p.pdf
PDF
Hi, I need help with a java programming project. specifically practi.pdf
PDF
Hello. Im currently working on the last section to my assignment a.pdf
PDF
Java questionI am having issues returning the score sort in numeri.pdf
First compile all the classes.But to run the program we have to run .pdf
public class Person { private String name; private int age;.pdf
define a class name Employee whose objects are records for employee..pdf
package employeeType.employee;public abstract class Employee {  .pdf
Preexisting code, please useMain.javapublic class Main { p.pdf
Hi, I need help with a java programming project. specifically practi.pdf
Hello. Im currently working on the last section to my assignment a.pdf
Java questionI am having issues returning the score sort in numeri.pdf

Similar to HELP IN JAVACreate a main method and use these input files to tes.pdf (20)

PDF
Can someone help me with this code When I run it, it stops after th.pdf
PDF
Scala 2013 review
PDF
the code is as the following- package hw1-public class Runner { public.pdf
PDF
package hw1-public class Runner { public static void main(String-- arg (1).pdf
DOCX
L11a Create the Student class derived from the Person class- A student.docx
PDF
package employeeType.employee;public class Employee {   private .pdf
PDF
In Java- Create a Graduate class derived from Student- A graduate has.pdf
PDF
java programming languageThe attached A12.txt file which has 2 col.pdf
PDF
Problem 1 Create Node class (or use what you have done in Lab4)• .pdf
PDF
You will need to develop a system that can track employee informatio.pdf
DOCX
2. Create a Java class called EmployeeMain within the same project Pr.docx
PDF
ACP Java Assignment.pdf
PPTX
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
PDF
I need help for my next project due next tuesday can you help me in .pdf
PDF
@author public class Person{   String sname, .pdf
PPT
Java naming conventions
PPT
Java naming conventions
DOCX
Assignment 7
PDF
Java The Person Student Employee Faculty and Staff clas.pdf
PDF
Add a private variable SSN to the person class- Add a private variable.pdf
Can someone help me with this code When I run it, it stops after th.pdf
Scala 2013 review
the code is as the following- package hw1-public class Runner { public.pdf
package hw1-public class Runner { public static void main(String-- arg (1).pdf
L11a Create the Student class derived from the Person class- A student.docx
package employeeType.employee;public class Employee {   private .pdf
In Java- Create a Graduate class derived from Student- A graduate has.pdf
java programming languageThe attached A12.txt file which has 2 col.pdf
Problem 1 Create Node class (or use what you have done in Lab4)• .pdf
You will need to develop a system that can track employee informatio.pdf
2. Create a Java class called EmployeeMain within the same project Pr.docx
ACP Java Assignment.pdf
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
I need help for my next project due next tuesday can you help me in .pdf
@author public class Person{   String sname, .pdf
Java naming conventions
Java naming conventions
Assignment 7
Java The Person Student Employee Faculty and Staff clas.pdf
Add a private variable SSN to the person class- Add a private variable.pdf

More from fatoryoutlets (20)

PDF
Write an informal paper that is exactly 3 pages long not counting th.pdf
PDF
Write a C program to find factorial of an integer n, where the user .pdf
PDF
When was the black body mutation in drosophila melanogaster discover.pdf
PDF
What is it that consumer researchers try to find among varying cultu.pdf
PDF
What are pros and cons of Symantec endpoint security softwareSo.pdf
PDF
All of the following describe the International Accounting Standard .pdf
PDF
The right and left sternocleidomastoid muscles of humans originate o.pdf
PDF
The code in image3.cpp has the error as shown above, could you help .pdf
PDF
the ability of the cardiac muscle cells to fire on their own is .pdf
PDF
Template LinkedList;I am using templates to make some linkedLists.pdf
PDF
Surface water entering the North Atlantic may have a temperature of .pdf
PDF
Suppose the rate of plant growth on Isle Royale supported an equilib.pdf
PDF
Q1. public void operationZ() throws StackUnderflowException {   .pdf
PDF
Print Calculator Question 18 of 20 Sapling Learning Map do ment of Lu.pdf
PDF
Part A 1. Solid product forms during the addition of the bleach solu.pdf
PDF
Only 15 of the carbon-14 in a wooden bowl remains. How old is the b.pdf
PDF
Note Use Java Write a web server that is capable of processing only.pdf
PDF
Negotiations are not usually faster in Deal focused cultures than re.pdf
PDF
9. The tort of assault and the tort of battery A) can occur independe.pdf
PDF
Jj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdf
Write an informal paper that is exactly 3 pages long not counting th.pdf
Write a C program to find factorial of an integer n, where the user .pdf
When was the black body mutation in drosophila melanogaster discover.pdf
What is it that consumer researchers try to find among varying cultu.pdf
What are pros and cons of Symantec endpoint security softwareSo.pdf
All of the following describe the International Accounting Standard .pdf
The right and left sternocleidomastoid muscles of humans originate o.pdf
The code in image3.cpp has the error as shown above, could you help .pdf
the ability of the cardiac muscle cells to fire on their own is .pdf
Template LinkedList;I am using templates to make some linkedLists.pdf
Surface water entering the North Atlantic may have a temperature of .pdf
Suppose the rate of plant growth on Isle Royale supported an equilib.pdf
Q1. public void operationZ() throws StackUnderflowException {   .pdf
Print Calculator Question 18 of 20 Sapling Learning Map do ment of Lu.pdf
Part A 1. Solid product forms during the addition of the bleach solu.pdf
Only 15 of the carbon-14 in a wooden bowl remains. How old is the b.pdf
Note Use Java Write a web server that is capable of processing only.pdf
Negotiations are not usually faster in Deal focused cultures than re.pdf
9. The tort of assault and the tort of battery A) can occur independe.pdf
Jj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdf

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Complications of Minimal Access Surgery at WLH
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Lesson notes of climatology university.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
Insiders guide to clinical Medicine.pdf
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
O7-L3 Supply Chain Operations - ICLT Program
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Complications of Minimal Access Surgery at WLH
Supply Chain Operations Speaking Notes -ICLT Program
Lesson notes of climatology university.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
2.FourierTransform-ShortQuestionswithAnswers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life

HELP IN JAVACreate a main method and use these input files to tes.pdf

  • 1. HELP IN JAVA: Create a main method and use these input files to test that you are able to input the Undergraduates, put them into an ArrayList, print out the list. Repeat for Graduate, Faculty, Staff. Do it all in ONE main. Then- Add a compareTo to each class (and any other code you need) so you can sort all your ArrayLists and print out the results. Your final output should be all the Undergraduates (sorted), all the Graduates (sorted), all the Faculty(sorted), and all the Staff (sorted). To sort: Sort undergrads and grads according to ID. Sort Faculty according to Department and Staff according to Salary. I have Lab10.java to keep main in but it is not completed, which is what im seeking help for. I have Person.java ; Student.java ; Employee.java ; Undergraduate.java ; Graduate.java ; Faculty.java & Staff.java completed. Thanks!!!!!!! Student.java public class Student extends Person { private int studentNumber; public Student(){ super(); //call to constructor of Person studentNumber=0; } public Student(String initialName, int initialStudentNumber){ super(initialName); //call to constructor of Person studentNumber=initialStudentNumber; } public int getStudentNumber() { return studentNumber; } public void setStudentNumber(int studentNumber) { this.studentNumber = studentNumber; } public void Reset(String newName, int newNumber){
  • 2. setName(newName); this.studentNumber=newNumber; } public void WriteOutput(){ super.WriteOutput(); System.out.println("Student Number: "+studentNumber); } public boolean Equals(Student other){ return this.hasSameName(other)&&(this.studentNumber==other.studentNumber); } } Person.java public class Person { private String name; public Person(){ name="No name yet"; } public Person (String initialName){ name=initialName; } //refactor-encapsulate field getSetter public String getName() { return name; } public void setName(String name) { this.name = name; } public void WriteOutput() { System.out.println("Name: "+name); } public boolean hasSameName(Person other){ return this.name.equalsIgnoreCase(other.name); //if (this.hasSameName(other)) } }
  • 3. Solution // i sorted based on parent if you want implements every class comparable interface .put in every type of objects in one collection and write compareto() method in every class and implement what i showed in the code. import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class Demo { public static void main(String[] args) { Employee e = new Faculty("marksmith", "tcs", new Date(), 25000, 8, 1); Employee e1 = new Faculty("mark", "tcs", new Date(), 36000, 8, 1); Employee e2 = new Faculty("smith", "tcs", new Date(), 12000, 8, 1); Employee e3 = new Staff("davidwarner", "ibm", new Date(), 80000, "administration"); Employee e4 = new Staff("davidwarner", "ibm", new Date(), 40000, "registar"); Employee e5 = new Staff("davidwarner", "ibm", new Date(), 5000, "clerk"); List employees = new ArrayList(); employees.add(e); employees.add(e1); employees.add(e2); employees.add(e3); employees.add(e4); employees.add(e5); System.out.println("before sorting according to salary"); for (Employee emp1 : employees) { System.out.println(emp1); } System.out.println("after sorting according to salary"); Collections.sort(employees); for (Employee emp : employees) { System.out.println(emp); }
  • 4. Student s = new Graduate("bob", 256, "G256"); Student s1 = new Graduate("alice", 25, "G25"); Student s2 = new Graduate("david", 512, "G512"); Student s3 = new UnderGraduate("ram", 5, "U5"); Student s4 = new UnderGraduate("ramesh", 890, "U890"); Student s5 = new UnderGraduate("rajesh", 212, "U212"); List students = new ArrayList(); students.add(s); students.add(s1); students.add(s2); students.add(s3); students.add(s4); students.add(s5); System.out.println("before sorting"); for (Student stu : students) { System.out.println(stu); } System.out.println("after sorting according to student no"); Collections.sort(students); for (Student stud : students) { System.out.println(stud); } } } ***************************************************************************** ***************************************** import java.util.Date; public class Employee extends Person implements Comparable { public String officeName; public Date dateHired; public double salary; /** * @param initialName * @param officeName * @param dateHired * @param salary
  • 5. */ public Employee(String initialName, String officeName, Date dateHired, double salary) { super(initialName); this.officeName = officeName; this.dateHired = dateHired; this.salary = salary; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Employee [officeName=" + officeName + ", dateHired=" + dateHired + ", salary=" + salary + ", name=" + name + "]"; } @Override public int compareTo(Employee o) { // to sort the employees according to salary return (int) (this.salary - o.salary); } } ***************************************************************************** ************************ public class Person { public String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } // refactor-encapsulate field getSetter public String getName() {
  • 6. return name; } public void setName(String name) { this.name = name; } public void WriteOutput() { System.out.println("Name: " + name); } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } /* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (name == null) { if (other.name != null)
  • 7. return false; } else if (!name.equals(other.name)) return false; return true; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Person [name=" + name + "]"; } } ***************************************************************************** ********************************* import java.util.Date; public class Faculty extends Employee { public int officeHours; public int rank; /** * @param initialName * @param officeName * @param dateHired * @param salary * @param officeHours * @param rank */ public Faculty(String initialName, String officeName, Date dateHired, double salary, int officeHours, int rank) { super(initialName, officeName, dateHired, salary); this.officeHours = officeHours; this.rank = rank; } /*
  • 8. * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Faculty [officeHours=" + officeHours + ", rank=" + rank + ", officeName=" + officeName + ", dateHired=" + dateHired + ", salary=" + salary + "]"; } } ***************************************************************************** ************************************************** import java.util.Date; public class Staff extends Employee { public String title; /** * @param initialName * @param officeName * @param dateHired * @param salary * @param title */ public Staff(String initialName, String officeName, Date dateHired, double salary, String title) { super(initialName, officeName, dateHired, salary); this.title = title; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Staff [title=" + title + ", officeName=" + officeName
  • 9. + ", dateHired=" + dateHired + ", salary=" + salary + ", name=" + name + "]"; } } ***************************************************************************** ***************************************** //actually not required to write hashcode() and equals() methods .for knowing purpose i implemented those methods public class Student extends Person implements Comparable { protected int studentNumber; public Student() { studentNumber = 0; } public Student(String initialName, int initialStudentNumber) { super(initialName); // call to constructor of Person studentNumber = initialStudentNumber; } public int getStudentNumber() { return studentNumber; } public void setStudentNumber(int studentNumber) { this.studentNumber = studentNumber; } public void Reset(String newName, int newNumber) { setName(newName); this.studentNumber = newNumber; } public void WriteOutput() { super.WriteOutput(); System.out.println("Student Number: " + studentNumber); } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */
  • 10. @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + studentNumber; return result; } /* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (studentNumber != other.studentNumber) return false; return true; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Student [studentNumber=" + studentNumber + ", name=" + name + "]"; } @Override
  • 11. public int compareTo(Student student) { // to sort the students using studentno return this.studentNumber - student.studentNumber; } } ***************************************************************************** ************************************* public class Graduate extends Student { protected String gruaduateId; /** * @param initialName * @param initialStudentNumber * @param gruaduateId */ public Graduate(String initialName, int initialStudentNumber, String gruaduateId) { super(initialName, initialStudentNumber); this.gruaduateId = gruaduateId; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Graduate [gruaduateId=" + gruaduateId + ", studentNumber=" + studentNumber + ", name=" + name + "]"; } } ***************************************************************************** **************************** public class UnderGraduate extends Student { protected String id; /** * @param initialName
  • 12. * @param initialStudentNumber * @param id */ public UnderGraduate(String initialName, int initialStudentNumber, String id) { super(initialName, initialStudentNumber); this.id = id; } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public String toString() { return "UnderGraduate [id=" + id + ", studentNumber=" + studentNumber + ", name=" + name + "]"; } } output before sorting according to salary Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=25000.0] Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=36000.0] Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=12000.0] Staff [title=administration, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=80000.0, name=davidwarner] Staff [title=registar, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=40000.0, name=davidwarner] Staff [title=clerk, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=5000.0, name=davidwarner] after sorting according to salary Staff [title=clerk, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=5000.0, name=davidwarner] Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017,
  • 13. salary=12000.0] Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=25000.0] Faculty [officeHours=8, rank=1, officeName=tcs, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=36000.0] Staff [title=registar, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=40000.0, name=davidwarner] Staff [title=administration, officeName=ibm, dateHired=Fri Feb 24 18:53:39 IST 2017, salary=80000.0, name=davidwarner] before sorting Graduate [gruaduateId=G256, studentNumber=256, name=bob] Graduate [gruaduateId=G25, studentNumber=25, name=alice] Graduate [gruaduateId=G512, studentNumber=512, name=david] UnderGraduate [id=U5, studentNumber=5, name=ram] UnderGraduate [id=U890, studentNumber=890, name=ramesh] UnderGraduate [id=U212, studentNumber=212, name=rajesh] after sorting according to student no UnderGraduate [id=U5, studentNumber=5, name=ram] Graduate [gruaduateId=G25, studentNumber=25, name=alice] UnderGraduate [id=U212, studentNumber=212, name=rajesh] Graduate [gruaduateId=G256, studentNumber=256, name=bob] Graduate [gruaduateId=G512, studentNumber=512, name=david] UnderGraduate [id=U890, studentNumber=890, name=ramesh]