Use Java language to write this program in this assignment, you will write a cla
ID: 3745218 • Letter: U
Question
Use Java language to write this program
in this assignment, you will write a class that implements a contact book entry. For example, my iPhone (and pretty much any smartphone) has a contacts list app that allows you to store information about your friends, colleagues, and businesses. In later assignments, you will have to implement this type of app (as a command line program, of course.) For now, you will just have to implement the building blocks for this app, namely, the Contact class.
Your Contact class should have the following private data:
• The first name of the person
• The last name of the person
•The phone number of the person
• The street address of the person
• The city of the person
• The state of the person
This contact class should implement the Comparable interface. More on this clater
Of course, you may implement private helper functions if it helps your implementation.
Your class should have the following public functions:
• A constructor that initializes all the fields with information.
• A constructor that initializes only the name and phone number.
. accessor(getter) methods for all of the data members.
. an updata method that allows the user to chnage all information ( they must change all of it)
. an overridden equals() method that can tell if one contact is the same as anther.
It should have the method signature: public boolean equals(Object obj);
We will define one Contact as being the same as another contact if the first and last names both match. (Be careful! The parameter may not be a bona fide Contact!) • An overridden toString() method that creates a printable representation for a Contact object.
It should have the method signature: public String toString();
The String should be created in the following form:
<First name> <last name> Phone number: <phone number> <street address> <city> , <state>
For example my contact info would look like:
Ari Mermelstein Phone number: (718) 111-1111
2900 Bedford Avenue Brooklyn, NY
• A comparison method that looks like this:
public int compareTo(Contact another);
We will define this method in the following way:
If the last name of “another” is lexicographically first, return a positive number.
If the last name of “another” is lexicographically second, return a negative number.
If the last names are the same and the first names are also the same, return 0.
If the last names are the same and the first names are different, use the first names to determine the order.
You must also declare the fact that Contact implements the Comparable interface.
You must also write a main program that tests each of these functions and shows me that you understand how to use the Contact class in a program.
You can choose to write this main program as a stand alone class that sits in the same directory as the Contact class, or you can just make the main program the main program of the Contact class itself. It doesn’t matter much to me.
Explanation / Answer
import java.util.Arrays;
import java.util.Scanner;
public class Contact implements Comparable<Contact> {
private String firstName;
private String lastName;
private String phoneNumber;
private String streetAddress;
private String city;
private String state;
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 getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Contact(String firstName, String lastName, String phoneNumber, String streetAddress, String city,
String state) {
setFirstName(firstName);
setLastName(lastName);
setPhoneNumber(phoneNumber);
setStreetAddress(streetAddress);
setCity(city);
setState(state);
}
public Contact(String firstName, String lastName, String phoneNumber){
setFirstName(firstName);
setLastName(lastName);
setPhoneNumber(phoneNumber);
}
public void upData(){
Scanner s = new Scanner(System.in);
System.out.println("Enter First Name.");
String firstName = s.nextLine();
System.out.println("Enter Last Name.");
String lastName = s.nextLine();
System.out.println("Enter Phone Number.");
String phoneNumber = s.nextLine();
System.out.println("Enter Street Address.");
String streetAddress = s.nextLine();
System.out.println("Enter City. ");
String city = s.nextLine();
System.out.println("Enter State.");
String state = s.nextLine();
setFirstName(firstName);
setLastName(lastName);
setPhoneNumber(phoneNumber);
setStreetAddress(streetAddress);
setCity(city);
setState(state);
}
@Override
public int compareTo(Contact o) {
// TODO Auto-generated method stub
if(!o.lastName.equals(lastName)){
return lastName.compareTo(o.lastName);
}
if(!o.firstName.equals(firstName)){
return firstName.compareTo(o.firstName);
}
return 0;
}
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Contact)) {
return false;
}
Contact cc = (Contact)o;
return cc.phoneNumber == phoneNumber;
}
@Override
public String toString() {
// TODO Auto-generated method stub
String display = new String();
display = firstName +" "+lastName +" Phone Number: "+phoneNumber+" "+streetAddress +" "+city+" "+state;
return display;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Contact testContact[] = new Contact[3];
for(int i = 0;i<3;i++){
System.out.println("For contact "+(i+1));
System.out.println("Enter First Name.");
String firstName = s.nextLine();
System.out.println("Enter Last Name. ");
String lastName = s.nextLine();
System.out.println("Enter Phone Number. ");
String phoneNumber = s.nextLine();
System.out.println("Enter Street Address. ");
String streetAddress = s.nextLine();
System.out.println("Enter City. ");
String city = s.nextLine();
System.out.println("Enter State. ");
String state = s.nextLine();
testContact[i] = new Contact(firstName,lastName,phoneNumber,streetAddress,city,state); //Constructor
}
System.out.println("Updata for contact 2"); //Updata() function
testContact[2].upData();
Arrays.sort(testContact); //to test compareTo and equals
for(int i= 0;i<3;i++){ //to test toString
System.out.println(testContact[i]);
}
}
}
/*I hope it helps you in with your problem. If any problem, comment I will try to solve. All the best. Tried and tested in main. you can also try your own main*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.