Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class Person that implements the Comparable interface. A person has a n

ID: 3910415 • Letter: C

Question

Create a class Person that implements the Comparable interface. A person has a name and a year of birth. You should provide a constructor that takes in the name as a string and the year of birth as an int. You should provide accessor methods for both fields and a toString method. Your compareTo method should compare by birth year.

Once that is complete make two additional classes: Student and Instructor. Both of these classes should extend Person. Students have a major (store it as a string) and Instructors have a salary (store it as an int). Create the appropriate constructors for Student and Instructor (such that they take in and populate all the inherited fields from Person as well as their additional field). Make sure to update your toString method as needed and provide an extra accessor method for the added property.

Supply a PersonTester class that creates and ArrayList typed as Person. Populate that array list with at least one object of type Person, one object of type Instructor, and one object of type Student.

Print out the contents of the ArrayList. Then, using the Collections sort method, sort the ArrayList by birth year and print out the resulting sorted array.

Explanation / Answer

Person.java

public class Person implements Comparable<Person> {

public int year;

public String name;

public Person(int year, String name) {

this.year = year;

this.name = name;

}

public void setYear(int year) {

this.year = year;

}

public void setName(String name) {

this.name = name;

}

public int getYear() {

return year;

}

public String getName() {

return name;

}

@Override

public String toString() {

return "Person: Year=" + year + ", Name=" + name;

}

public int compareTo(Person o) {

int val = 0;

Person c = (Person) o;

double yr = c.getYear();

if (getYear() > yr)

val = 1;

else if (getYear() < yr)

val = -1;

else if (getYear() == yr)

val = 0;

return val;

}

}

_____________________

Instructor.java

public class Instructor extends Person {

private int Salary;

public Instructor(int year, String name, int salary) {

super(year, name);

Salary = salary;

}

public int getSalary() {

return Salary;

}

public void setSalary(int salary) {

Salary = salary;

}

@Override

public String toString() {

return "Instructor: Salary=" + Salary +" "+super.toString();

}

}

____________________

Student.java

public class Student extends Person {

private String major;

public Student(int year, String name, String major) {

super(year, name);

this.major = major;

}

public String getMajor() {

return major;

}

public void setMajor(String major) {

this.major = major;

}

@Override

public String toString() {

return "Student: Major=" + major + " "+super.toString();

}

}

______________________

PersonTester.java

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

public class PersonTester {

public static void main(String[] args) {

ArrayList<Person> arl=new ArrayList<Person>();

Person p=new Person(1995,"Rahul");

Instructor i=new Instructor(2001,"Henrey",50000);

Student s=new Student(1998,"Gokul","Computer Science");

arl.add(p);

arl.add(i);

arl.add(s);

Collections.sort(arl);

System.out.println(" After Sorting Based on Birth Year :");

for(Person pe: arl)

{

System.out.println(pe.toString());

}

}

}

____________________

Output:


After Sorting Based on Birth Year :
Person: Year=1995, Name=Rahul
Student: Major=Computer Science Person: Year=1998, Name=Gokul
Instructor: Salary=50000 Person: Year=2001, Name=Henrey

___________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote