In: Computer Science
Add comments to the following code:
PeopleQueue.java
import java.util.*;
public class PeopleQueue {
public static void main(String[] args)
{
PriorityQueue<Person> peopleQueue = new
PriorityQueue<>();
Scanner s = new
Scanner(System.in);
String
firstNameIn;
String lastNameIn;
int ageIn = 0;
int count = 1;
boolean done =
false;
System.out.println("Enter the first name, last name and age of 5
people.");
while(peopleQueue.size()
< 5) {
System.out.println("Enter a person");
System.out.print("First Name: ");
firstNameIn = s.nextLine();
System.out.print("Last Name: ");
lastNameIn = s.nextLine();
System.out.print("Age: ");
while(!done) {
try {
ageIn = Integer.parseInt(s.nextLine());
done = true;
} catch (NumberFormatException e) {
System.out.println("Error: Please enter a number for age");
System.out.print("Age: ");
}
}
Person person = new Person(firstNameIn, lastNameIn, ageIn);
peopleQueue.add(person);
System.out.println("Added to queue: " + firstNameIn + " " +
lastNameIn + ", Age: " + ageIn + "\n");
done = false;
}
NameCompare
nameCompare = new NameCompare();
List<Person>
people = new ArrayList(peopleQueue);
Collections.sort(people,
nameCompare);
System.out.println("Sorted by Last Name\n");
for (int i = 0; i <
people.size(); i++) {
System.out.println("Name: " + people.get(i).getFirstName() + "
"
+ people.get(i).getLastName() + ", Age: "
+ people.get(i).getAge());
}
System.out.println("\nSorted by Age and removed from
queue\n");
while
(!peopleQueue.isEmpty()) {
System.out.println("Removed Person " + count + " -" +
peopleQueue.remove());
count++;
}
}
}
Person.java
public class Person implements Comparable<Person> {
private int age;
private String firstName;
private String lastName;
public Person(String first, String last, int
theirAge) {
this.firstName =
first;
this.lastName =
last;
this.age =
theirAge;
}
public String getFirstName() {
return
this.firstName;
}
public String getLastName() {
return
this.lastName;
}
public void setLastName(String last) {
this.lastName =
last;
}
public int getAge() {
return this.age;
}
@Override
public int compareTo(Person person) {
if(this.equals(person))
return 0;
else if(getAge() <
person.getAge())
return 1;
else
return -1;
}
public String toString() {
return " Name: " +
getFirstName() + " " + getLastName() + ", Age: " + getAge();
}
}
NameCompare.java
import java.util.Comparator;
public class NameCompare implements Comparator<Person> {
public int compare(Person p1, Person p2)
{
int i =
p1.getLastName().compareTo(p2.getLastName());
if(i != 0) return
-i;
return i;
}
}
Here is the commented code for this problem. Comments are included in every important statements, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// PeopleQueue.java
import java.util.*;
public class PeopleQueue {
public static void main(String[] args) {
// creating a PriorityQueue of Person objects
PriorityQueue<Person> peopleQueue = new PriorityQueue<Person>();
// creating a Scanner for reading keyboard input
Scanner s = new Scanner(System.in);
// declaring needed variables to represent data fields of a person
String firstNameIn;
String lastNameIn;
int ageIn = 0;
int count = 1;
// inner loop controller
boolean done = false;
// asking for inputs for 5 persons
System.out
.println("Enter the first name, last name and age of 5 people.");
// looping until queue contains at least 5 records
while (peopleQueue.size() < 5) {
// prompting and getting first and last names
System.out.println("Enter a person");
System.out.print("First Name: ");
firstNameIn = s.nextLine();
System.out.print("Last Name: ");
lastNameIn = s.nextLine();
System.out.print("Age: ");
// looping until done variable is true
while (!done) {
// reading a String, converting to integer
try {
ageIn = Integer.parseInt(s.nextLine());
// parsing is successful,setting done to true, inner loop
// will not run next time
done = true;
} catch (NumberFormatException e) {
System.out.println("Error: Please enter a number for age");
System.out.print("Age: ");
}
}
// creating a person
Person person = new Person(firstNameIn, lastNameIn, ageIn);
// adding to queue
peopleQueue.add(person);
// displaying added data
System.out.println("Added to queue: " + firstNameIn + " "
+ lastNameIn + ", Age: " + ageIn + "\n");
// setting done to false for next iteration of inner loop
done = false;
}
// creating a NameCompare object
NameCompare nameCompare = new NameCompare();
// creating a List of person objects from queue
List<Person> people = new ArrayList(peopleQueue);
// using NameCompare comparator, sorting person objects by name
Collections.sort(people, nameCompare);
System.out.println("Sorted by Last Name\n");
// displaying list sorted by name
for (int i = 0; i < people.size(); i++) {
System.out.println("Name: " + people.get(i).getFirstName() + " "
+ people.get(i).getLastName() + ", Age: "
+ people.get(i).getAge());
}
// now looping and printing the list of persons sorted by age from queue
// until queue is empty. Since Person class's compareTo method compare
// by age, the elements in priority queue will be automatically sorted
// order by age
System.out.println("\nSorted by Age and removed from queue\n");
while (!peopleQueue.isEmpty()) {
System.out.println("Removed Person " + count + " -"
+ peopleQueue.remove());
count++;
}
}
}
// Person.java
public class Person implements Comparable<Person> {
/**
* attributes
*/
private int age;
private String firstName;
private String lastName;
/**
* constructor taking values for all fields
*/
public Person(String first, String last, int theirAge) {
this.firstName = first;
this.lastName = last;
this.age = theirAge;
}
/**
* getter for first name
*/
public String getFirstName() {
return this.firstName;
}
/**
* getter for last name
*/
public String getLastName() {
return this.lastName;
}
/**
* setter for last name
*/
public void setLastName(String last) {
this.lastName = last;
}
/**
* getter for age
*/
public int getAge() {
return this.age;
}
/**
* compares two Person objects by age in descending order, returns a
* negative value if this person comes before other, 0 if both have same
* ordering, positive value if other person comes before this person.
*/
@Override
public int compareTo(Person person) {
if (this.equals(person))
return 0; // same objects
else if (getAge() < person.getAge())
return 1; // this person comes after other
else
return -1; // this person comes before other
}
/**
* returns a String containing person details
*/
public String toString() {
return " Name: " + getFirstName() + " " + getLastName() + ", Age: "
+ getAge();
}
}
// NameCompare.java
import java.util.Comparator;
/**
* comparator class that compares two Person objects by last names
*/
public class NameCompare implements Comparator<Person> {
/**
* this method returns a negative value if p1 comes before p2, 0 if p1 and
* p2 have same ordering, positive value if p2 comes before p1. This method
* is used to sort Person objects by reverse alphabetical order of their
* lastnames
*/
public int compare(Person p1, Person p2) {
// comparing last names
int i = p1.getLastName().compareTo(p2.getLastName());
if (i != 0)
return -i; // i is alphabetical order, -i yields reverse
// alphabetical order
return i;
}
}