Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program is not working, what'sup? PersonSorter.java import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.ArrayList; /** Testing the Personclass with two Comparators- one for sorting

The program is not working, what'sup?




PersonSorter.java


import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
/**
Testing the Personclass with two Comparators- one for sorting by nameand for sorting byage
*/
public class PersonSorter
{
public static void main(String[] args)
{
Person[] persons = {
new Person("Elvis", "Goodyear", 56),
new Person("Stanley", "Clark", 8),
new Person("Jane", "Graff", 16),
new Person("Nancy", "Goodyear", 69),
new Person("Alice", "Goodyear", 100)
} ;
System.out.println("Initial Order:");
for (int i=0; i<4; i++) {
Person person = persons[i];
String lastName = person.getLastName();
String firstName = person.getFirstName();
int age = person.getAge();
System.out.println(lastName + ", " + firstName + ". Age:" + age);
}
System.out.println();
System.out.println("Sorted by name (alphabetically):");
//-----------Start belowhere. Todo: approximate lines of code= 6
// ToDo create asimple (inner) class that implements theComparatorinterface
// to sorttwo person objectsby name
// compare last namealphabetically. If they are equalthen compare first names
// Hint: make use of theclass String compareTo() methodto comparename strings



class NameComparator implements Comparator{  
 public int compare(Person a, Person b){
 if(a.getFirstName().compareTo(b.getFirstName())==0){
return a.getLastName().compareTo(b.getLastName());
}
 return a.getFirstName().compareTo(b.getFirstName());  
 }  
 }  

ArrayList ar=new ArrayList();

for (int i=0; i<4; i++) {
Person per = persons[i];
ar.add(new Person(per.getLastName(),per.getFirstName(),per.getAge()));
}
Collections.sort(ar,new NameComparator());
int k=0;
for(Person p: ar){
persons[k] =new Person(p.getFirstName(),p.getLastName(),p.getAge());
k++;
}  
// call Array.sort with two parameters - one passing in the array of persons
// and one passing in an object of the inner class
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
for (int i=0; i<4; i++) {
Person person = persons[i];
String lastName = person.getLastName();
String firstName = person.getFirstName();
int age = person.getAge();
System.out.println(lastName + ", " + firstName + ". Age:" + age);
}
System.out.println("Expected:Clark, Stanley. Age:8Goodyear, Alice. Age:100Goodyear, Elvis. Age:56Goodyear, Nancy. Age:69");
System.out.println();
System.out.println("Sorted by age (youngest to oldest):");
//-----------Start below here. To do:approximate lines of code= 4
// ToDo createa simple (inner) class that implementsthe Comparator interface
// to sort twoperson objects byage (youngest tooldest)
class AgeComparator implements Comparator{  
public int compare(Person a, Person b){  
if(a.getAge()==b.getAge())  
return 0;  
else if(a.getAge()>b.getAge())  
return 1;  
else  
return -1;  
}  
}  
Collections.sort(ar,new AgeComparator());
int x=0;
for(Person p: ar){
persons[x] =new Person(p.getFirstName(),p.getLastName(),p.getAge());
x++;
}
// call Array.sort with twoparameters - one passing in thearray of persons
// and one passing in anobject of the innerclass

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
for (int i=0; i<4; i++) {
Person person = persons[i];
String lastName = person.getLastName();
String firstName = person.getFirstName();
int age = person.getAge();
System.out.println(lastName + ", " + firstName + ". Age:" + age);
}
System.out.println("Expected:Clark, Stanley. Age:8Graff, Jane. Age:16Goodyear, Elvis. Age:56Goodyear, Nancy. Age:69");
}
}
/**
This class Person models a person object that has a name and age.
Notice there is no constructor other than the default one.
You are setting up the Comparable interface so that the
sorting will be from young to old (ascending).
*/

class Person
{
private String firstName;
private String lastName;
private int age;
/**
Initializes firstName, lastName and age
@param first the first name
@param last the last name
@param years the age in years
*/
public Person(String first, String last, int years)
{
firstName = first ; lastName = last ; age = years ;
}
/**
Accesses the first name
@return firstName
*/    
public String getFirstName() {
return firstName;
}
/**
Sets the first name
@param firstName the new first name
*/    
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
Accesses the last name
@return lastName
*/    
public String getLastName() {
return lastName;
}
/**
Sets the last name
@param lastName the new value.
*/    
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
Accesses the first age
@return age
*/    
public int getAge() {
return age;
}
/**
Sets the age
@param age the new age
*/    
public void setAge(int age) {
this.age = age;
}
}

Step by Step Solution

3.49 Rating (159 Votes )

There are 3 Steps involved in it

Step: 1

PersonSorterjava import javautilArrays import javautilCollections import javautilComparator import j... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intermediate Accounting

Authors: Donald E. Kieso, Jerry J. Weygandt, Terry D. Warfield

16th edition

1118742974, 978-1118743201, 1118743202, 978-1118742976

More Books

Students also viewed these Programming questions