Write an application that will have; using the data structure of your choice, at
ID: 3795550 • Letter: W
Question
Write an application that will have; using the data structure of your choice, at least 5 Contact objects that will be sorted using Comparators. These are the following requirements:
- A prompt for the sort by options (with 0 being exit):
Sort by lastname :[1]
Sort by home state :[2]
Sort by age :[3]
Enter option or 0 to end input: 0
Exiting...
- 1= Sort by last name
- 2 = Sort by Home State
- 3 = Sort by Age
- Also regonize an invalid entry
Sort by lastname :[1]
Sort by home state :[2]
Sort by age :[3]
Enter option or 0 to end input: 4
Invalid Entry
/*
* The Contact class will have 3 properties:
* firstname - String
* lastname - String
* homestate - String
* age - Integer
*/
public class Contact {
}
import java.util.ArrayList;
public class TestSortOptions {
public static void main(String[] args) {
ArrayList<Contact> contacts = initializeContactsArray();
promptForOption(contacts);
}
/*
* Data Initialization
*/
private static ArrayList<Contact> initializeContactsArray() {
// TODO: Initialize an array of Student objects
return null;
}
/*
* Prompt for the user to enter their option from the keyboard
*
* 1 = Sort by last name
* 2 = Sort by Home State
* 3 = Sort by Age
* 0 = End input and exit/terminate the application
*
*/
private static void promptForOption(ArrayList<Contact> contacts) {
// TODO: Prompt and accept option input
}
/*
* Display the Contact information sorted by using the selected option from
* the above "promptForOption" method result
*/
private static void displayContacts(ArrayList<Contact> contacts) {
// TODO: Display the contents of the Contacts Array
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class TestSortOptions {
public static void main(String[] args) {
ArrayList<Contact> contacts = initializeContactsArray();
while(true){
promptForOption(contacts);
displayContacts(contacts);}
}
/*
* Data Initialization
*/
private static ArrayList<Contact> initializeContactsArray() {
System.out.println("Enter total number of contacts");
Scanner ob2=new Scanner(System.in);
int totalContact=ob2.nextInt();
ArrayList<Contact> contacts=new ArrayList<Contact>();
for(;totalContact>0;totalContact--)
{
Contact neew=new Contact();
contacts.add(neew);
}
return contacts;
}
/*
* Prompt for the user to enter their option from the keyboard
*
* 1 = Sort by last name
* 2 = Sort by Home State
* 3 = Sort by Age
* 0 = End input and exit/terminate the application
*
*/
private static void promptForOption(ArrayList<Contact> contacts) {
System.out.println("Sort By:");
System.out.println("1:Last Name");
System.out.println("2:Home State");
System.out.println("3:Age");
System.out.println("0:Exit");
Scanner ob1=new Scanner(System.in);
int test=ob1.nextInt();
if(test<=0||test>=4)
{
System.out.println("Invalid Option");
System.exit(0);
}
else
{
Contact.sortby=test;
Collections.sort(contacts);
}
}
/*
* Display the Contact information sorted by using the selected option from
* the above "promptForOption" method result
*/
private static void displayContacts(ArrayList<Contact> contacts) {
int i;
for(Contact b:contacts){
b.display();
}
}
}
class Contact implements Comparable<Contact> {
public String firstname;
public String lastname;
public String homestate;
public int age;
public static int sortby;
Contact()
{
Scanner ob1=new Scanner(System.in);
System.out.println("Enter first name");
firstname=ob1.next();
System.out.println("Enter last name");
lastname=ob1.next();
System.out.println("Enter home state");
homestate=ob1.next();
System.out.println("Enter age");
age=ob1.nextInt();
}
public void display()
{
System.out.println("First Name= "+firstname);
System.out.println("Last Name= "+lastname);
System.out.println("Home State= "+homestate);
System.out.println("Age= "+age);
}
@Override
public int compareTo(Contact o) {
switch (sortby) {
case 1: // Sort by last name
return lastname.compareTo(o.lastname);
case 2: // Sort by home state
return homestate.compareTo(o.homestate);
case 3: // Sort by age
return Integer.compare(age, o.age);
default: // Sort by grade by default
return 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.