Homework Help Question & Answers

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
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 Coin

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
  • 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...

  • 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...

  • 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...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • 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...

  • Could someone provide me general java script statements that would answer these questions? These can be...

    Could someone provide me general java script statements that would answer these questions? These can be very general, I just need to know what statements to use to perform these functions so I can develop an outline. How to implement concatenated if/else statements. How to implement while loops, do while loops, and for loops. Be able to perform error checking using while loops. Define and implement classes: properties, accessors, mutators, constructors (defaults and with arguments), methods, toString method Create objects...

  • Create a data class named Automobile that implements the Comparable interface. Give the class data fields...

    Create a data class named Automobile that implements the Comparable interface. Give the class data fields for make, model, year, and price. Then add a constructor, all getters, a toString method that shows all attribute values, and implement Comparable by using the year as the criterion for comparing instances. Write a program named TestAutos that creates an ArrayList of five or six Automobiles. Use a for loop to display the elements in the ArrayList. Sort the Arraylist of autos by...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void...

    Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void +setWeight(double):void +compareName(String):boolean +compareTo(Horse):int +toString():String Team -team:ArrayList<Horse> +Team():void +addHorse(String, int, double):void +addHorse(Horse):void +averageAge():int +averageWeight():int +getHorse(int):Horse +getSize():int +findHorse(String):Horse +largestHorse():Horse +oldestHorse():Horse +removeHorse(String):void +smallestHorse():Horse +toString():String +youngestHorse():Horse HorseTeamDriver +main(String[]):void Create a class (Horse from the corresponding UML diagram) that handles names of horse, the age of the horse, and the weight of the horse. In addition to the standard accessor and mutator methods from the instance fields, the class...

Free Homework 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.