Modify the assignment by adding methods to the object to sor
Modify the assignment by adding methods to the object to sort the array by last name and by zip code. The zipcode method should sort names in the same zip code by last name. Use a data file to enter your data.
Complete the above program using an arrayList.
import java.util.Scanner;
public class PP88
{
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
Person[] ind = new Person[25];
for(int i=0;i<ind.length;i++)
{
System.out.println(\"Enter first name, last name, and zip code seperated by a space\");
String firstName = scan.next();
String lastName = scan.next();
int Zip = scan.nextInt();
ind[i]= new Person(firstName, lastName, Zip);
}
System.out.println(\"List of names and zip codes: \");
for(int i=0;i<ind.length;i++);
{
System.out.println(ind[i].toString());
}
}
}
public class Person
{
String firstName, lastName;
int Zip;
public Person(String firstName,String lastName, int Zip)
{
this.firstName=firstName;
this.lastName=lastName;
this.Zip=Zip;
}
public String toString()
{
String format =\"\";
format =\"First Name: \"+firstName +\"Last Name: \" +lastName +\"Zip Code: \" +Zip;
return format;
}
}
Solution
/**
 * The java program PP88 that reads a file data.txt
 * that contains first ,last and zip codes and
 * calls the methods sort by name and sort by zip
 * and prints the results to console.
 * */
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;
 public class PP88
 {
    public static void main(String[]args)
    {
        new Scanner(System.in);
        Person[] persons = new Person[25];
        String fileName=\"data.txt\";
        int i;
        Scanner filescasnner=null;
        try {
           filescasnner=new Scanner(new File(fileName));
            for(i=0;i<persons.length;i++)
            {
               String firstName = filescasnner.next();
                String lastName = filescasnner.next();
                int Zip = filescasnner.nextInt();
                persons[i]= new Person(firstName, lastName, Zip);
            }
           
           
            filescasnner.close();
           
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }
       System.out.println(\"List of names and zip codes: \");
        System.out.printf(\"%-15s%-15s%-15s\ \",\"FirstName\",\"LastName\",\"Zip\");
         for (int j = 0; j < persons.length; j++)
        {
            System.out.println(persons[j]);
        }      
           
       System.out.println(\"List of names sort by first name\");
        //calling sortByName
        sortByName(persons);
        System.out.printf(\"%-15s%-15s%-15s\ \",\"FirstName\",\"LastName\",\"Zip\");
         for (int j = 0; j < persons.length; j++)
        {
            System.out.println(persons[j].toString());
        }      
           
        System.out.println(\"List of names sort by first name\");
        //calling sortByZipCode
        sortByZipCode(persons);
        System.out.printf(\"%-15s%-15s%-15s\ \",\"FirstName\",\"LastName\",\"Zip\");
         for (int j = 0; j < persons.length; j++)
        {
            System.out.println(persons[j].toString());
        }      
           
   }
    /**
    * Public static method that takes persons array object
    * and sorts the array by first name
    * */
    public static void sortByName(Person[] persons)
    {
        for (int i = 0; i < persons.length; i++)
        {
            for (int j = 0; j < persons.length-1; j++)
            {
                if(persons[j].getFirstName().compareTo(persons[j+1].getFirstName())>0)
                 {
                    Person temp=persons[j];
                    persons[j]=persons[j+1];
                    persons[j+1]=temp;
                }
            }
        }
    }
   /**
    * Public static method that takes persons array object
    * and sorts the array by zip code
    * */
    public static void sortByZipCode(Person[] persons)
    {
        for (int i = 0; i < persons.length; i++)
        {
            for (int j = 0; j < persons.length-1; j++)
            {
                if(persons[j].getZipCode()>persons[j+1].getZipCode())
                {
                    Person temp=persons[j];
                    persons[j]=persons[j+1];
                    persons[j+1]=temp;
                }
            }
        }
    }
}
 ------------------------------------------------------------------------------------------
//Person.java
public class Person
 {
   private String firstName, lastName;
    private int Zip;
   
    public Person(String firstName,String lastName, int Zip)
    {
        this.firstName=firstName;
        this.lastName=lastName;
        this.Zip=Zip;      
    }
   
    public String getFirstName(){
        return firstName;
    }
   public int getZipCode(){
        return Zip;
    }
   
    public String toString()
    {      
        return String.format(\"%-15s%-15s%-10d\",firstName,lastName,Zip);      
    }
 }
------------------------------------------------------------------------------------------
data.txt
Hayden Asher 1232
 Alethia Eccles 2324
 Hailey Exline 4242
 Jaleesa Nightingale 4224
 Carlene Veach 4242
 Joye Vigil 4244
 Glynda Sinha 6656
 Rita Hartin 5657
 Luis Lind 6878
 Jacquelin Chill 5675
 Roselyn Germano 5754
 Louanne Tondreau 3465
 Dessie Dials 9889
 Usha Feenstra 4535
 Isaac Stallings 3543
 Libbie Brame 7657
 Jeane Hylton 5756
 Stephine Fudge 3634
 Sabine Kain 3676
 Glayds Mcgilvray 4364
 Keven Oday 6436
 Angella Raphael 6354
 Pamala Lipinski 3576
 Mitsue Lehner 6465
 Kara Woolsey 4643
------------------------------------------------------------------------------------------
Sample Output:
List of names and zip codes:
 FirstName      LastName       Zip          
  Hayden         Asher          1232    
 Alethia        Eccles         2324    
 Hailey         Exline         4242    
 Jaleesa        Nightingale    4224    
 Carlene        Veach          4242    
 Joye           Vigil          4244    
 Glynda         Sinha          6656    
 Rita           Hartin         5657    
 Luis           Lind           6878    
 Jacquelin      Chill          5675    
 Roselyn        Germano        5754    
 Louanne        Tondreau       3465    
 Dessie         Dials          9889    
 Usha           Feenstra       4535    
 Isaac          Stallings      3543    
 Libbie         Brame          7657    
 Jeane          Hylton         5756    
 Stephine       Fudge          3634    
 Sabine         Kain           3676    
 Glayds         Mcgilvray      4364    
 Keven          Oday           6436    
 Angella        Raphael        6354    
 Pamala         Lipinski       3576    
 Mitsue         Lehner         6465    
 Kara           Woolsey        4643    
 List of names sort by first name
 FirstName      LastName       Zip          
  Alethia        Eccles         2324    
 Angella        Raphael        6354    
 Carlene        Veach          4242    
 Dessie         Dials          9889    
 Glayds         Mcgilvray      4364    
 Glynda         Sinha          6656    
 Hailey         Exline         4242    
 Hayden         Asher          1232    
 Isaac          Stallings      3543    
 Jacquelin      Chill          5675    
 Jaleesa        Nightingale    4224    
 Jeane          Hylton         5756    
 Joye           Vigil          4244    
 Kara           Woolsey        4643    
 Keven          Oday           6436    
 Libbie         Brame          7657    
 Louanne        Tondreau       3465    
 Luis           Lind           6878    
 Mitsue         Lehner         6465    
 Pamala         Lipinski       3576    
 Rita           Hartin         5657    
 Roselyn        Germano        5754    
 Sabine         Kain           3676    
 Stephine       Fudge          3634    
 Usha           Feenstra       4535    
 List of names sort by first name
 FirstName      LastName       Zip          
  Hayden         Asher          1232    
 Alethia        Eccles         2324    
 Louanne        Tondreau       3465    
 Isaac          Stallings      3543    
 Pamala         Lipinski       3576    
 Stephine       Fudge          3634    
 Sabine         Kain           3676    
 Jaleesa        Nightingale    4224    
 Carlene        Veach          4242    
 Hailey         Exline         4242    
 Joye           Vigil          4244    
 Glayds         Mcgilvray      4364    
 Usha           Feenstra       4535    
 Kara           Woolsey        4643    
 Rita           Hartin         5657    
 Jacquelin      Chill          5675    
 Roselyn        Germano        5754    
 Jeane          Hylton         5756    
 Angella        Raphael        6354    
 Keven          Oday           6436    
 Mitsue         Lehner         6465    
 Glynda         Sinha          6656    
 Luis           Lind           6878    
 Libbie         Brame          7657    
 Dessie         Dials          9889    






