Need SOON PLEASE and without having to ADD sections after, ENTIRE code please! T
ID: 3673331 • Letter: N
Question
Need SOON PLEASE and without having to ADD sections after, ENTIRE code please! Thank you!
In JAVA; Write an application that will have; using the data structure of your choice, at least 5 Contact objects. These are the follwing requirements:
1. A promt for the sort by options ( with 0 being an exit)
2. 1 = Sort by last name.
2 = Sort by Home State.
3 = Sort by Age
3. Reconize an invalid entry i.e. 4 = Invalid entry.
· 1 =sor 1 = Sort by last name · 2 = Sort by Home State ·3 = Sort by Age Sort by lastname 1] Sort by home state [2] Sort by age Enter option or 0 to end imput: 1 ontact [firstname-Adam, lastname-Ant, homestate-MI, age-14] Contact [firstname-Bill, lastname-Barnes, homestate-0H, age-62] Contact [firstname=Jane, lastname-Doe , homestate-CA, age-41] Contact [firstname-Joe, lastname-Jones, homestate-IL, age-35] Contact [firstname-Ida, lastname-Know, homestate-FL, age-23]Explanation / Answer
Please find the required classes:
public class Contact {
String lastName;
String firstName;
String homeState;
int age;
public Contact(String lastName, String firstName, String homeState, int age) {
this.lastName = lastName;
this.firstName = firstName;
this.homeState = homeState;
this.age = age;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getHomeState() {
return homeState;
}
public void setHomeState(String homeState) {
this.homeState = homeState;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "[First name:" + firstName + ", Last Name:" + lastName
+ ", age:" + age + ", home state:" + homeState;
}
}
-------------------------------------
import java.util.Comparator;
public class AgeComparator implements Comparator<Contact> {
@Override
public int compare(Contact s1, Contact s2) {
if (s1.age == s2.age)
return 0;
else if (s1.age > s2.age)
return 1;
else
return -1;
}
}
-------------------------------------------------
import java.util.Comparator;
public class NameComparator implements Comparator<Contact> {
@Override
public int compare(Contact o1, Contact o2) {
Contact s1 = (Contact) o1;
Contact s2 = (Contact) o2;
return s1.firstName.compareTo(s2.firstName);
}
}
-----------------------------------------------------
import java.util.Comparator;
public class StateComparator implements Comparator<Contact> {
@Override
public int compare(Contact o1, Contact o2) {
Contact s1 = (Contact) o1;
Contact s2 = (Contact) o2;
return s1.homeState.compareTo(s2.homeState);
}
}
-----------------------------------------
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class TestComparator {
public static void main(String[] args) {
List<Contact> contactList = new ArrayList<Contact>();
Contact rag = new Contact("k", "raghu", "IN", 25);
Contact rams = new Contact("s", "rams", "JB", 24);
Contact sra = new Contact("g", "sravan", "LM", 21);
Contact ran = new Contact("m", "ranjit", "KD", 30);
contactList.add(rag);
contactList.add(rams);
contactList.add(sra);
contactList.add(ran);
System.out.println("Input List");
System.out.println(contactList);
System.out.println("Sort by last name:[1]");
System.out.println("Sort by Home State:[2]");
System.out.println("Sort by Age:[3]");
System.out.println("Enter Input:");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
switch (i) {
case 1:
System.out.println("Sort by Last name:");
Collections.sort(contactList, new NameComparator());
break;
case 2:
System.out.println("Sort by Home state");
Collections.sort(contactList, new StateComparator());
break;
case 3:
System.out.println("Sort by Age");
Collections.sort(contactList, new AgeComparator());
break;
default:
System.out.println("Invalid Input");
}
System.out.println(contactList);
input.close();
}
}
Sample output:
Input List
[[First name:raghu, Last Name:k, age:25, home state:IN, [First name:rams, Last Name:s, age:24, home state:JB, [First name:sravan, Last Name:g, age:21, home state:LM, [First name:ranjit, Last Name:m, age:30, home state:KD]
Sort by last name:[1]
Sort by Home State:[2]
Sort by Age:[3]
Enter Input:
3
Sort by Age
[[First name:sravan, Last Name:g, age:21, home state:LM, [First name:rams, Last Name:s, age:24, home state:JB, [First name:raghu, Last Name:k, age:25, home state:IN, [First name:ranjit, Last Name:m, age:30, home state:KD]
Input List
[[First name:raghu, Last Name:k, age:25, home state:IN, [First name:rams, Last Name:s, age:24, home state:JB, [First name:sravan, Last Name:g, age:21, home state:LM, [First name:ranjit, Last Name:m, age:30, home state:KD]
Sort by last name:[1]
Sort by Home State:[2]
Sort by Age:[3]
Enter Input:
3
Sort by Age
[[First name:sravan, Last Name:g, age:21, home state:LM, [First name:rams, Last Name:s, age:24, home state:JB, [First name:raghu, Last Name:k, age:25, home state:IN, [First name:ranjit, Last Name:m, age:30, home state:KD]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.