Question
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;
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
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
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
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]));
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started