Question

In: Computer Science

In this assignment, you will write a class that implements a contact book entry. For example,...

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
The Contact class should implement the Comparable interface. More on this later.
Of course, you may implement private helper methods if it helps your implementation.
Your class should have the following public methods:
• 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 update method that allows the user to change all information. (They must change all of it).
• An overridden equals() method that can tell if one Contact is the same as another. 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:
john Doe Phone number: (111) 111-1111
2900 XYZ Avenue
city, state
• 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.

Solutions

Expert Solution

Create a file and save it as Contact.java

The class would look like this as below (I have added cooments as of to mention what each function would do:

//Create a class that implements the Comparable interface. As the comparision is with another Contact object, we specify Comparable<Contact>.
public class Contact implements Comparable<Contact>
{
    //define the private variables as required
    private String firstName;
    private String lastName;
    private String phoneNumber;
    private String streetAddress;
    private String city;
    private String state;

    //public constructor with all fields
    public Contact(String firstName, String lastName, String phoneNumber, String streetAddress, String city, String state) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.streetAddress = streetAddress;
        this.city = city;
        this.state = state;
    }

    //public constructor with name and phoneNumber
    public Contact(String firstName, String lastName, String phoneNumber) {
        //Calling another contructor and passing the supplied parameters. For others, passing empty string
        this(firstName,lastName,phoneNumber,"","","");
    }

    //Getters
    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public String getPhoneNumber(){
        return phoneNumber;
    }

    public String getStreetAddress(){
        return streetAddress;
    }

    public String getCity(){
        return city;
    }

    public String getState(){
        return state;
    }

    //update method to change information
    public void update(String firstName, String lastName, String phoneNumber, String streetAddress, String city, String state) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.streetAddress = streetAddress;
        this.city = city;
        this.state = state;
    }

    //check if the two contacts are equal
    public boolean equals(Object obj){
        if(obj instanceof Contact) {
            Contact c = (Contact)obj;
            if(this.firstName.equals(c.getFirstName()) && this.lastName.equals(c.getLastName()))
                return true;
            else
                return false;
        }
        return false;
    }

    //Contact to string
    public String toString(){
        StringBuilder builder = new StringBuilder();
        builder.append(firstName);
        builder.append(" ");
        builder.append(lastName);
        builder.append(" Phone number: ");
        builder.append(phoneNumber);
        builder.append(" ");
        builder.append(System.getProperty("line.separator")); //to create a new line
        builder.append(streetAddress);
        builder.append(System.getProperty("line.separator")); //to create a new line
        builder.append(city);
        builder.append(",");
        builder.append(state);

        return builder.toString();
    }

    public int compareTo(Contact another){
        if(!lastName.equals(another.getLastName())){
            if(lastName.compareTo(another.getLastName()) > 1)
                return 1;
            else if(lastName.compareTo(another.getLastName()) < 1)
                return -1;
        }
        else if(!firstName.equals(another.getFirstName())) {
            if (firstName.compareTo(another.getFirstName()) > 1)
                return 1;
            else if (firstName.compareTo(another.getFirstName()) < 1)
                return -1;
        }
        return 0;
    }

    //main program to run the class
    public static void main(String[] args){
        Contact contact1 = new Contact("Albert","Einstein","1111111111","101, some avenue","Some city", "NY");
        Contact contact2 = new Contact("Isaac","Newton","1231231231");
        Contact contact3 = new Contact("Albert","Einstein","1111111111");

        //Print the contact
        System.out.println(contact1);

        //Check if two contacts are equal
        System.out.println(contact1.equals(contact2)); //false
        System.out.println(contact1.equals(contact3)); //true

        //Compare two contacts
        System.out.println(contact1.compareTo(contact2)); //-1
        System.out.println(contact1.compareTo(contact3)); //0

        //update contact
        contact1.update("Albert","Einstein","1011111101","101, some avenue","Some city", "NY");
        System.out.println(contact1);

        //test for Getters
        System.out.println(contact1.getFirstName());
        System.out.println(contact1.getLastName());
        System.out.println(contact1.getPhoneNumber());
        System.out.println(contact1.getStreetAddress());
        System.out.println(contact1.getCity());
        System.out.println(contact1.getState());

    }


}


Related Solutions

in Java please For this assignment you are to write a class that supports the addition...
in Java please For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this assignment you will write a class that transforms a Postfix expression (interpreted as a...
For this assignment you will write a class that transforms a Postfix expression (interpreted as a sequence of method calls) into an expression tree, and provides methods that process the tree in different ways. We will test this using our own program that instantiates your class and calls the expected methods. Do not use another class besides the tester and the ExpressionTree class. All work must be done in the class ExpressionTree. Your class must be called ExpressionTree and have...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square shape. The class should contain a side property. Provide an __init__ method that takes the side length as an argument. Also, provide the following read-only properties: a) perimeter returns 4 × side. b) area returns side × side. c) diagonal returns the square root of the expression (2 × side2). The perimeter, area and diagonal should not have corresponding data attributes; rather, they should...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
Long Integer Addition For this assignment you are to write a class that supports the addition...
Long Integer Addition For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
For this week’s assignment, you will write a program class that has two subroutines and a...
For this week’s assignment, you will write a program class that has two subroutines and a main routine. The program should be a part of the ‘Firstsubroutines’ class and you should name your project Firstsubroutines if you are using Netbeans. Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards,...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT