Question

create a Person class with two fields (name(String), age(int)) create all the required methods such as...

create a Person class with two fields (name(String), age(int)) create all the required methods such as the accessors, mutators, toString(),constructors etc. Use the comparable interface and implement the compareTo method such that the person objects can be compared and sorted according to their age.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* person.java */


import java.util.*;

class Person implements Comparator<Person>, Comparable<Person> {
private String name;
private int age;
Person() {
}

Person(String n, int a) {
name = n;
age = a;
}

public String getPersonName() {
return name;
}

public int getPersonAge() {
return age;
}

// Overriding the compareTo method
public int compareTo(Person d) {
return (this.name).compareTo(d.name);
}

// Overriding the compare method to sort the age
public int compare(Person d, Person d1) {
return d.age - d1.age;
}
}

public class Example {

public static void main(String args[]) {
// Takes a list o Dog objects
List<Person> list = new ArrayList<Person>();

list.add(new Person("Jack", 33));
list.add(new Person("Java", 32));
list.add(new Person("Buddy", 40));
list.add(new Person("Rob", 84));
list.add(new Person("Kevin", 21));
Collections.sort(list); // Sorts the array list

/* for(Person a: list) // printing the sorted list of names
System.out.print(a.getPersonName() + ", ");*/

// Sorts the array list using comparator
Collections.sort(list, new Person());
System.out.println(" ");
  
for(Person a: list) // printing the sorted list of ages
System.out.print(a.getPersonName() +" : "+ a.getPersonAge() + ", ");
}
}

Expected output

==============


Kevin : 21, Java : 32, Jack : 33, Buddy : 40, Rob : 84,

Please find attached screenshot

Kevin: 21, Java 32, Jack 33, Buddy 40, Rob : 84,

Add a comment
Know the answer?
Add Answer to:
create a Person class with two fields (name(String), age(int)) create all the required methods such as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...

    Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the type int, assessedPrice of the type double. A constructor for this class should have three arguments for initializing these fields. Add accessors and mutators for all fields except a mutator for yearBuilt, and the method toString. Create a class StreetHouse which extends House and has additional fields: streetNumber of the type int, homeowner of the type String, and two neighbors: leftNeighbor and rightNeighbor, both...

  • Please create two tests classes for the code down below that use all of the methods...

    Please create two tests classes for the code down below that use all of the methods in the Transaction class, and all of the non-inherited methods in the derived class. Overridden methods must be tested again for the derived classes. The first test class should be a traditional test class named "Test1" while the second test class should be a JUnit test class named "Test2". All I need is to test the classes, thanks! Use a package that contains the...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • create a class in Java for a Plane the class will include String Data Fields for...

    create a class in Java for a Plane the class will include String Data Fields for the make, model, and year. (as well as mutators and accessors) Defined constructor and copy constructor. there must be a toString method as well as a copy method. runner program the program will create an array for Plane class. The size of the array will be set to user input. then the user will input the information into each plane object in the array....

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • The object class should be a Student with the following attributes: id: integer first name: String...

    The object class should be a Student with the following attributes: id: integer first name: String last name: String write the accessors, mutators, constructor, and toString(). In your main test class you will write your main method and do the following things: Create an array of Student objects with at least 5 students in your array. Write a sort method that must be your original work and included in the main class. The sort method will sort based on student...

  • Using C++ create a class called person. A person can have a name, address, age. For...

    Using C++ create a class called person. A person can have a name, address, age. For this class create a default constructor and parameterized constructor. Show the main as int main() { } ----------------------------------------------------- Show how you will define the class and call it in the main. Only create two instances/objects in main using the two created constructors.

  • Goal: In this lab, we will practice how to apply both the inheritance and composition concepts...

    Goal: In this lab, we will practice how to apply both the inheritance and composition concepts to write a Department class. 1. Implement the Department Class. Implement the following class: public class Department{ * The basic feature of a department */ private String deptName; private int numMajors; private Teacher[] list Teachers; //inherits from Person class private Student[] listStudents; //inherits from Person class /* Construct a department object (at least TWO constructors) */ * Accessors and mutators (one pair per each...

  • Using JAVA* Design a class named Person with fields for holding a person’s name, address, and...

    Using JAVA* Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator...

  • Define a class named Employee . Derive this class from the class Person. An employee record...

    Define a class named Employee . Derive this class from the class Person. An employee record inherits an employee's name from the class Person. In addition, an employee record contains an annual salary (represented by a single value of type double), a hire date that gives the year hired (as a single value of type int), an identification number of type int, and a department of type String. Give your class a reasonable complement of constructors, accessors, mutators, an equals...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT