Assignment 1 Create a public class Person that implements th

Assignment

1. Create a public class Person that implements the Comparable interface

a. Instance V ariables

private int id;

private String name;

private String street;

private String city;

private String state;

private String phoneNumber;

b. Constructors:

i. public Person(int num,String n,String s,String c, String st, String p)

ii. public Person(String n) (initialize the name and sets the other values to null or 0 as appropriate)

iii. public Person(int n) (initialize the id number and sets the other values to null)

c. Methods

i. publicStringtoString()(whichconcatenatesallPersoninstance variables)

ii. public int getID() (accessor method for the id instance variable)

d. The Comparable Interface

i. Comparable is a generic interface (as of Java 5): Comparable<Person>

ii. public int compareTo(Person person)

iii. Compare two Person objects by name using the String compareTo method (this is done in your compareTo method) your method should return the value returned by the String method

2. Create a class named CompareInt

a. implements java.util.Comparator<Person> (see p. 212)

b. Defines a single method: public int compare(Person p1, Person p2) that compares two Person objects by id (call the getID method defined in class Person)

3. Complete the SortPerson.java class below (edit points are noted with comments). Review the sort and binary Search methods defined in both the Arrays and Collections classes of the util package and use the appropriate class to complete the assignment. Be sure to read the comments at the top of the file.

a. Use (i.e., call) the sort method to sort the list of Person objects alphabetically by name (use the single parameter sort method)

b. Use the class binarySearch method to search for a name entered on the command line. The method requires two parameters: the list and the key. The key is a Person object that only contains a name

c. Call the sort method to sort the list of Person objects numerically by id (use the two parameter version; the first is an list and the second is a comparator)

d. Use the binarySearch method to search for an id entered on the command line. The method requires three parameters: the list to search, a key, and the comparator

//   Run the following main like this:

//       java SortPerson \"Isaac Newton\" 301

//   Note that the quotation marks are required!

//

// Setting Command Line Arguments in the Eclipse IDE:

// 1. \"Run\" (top menu) -> \"Run Configurations...\"

// 2. Select the \"(x)= Arguments\" tab

// 3. Add the command line arguments in the \"Program arguments:\" text area

// 4. Press \"Apply\"

import   java.util.*;

public class SortPerson

{

   private   ArrayList<Person>   people = new ArrayList<Person>();

   public SortPerson()

   {

       people.add(new Person(301, \"Albert Einstein\", \"123 My Street\", \"Your Town\", \"UT\", \"123-4567\"));

       people.add(new Person(860, \"John Smith\", \"867 Elm St.\", \"Lake Forest\", \"AZ\", \"555-6543\"));

       people.add(new Person(51, \"Cranston Snort\", \"1600 Pennsylvania Ave\", \"Washington\", \"DC\", \"1-800-123-4783\"));

       people.add(new Person(602, \"Fred Wally\", \"123 E. Wilson\", \"Sunset\", \"UT\", \"678-4351\"));

       people.add(new Person(857, \"Isaac Newton\", \"1234 W. 900 S.\", \"Salt Lake City\", \"UT\", \"563-4567\"));

       people.add(new Person(403, \"Wilson\", \"1492 USA Way\", \"Morristown\", \"NJ\", \"345-8765\"));

       people.add(new Person(4567, \"John Smith\", \"417 El Toro\", \"Irvine\", \"CA\", \"869-3482\"));

   }

   public void sortByName(String name)

   {

       // sort list people by name here

       ____________________________________  

       for (Person p : people)               // print sorted list

           System.out.println(p);

       System.out.println(\"\ Searching for:\");

       // make key based on search name

       __________________________________________

       // add natural (by name in this case) search code here

       __________________________________________

       if (index >= 0)

           System.out.println(people.get(index));

       else

           System.out.println(name + \" was not found\");

System.out.println();               // print a blank line

   }

   public void sortByID(int id)

   {

       // make a int-based compartor here

       ____________________________________

       // sort list people by id here

       ____________________________________

       for (Person p : people)               // print sorted list

           System.out.println(p);

       System.out.println(\"\ Searching for:\");

       // make key based on id

       _____________________________________

       // add comparator-based search code here

       if (index >= 0)

           System.out.println(people.get(index));

       else

           System.out.println(id + \" was not found\");

   }

   public static void main(String args[])

   {

       SortPerson   sp = new SortPerson();

       sp.sortByName(args[0]);

       sp.sortByID(Integer.parseInt(args[1]));

   }

}

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

############ Person.java ###############

public class Person implements Comparable<Person>{

  

   // instance variables

   private int id;

   private String name;

   private String street;

   private String city;

   private String state;

   private String phoneNumber;

  

   public Person(int num,String n,String s,String c, String st, String p){

       id = num;

       name = n;

       street = s;

       city = c;

       state = st;

       phoneNumber = p;

   }

  

   public Person(String n){

       id = 0;

       name = n;

       street = null;

       city = null;

       state = null;

       phoneNumber = null;

   }

  

   public Person(int n) {

       id = n;

       name = null;

       street = null;

       city = null;

       state = null;

       phoneNumber = null;

   }

   public String toString(){

       return id+\" \"+name+\" \"+street+\" \"+

               city+\" \"+state+\" \"+phoneNumber;

   }

  

   public int getID(){

       return id;

   }

  

   public int compareTo(Person person){

       return name.compareTo(person.name);

   }

}

############# CompareInt.java ##########

import java.util.Comparator;

public class CompareInt implements Comparator<Person> {

   @Override

   public int compare(Person o1, Person o2) {

       if(o1.getID() < o2.getID())

           return -1;

       else if(o1.getID() > o2.getID())

           return 1;

       else

           return 0;

   }

}

############ SortPerson.java #############

import java.util.*;

public class SortPerson

{

   private ArrayList<Person> people = new ArrayList<Person>();

   private static Scanner input = new Scanner(System.in);

   public SortPerson()

   {

       people.add(new Person(301, \"Albert Einstein\", \"123 My Street\", \"Your Town\", \"UT\", \"123-4567\"));

       people.add(new Person(860, \"John Smith\", \"867 Elm St.\", \"Lake Forest\", \"AZ\", \"555-6543\"));

       people.add(new Person(51, \"Cranston Snort\", \"1600 Pennsylvania Ave\", \"Washington\", \"DC\", \"1-800-123-4783\"));

       people.add(new Person(602, \"Fred Wally\", \"123 E. Wilson\", \"Sunset\", \"UT\", \"678-4351\"));

       people.add(new Person(857, \"Isaac Newton\", \"1234 W. 900 S.\", \"Salt Lake City\", \"UT\", \"563-4567\"));

       people.add(new Person(403, \"Wilson\", \"1492 USA Way\", \"Morristown\", \"NJ\", \"345-8765\"));

       people.add(new Person(4567, \"John Smith\", \"417 El Toro\", \"Irvine\", \"CA\", \"869-3482\"));

   }

   public void sortByName(String name)

   {

       // sort list people by name here

       //____________________________________

       Collections.sort(people);

      

       for (Person p : people) // print sorted list

           System.out.println(p);

       System.out.println(\"\ Searching for:\");

       // make key based on search name

       //__________________________________________

       Person search = new Person(name);

       // add natural (by name in this case) search code here

       //__________________________________________

       int index = Collections.binarySearch(people, search);

       if (index >= 0)

           System.out.println(people.get(index));

       else

           System.out.println(name + \" was not found\");

       System.out.println(); // print a blank line

   }

   public void sortByID(int id)

   {

       // make a int-based compartor here

       //____________________________________

       CompareInt comp = new CompareInt();

       // sort list people by id here

       //____________________________________

       Collections.sort(people, comp);

       for (Person p : people) // print sorted list

           System.out.println(p);

       System.out.println(\"\ Searching for:\");

       // make key based on id

       Person search = new Person(id);

       //_____________________________________

       // add comparator-based search code here

       int index = Collections.binarySearch(people, search, comp);

       if (index >= 0)

           System.out.println(people.get(index));

       else

           System.out.println(id + \" was not found\");

   }

   public static void main(String args[])

   {

       SortPerson sp = new SortPerson();

       sp.sortByName(args[0]);

       sp.sortByID(Integer.parseInt(args[1]));

   }

}

/*

Sample run:

301 Albert Einstein 123 My Street Your Town UT 123-4567

51 Cranston Snort 1600 Pennsylvania Ave Washington DC 1-800-123-4783

602 Fred Wally 123 E. Wilson Sunset UT 678-4351

857 Isaac Newton 1234 W. 900 S. Salt Lake City UT 563-4567

860 John Smith 867 Elm St. Lake Forest AZ 555-6543

4567 John Smith 417 El Toro Irvine CA 869-3482

403 Wilson 1492 USA Way Morristown NJ 345-8765

Searching for:

602 Fred Wally 123 E. Wilson Sunset UT 678-4351

51 Cranston Snort 1600 Pennsylvania Ave Washington DC 1-800-123-4783

301 Albert Einstein 123 My Street Your Town UT 123-4567

403 Wilson 1492 USA Way Morristown NJ 345-8765

602 Fred Wally 123 E. Wilson Sunset UT 678-4351

857 Isaac Newton 1234 W. 900 S. Salt Lake City UT 563-4567

860 John Smith 867 Elm St. Lake Forest AZ 555-6543

4567 John Smith 417 El Toro Irvine CA 869-3482

Searching for:

403 Wilson 1492 USA Way Morristown NJ 345-8765

*/

Assignment 1. Create a public class Person that implements the Comparable interface a. Instance V ariables private int id; private String name; private String s
Assignment 1. Create a public class Person that implements the Comparable interface a. Instance V ariables private int id; private String name; private String s
Assignment 1. Create a public class Person that implements the Comparable interface a. Instance V ariables private int id; private String name; private String s
Assignment 1. Create a public class Person that implements the Comparable interface a. Instance V ariables private int id; private String name; private String s
Assignment 1. Create a public class Person that implements the Comparable interface a. Instance V ariables private int id; private String name; private String s
Assignment 1. Create a public class Person that implements the Comparable interface a. Instance V ariables private int id; private String name; private String s
Assignment 1. Create a public class Person that implements the Comparable interface a. Instance V ariables private int id; private String name; private String s
Assignment 1. Create a public class Person that implements the Comparable interface a. Instance V ariables private int id; private String name; private String s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site