Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete a simple address book. Start with an Address class that will hold five

ID: 3544094 • Letter: C

Question

Complete a simple address book. Start with an Address class that will hold five strings representing street

address, city, state and zipcode. Implement two constructors; one that takes five strings and the other is a copy

constructor. Implement appropriate getters, setters and a toString method.


Sample code (does not completely demonstrate functionality):


Address a1 = new Address("33 Colchester Avenue","Burlington","VT","05405");

Address a2 = new Address(a1);

System.out.println(a1);

a1.setZipcode("05401");

System.out.println(a1);

System.out.println(a2);



Would produce the output:


33 Colchester Avenue

Burlington, VT 05405

33 Colchester Avenue

Burlington, VT 05401

33 Colchester Avenue

Burlington, VT 05405



Next, create a Contact class that will represent a single contact in your address book. Each contact has a first

name (String), a last name (String), an address (Address object) and a phone number (String). Create

three constructors; 1) takes in a string for all fields (including the parts of the Address object). 2) Takes strings for

first name, last name, an Address object for address, and a phone number string and 3) takes only two strings

representing first and last name and initializes the remaining fields to null. Implement a toString method and

an equals method. Equality will be based solely on the name of the contact.


Sample code (does not completely demonstrate functionality):


Contact c1 = new Contact("Jackie","Horton","33 Colchester Avenue",

"Burlington","VT","05452","802-656-3330");

Address a = new Address("42 West Oak Hill", "Williston","VT","05495");

Contact c2 = new Contact("Joe","Wells",a,"802-878-0273");

Contact c3 = new Contact("Joe","Wells",a,"802-878-0273");

System.out.println(c1);

System.out.println(c2.equals(c3));

Explanation / Answer


public class Address {
String streetaddress;
String city;
String state;
String zipcode;
public Address(String a,String b,String c,String d){
    streetaddress = a;
    city = b;
    state = c;
    zipcode=d;
}
public Address(Address a){
    streetaddress = a.streetaddress;
    city=a.city;
    state=a.state;
    zipcode=a.zipcode;
}
public String getstreetaddress(){
    return streetaddress;
}
public String getcity(){
    return city;
}
public String getstate(){
    return state;
}
public String getzip(){
    return zipcode;
}
public void setzip(String a){
    zipcode=a;
}
public void setstreet(String a){
    streetaddress=a;
}
public void setcity(String a){
    city = a;
}
public void setstate(String a){
    state =a ;
    }
public String toString(){
    return streetaddress+" "+city +","+state+" "+zipcode;
}
}

==========================================================================


public class singleContact {
String fname;
String lname;
Address a;
String phnum;
public singleContact(String fname,String lname,String street,String city,String state,String zip,String fnum){
    this.fname=fname;
    this.lname = lname;
    this.phnum=fnum;
    a.city=city;
    a.streetaddress=street;
    a.state=state;
    a.zipcode=zip;
}

public singleContact(String fname,String lname,Address b,String fnum){
    this.fname=fname;
    this.lname = lname;
    this.phnum=fnum;
    a=b;
}
public singleContact(String fname,String lname){
    this.fname=fname;
    this.lname = lname;
    this.phnum=null;
    a=null;
}
public String toString (){
    return fname +" "+lname +" "+a.toString()+" "+phnum;
}
public boolean equals(singleContact c){
    if(fname==c.fname&&lname==c.lname) return true;
    return false;
}
}


======================================================================

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class AddressBook {
ArrayList<singleContact> a;
public AddressBook(){
    a=new ArrayList<singleContact>();
}
public AddressBook(String file) throws IOException{
    a=new ArrayList<singleContact>();
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream(file);
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    int i=0;
    //singleContact s=new singleContact()
    //Read File Line By Line
    String fname1= null;
    String lname1 = null;
    String street1=null;
    String city1=null;
    String state1=null;
    String zip1=null;
    String phnum1=null;
    while ((strLine = br.readLine()) != null)   {
        i++;
        StringTokenizer tokenizer1 = new StringTokenizer(strLine);
            // pull tokens out one at a time using nextToken()
           
    // Print the content on the console
        switch(i%5){
        case 1:
            fname1= tokenizer1.nextToken();
            lname1=tokenizer1.nextToken();
        case 2:
            street1= tokenizer1.nextToken();
        case 3:
            city1= tokenizer1.nextToken();
            city1 = city1.substring(0,city1.length()-1);
            state1=tokenizer1.nextToken();
        zip1=tokenizer1.nextToken();
        case 4:
             phnum1= tokenizer1.nextToken();
             i=0;
             singleContact s=new singleContact(fname1, lname1,street1,city1,state1,zip1,phnum1);
        a.add(s);
        }
    System.out.println (strLine);
    }
}
public int find(String f,String l){
    int i=0;
    for(;i<a.size();i++){
        if(a.get(i).fname==f &&a.get(i).lname==l) return i;
    }
    return -1;
   
}
public singleContact get(int i){
    return a.get(i);
}
public void displayall(){
    int i=0;
    for(;i<a.size();i++){
        System.out.println(a.get(i).fname );
        System.out.println(a.get(i).lname );
        System.out.println(a.get(i).a.streetaddress );
        System.out.println(a.get(i).a.city+"," );
        System.out.println(a.get(i).a.state );
        System.out.println(a.get(i).a.zipcode);
        System.out.println(a.get(i).phnum );
    }
}

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote