java question (1) Cons der the Person, Student and Employee that we developed in
ID: 3745535 • Letter: J
Question
java question (1)
Cons der the Person, Student and Employee that we developed in Lab05 and the Comparable interface from the java.ut package Interface ComparableT> int compareTo(T o) You are required to modify the Person cass and implement the comparable interface. You will need to override the compareTo (Person p) method. The compar son is based on the ages. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specfied Person. Write the modified Person class in the answer box below assuming that the student and Employee have been done for you. For example: Test Result Person myListnew Person[2] myList[]new EmployeeC Susan", 43, "Bank tel0], Joe(21)] lers", 68000); myList[1] = new Person("Joe", 21); System.out.println(Arrays.toString(myList)); Employee[Susan(43), Bank tellers, 68808 Person myListnew Person[2]; myList[9]new Employee( Susan", 43, "Bank tels, 68808.08]J lers", 68000); myList[1] = new Person("Joe", 21); Arrays.sort(myList); System.out.println(Arrays.toString(myList)); [Joe(21), Employee Susan(43), Bank teller Answer: (penalty regime:0 %) 1l class Person implements Comparable Person protected String name; protected int age; 5public Person(String name, int age) this.namename; this.age age; 10 public int getAge) return age; 12 13 14 15 16 17 18 19 20 21 public, void setAge(int age) this.age - age; public String getNamet return name; public void setNome(String name) 23 24 25 this . name name public String toString return String.format , nore, age 28 29Explanation / Answer
class Person implements Comparable<Person>
{
protected String name;
protected int age;
public Person(String name,int age)
{
this.name = name;
this.age = age;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return age;
}
public String toString()
{
return String.format("%s(%d)",name,age);
}
// comparable interface
public int compareTo(Person p) {
if(age < p.age)
return -1;
else if(age == p.age)
return 0;
else
return 1;
}
}
class Employee extends Person
{
private String companyName;
private double salary;
public Employee(String name,int age,String companyName,double salary)
{
super(name,age);// call to base class constructor
this.companyName = companyName;
this.salary = salary;
}
public String toString()
{
return String.format("%s(%d), %s , %f",getName(),getAge(),companyName,salary);
}
}
class TestPerson
{
public static void main (String[] args)
{
Person[] mylist = new Person[2];
mylist[0] = new Employee("Susan",43,"Bank tellers",68000);
mylist[1] = new Person("Joe",21);
System.out.println(Arrays.toString(mylist));
}
}
Output:
[Susan(43), Bank tellers , 68000.000000, Joe(21)]
Do ask if any doubt. Please upvote
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.