Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Needs to be written in Java. The necessary class wil be included below this import java.util.*; public class SortPerson { private ArrayList people = new

Needs to be written in Java. The necessary class wil be included below this

image text in transcribed

import java.util.*;

public class SortPerson

{

private ArrayList people = new 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]));

}

}

Assignment Follow the Widget / WareHouse example program. 1. Create a public class Person that implements the Comparable interface Instance Variables 1. private int id; 11. private String name: 111. private String street: 1V. private String city: V. private String state: V1. private String phoneNumber: a 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. public String tostring ) (which concatenates all Person instance variables) ii. public int getID ) (accessor method for the id instance variable) 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 d done in your compareTo method) your method should return the value retumed by the String method 2. Create a class named CompareInt a. implements java.util.Comparator (see p. 212) b. Defines a single method: public int compare (Person pl, Ferson p2) that compares two Person objects by id (call the getID method defined in class Person) 3. Complete the SortPerson.java class from the web page (edit points are noted with comments). Review the sort andbinarySearch 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) 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 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) 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 b. c. d

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Step: 3

blur-text-image

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

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions