In java need help creating an application that will have usi
In java need help creating an application that will have; using the data structure, at least 5
Contact objects.
Sort by lastname :[1]
Sort by homestate :[2]
Sort by age :[3]
Enter option or 0 to end input: 0
Exiting...
Enter option or 0 to end input: 4
Invalid entry.
- A prompt for the sort by options (with 0 being exit):
1 = Sort by last name
2 = Sort by Home State
3 = Sort by Age
Example run:
Sort by lastname :[1]
Sort by homestate :[2]
Sort by age :[3]
Enter option or 0 to end input: 1
Contact [firstname =G , lastname=A , state=WQ, age=55 ]
Contact [firstname =I , lastname=B , state=JH, age=101 ]
Contact [firstname =W , lastname=C , state=QQ, age=22 ]
Contact [firstname =A , lastname=D , state=WE, age=45 ]
Contact [firstname =A , lastname=E , state=SF, age=48 ]
I have created the class so far.
public class Contact implements Comparable<Object>{
String firstname;
String lastname;
String homestate;
Integer age;
public Contact(String firstname, String lastname, String homestate, Integer age){
this.setFirstname(firstname);
this.setLastname(lastname);
this.setHomestate(homestate);
this.setAge(age);
}
public String getFirstname(){
return firstname;
}
public void setFirstname(String firstname){
this.firstname = firstname;
}
public String getLastname(){
return lastname;
}
public void setLastname(String lastname){
this.lastname = lastname;
}
public String getHomestate(){
return homestate;
}
public void setHomestate(String homestate){
this.homestate = homestate;
}
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age = age;
}
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
-------------------------------------------------------------
OUTPUT:
A prompt for the sort by options (with 0 being exit):
1 = Sort by last name
2 = Sort by Home State
3 = Sort by Age
1
After sorting by last name:
Contact{firstname=\'G\', lastname=\'A\', homestate=\'WQ\', age=55}
Contact{firstname=\'I\', lastname=\'B\', homestate=\'JH\', age=101}
Contact{firstname=\'W\', lastname=\'C\', homestate=\'QQ\', age=22}
Contact{firstname=\'A\', lastname=\'D\', homestate=\'WE\', age=45}
Contact{firstname=\'A\', lastname=\'E\', homestate=\'SF\', age=48}
A prompt for the sort by options (with 0 being exit):
1 = Sort by last name
2 = Sort by Home State
3 = Sort by Age
2
After sorting by home state:
Contact{firstname=\'I\', lastname=\'B\', homestate=\'JH\', age=101}
Contact{firstname=\'W\', lastname=\'C\', homestate=\'QQ\', age=22}
Contact{firstname=\'A\', lastname=\'E\', homestate=\'SF\', age=48}
Contact{firstname=\'A\', lastname=\'D\', homestate=\'WE\', age=45}
Contact{firstname=\'G\', lastname=\'A\', homestate=\'WQ\', age=55}
A prompt for the sort by options (with 0 being exit):
1 = Sort by last name
2 = Sort by Home State
3 = Sort by Age
3
After sorting by age:
Contact{firstname=\'W\', lastname=\'C\', homestate=\'QQ\', age=22}
Contact{firstname=\'A\', lastname=\'D\', homestate=\'WE\', age=45}
Contact{firstname=\'A\', lastname=\'E\', homestate=\'SF\', age=48}
Contact{firstname=\'G\', lastname=\'A\', homestate=\'WQ\', age=55}
Contact{firstname=\'I\', lastname=\'B\', homestate=\'JH\', age=101}
A prompt for the sort by options (with 0 being exit):
1 = Sort by last name
2 = Sort by Home State
3 = Sort by Age
0
![In java need help creating an application that will have; using the data structure, at least 5 Contact objects. Sort by lastname :[1] Sort by homestate :[2] Sor In java need help creating an application that will have; using the data structure, at least 5 Contact objects. Sort by lastname :[1] Sort by homestate :[2] Sor](/WebImages/28/in-java-need-help-creating-an-application-that-will-have-usi-1077323-1761565242-0.webp)
![In java need help creating an application that will have; using the data structure, at least 5 Contact objects. Sort by lastname :[1] Sort by homestate :[2] Sor In java need help creating an application that will have; using the data structure, at least 5 Contact objects. Sort by lastname :[1] Sort by homestate :[2] Sor](/WebImages/28/in-java-need-help-creating-an-application-that-will-have-usi-1077323-1761565242-1.webp)