QuestionQuestion

Objectives:
• Gain more experience with file I/O;
• Work with the ArrayList class;
• Learn to use the String split() method; Gain experience with creating classes; Using aggregation to create classes.

In this assignment you are to create 5 classes. Four of these are Name, Address, Phone, and Person. The fifth class is a class containing main that will utilize the four classes to accomplish a task.   
• Each Name object will have instance variables representing a first name, middle initial, and last name. All of these are Strings.
• Each Address object will have instance variables representing the first line of a street address, the second line of a street address (often empty), a city, a state postal abbreviation, and a zip code. Again all of these are Strings.
• Each Phone object will have a String of length 3 representing the area code, a String of length 3 representing the exchange, and a String representing the last four digits of the local phone number. All of these are Strings.
• Each Person object will have instance variables representing a name, an address, and a phone. (Here is where aggregation is occurring – using references to other classes within a given class.) In other words, the instance variables for Person will consist of a Name object, an Address object, and a Phone object.

Each of the four classes must contain:
• An overridden toString() method that will return a String that when printed on the standard output will result in well-formatted output. Examples:
o Name toString(): Charles L Lamb o Address toString(): 1515 North 8th Street
Apartment C printed only if this part isn’t an empty string
Nowhereville MA 78567
o Phone toString(): (800) 555 – 5555 o The Person class would print out the above information as it appears, lined up on the left.
• A method to return the information as a String in a comma separated format. The above examples would be:
o Charles,L,Lamb o 1515 North 8th Street,Apartment C,Nowhereville,MA,78567 o 800,555,5555 o The person class would return:
Charles,L,Lamb,1515 North 8th Street,Apartment C,Nowhereville,MA,78567   
The Name and Person classes must each have a compareTo() method (similar to that for Strings). For example if n1 and n2 are Name objects, then n1.compareTo(n2) would return a negative integer if n1 is less than n2, zero if they are equal, and a positive integer if n1 is greater than n2. The comparison is based on the last names if they are different; the first names if the last names are equal but the first names are different; and on the middle initial if all others are equal. The Person compareTo will merely return a value based solely on the Name.

NOTE: When reading a string of fields separated by commas, it’s a good idea in this assignment to get rid of leading and trailing white space. This will help you keep things aligned in case there exist extra blanks prior to or following the beginning of a name, address, or phone number. If str is a String, then str.trim() returns a string with leading and trailing white space removed.

Of course, all of these will need constructors. The design and development of constructors is left to the student.

Finally, main should do the following:
1. Print a message describing that a file with comma separated values will be read and stored in an ArrayList of Person objects.
2. Ask the user for the name of the file and then open it.
3. While there are more lines:
a. read a line;
b. use the String split() method to create an array of strings holding the parts separated by commas.
c. Create Name, Address, and Phone objects.
d. Using the objects in part c. create a Person object;
e. Add the person object to the ArrayList.
4. Report that the file has been read, and report how many objects were placed in the ArrayList.
5. Provide a menu driven program that will allow the user to manipulate the ArrayList. The options are:
a. Print the list in well-formatted output (this will utilize toString() methods; b. Add a person to the list;
c. Remove a person from the list. The person is to be identified only by Name.
d. Sort the list using InsertionSort. This is well suited for an ArrayList.
e. Write the Array list to a new file as a comma separated text file. Allow the user to input the new name and warn if the user is giving the same name as the original file. Let the program exit after this is done.

A sample run for the following csv file is provided below:

Fudd,Elmer,J,2000 North 8th St,,Kalamazoo,MI,49008,555,355,999
Pig, Porky, M, 11th Avenue,Apartment C,Crystal Falls,IL,65432,218,777,6666
Bunny,Bugs,L,1503 Carrot Street,,Big Hill, MN, 98765, 333,222,9678
Cat,Sylvester,C,3111 Cage Avenue,Cage 3,Honolulu,HI,33333,789,213,4444
Snerd,Mortimer,K,888 Seventeenth Street,,Paducah,TX,48848,333,333,1234
Bunny,Bugs,K,1503 Carrot Street,,Big Hill,MN,98765,333,222,9678

This program will open a file in csv format representing a directory.

Enter the name of the directory file: test.txt test.txt has been read and Person objects have been placed in an ArrayList of size 6.
What action do you wish to take?
Print the list: Enter 1:
Add a person to the list: Enter 2:
Remove a person: Enter 3:
Sort the interal ArrayList: Enter 4:   
Save the list to a new file and quit: Enter 5:

Enter 1, 2, 3, 4, or 5: 1
Elmer J Fudd
2000 North 8th St
Kalamazoo MI 49008
(555) 355-999

Porky M Pig
11th Avenue
Apartment C
Crystal Falls IL 65432
(218) 777-6666

Bugs L Bunny
1503 Carrot Street
Big Hill MN 98765
(333) 222-9678

Sylvester C Cat
3111 Cage Avenue
Cage 3
Honolulu HI 33333
(789) 213-4444

Mortimer K Snerd
888 Seventeenth Street
Paducah TX 48848
(333) 333-1234

Bugs K Bunny
1503 Carrot Street
Big Hill MN 98765
(333) 222-9678

What action do you wish to take?
Print the list: Enter 1:
Add a person to the list: Enter 2:
Remove a person: Enter 3:
Sort the interal ArrayList: Enter 4:   
Save the list to a new file and quit: Enter 5:

Enter 1, 2, 3, 4, or 5: 2
Enter first Name: Albert
Enter Middle Name: A
Enter Last Name: Anderson
Enter Address 1: 1111 South 11th Street
Enter Address 2:
Enter City: Irontown
Enter State Abbreviation : NH
Enter Zip Code: 39876
Enter Phone Area Code: 222
Enter Phone Exchange: 222
Enter Last Four Digits: 2222

What action do you wish to take?
Print the list: Enter 1:
Add a person to the list: Enter 2:
Remove a person: Enter 3:
Sort the interal ArrayList: Enter 4:   
Save the list to a new file and quit: Enter 5:

Enter 1, 2, 3, 4, or 5: 1
Elmer J Fudd
2000 North 8th St
Kalamazoo MI 49008
(555) 355-999

Porky M Pig
11th Avenue
Apartment C
Crystal Falls IL 65432
(218) 777-6666

Bugs L Bunny
1503 Carrot Street
Big Hill MN 98765
(333) 222-9678

Sylvester C Cat
3111 Cage Avenue
Cage 3
Honolulu HI 33333
(789) 213-4444

Mortimer K Snerd
888 Seventeenth Street
Paducah TX 48848
(333) 333-1234

Bugs K Bunny
1503 Carrot Street
Big Hill MN 98765
(333) 222-9678

Albert A Anderson
1111 South 11th Street
Irontown NH 39876
(222) 222-2222

What action do you wish to take?
Print the list: Enter 1:
Add a person to the list: Enter 2:
Remove a person: Enter 3:
Sort the interal ArrayList: Enter 4:   
Save the list to a new file and quit: Enter 5:

Enter 1, 2, 3, 4, or 5: 3
Enter the last name of the person to remove: Pig
Enter the first name of the person to remove: Porky
Enter the middle initial of the person to remove: M
Removing

Porky M Pig
11th Avenue
Apartment C
Crystal Falls IL 65432
(218) 777-6666 at position 1.

What action do you wish to take?
Print the list: Enter 1:
Add a person to the list: Enter 2:
Remove a person: Enter 3:
Sort the interal ArrayList: Enter 4:   
Save the list to a new file and quit: Enter 5:

Enter 1, 2, 3, 4, or 5: 1
Elmer J Fudd
2000 North 8th St
Kalamazoo MI 49008
(555) 355-999

Bugs L Bunny
1503 Carrot Street
Big Hill MN 98765
(333) 222-9678

Sylvester C Cat
3111 Cage Avenue
Cage 3
Honolulu HI 33333
(789) 213-4444

Mortimer K Snerd
888 Seventeenth Street
Paducah TX 48848
(333) 333-1234

Bugs K Bunny
1503 Carrot Street
Big Hill MN 98765
(333) 222-9678

Albert A Anderson
1111 South 11th Street
Irontown NH 39876
(222) 222-2222

What action do you wish to take?
Print the list: Enter 1:
Add a person to the list: Enter 2:
Remove a person: Enter 3:
Sort the interal ArrayList: Enter 4:   
Save the list to a new file and quit: Enter 5:

Enter 1, 2, 3, 4, or 5: 4 What action do you wish to take?
Print the list: Enter 1:
Add a person to the list: Enter 2:
Remove a person: Enter 3:
Sort the interal ArrayList: Enter 4:   
Save the list to a new file and quit: Enter 5:

Enter 1, 2, 3, 4, or 5: 1
Albert A Anderson
1111 South 11th Street
Irontown NH 39876
(222) 222-2222

Bugs K Bunny
1503 Carrot Street
Big Hill MN 98765
(333) 222-9678

Bugs L Bunny
1503 Carrot Street
Big Hill MN 98765
(333) 222-9678

Sylvester C Cat
3111 Cage Avenue
Cage 3
Honolulu HI 33333
(789) 213-4444

Elmer J Fudd
2000 North 8th St
Kalamazoo MI 49008
(555) 355-999

Mortimer K Snerd
888 Seventeenth Street
Paducah TX 48848
(333) 333-1234

What action do you wish to take?
Print the list: Enter 1:
Add a person to the list: Enter 2:
Remove a person: Enter 3:
Sort the interal ArrayList: Enter 4:   
Save the list to a new file and quit: Enter 5:

Enter 1, 2, 3, 4, or 5: 5
Enter the new file name: zzzzt.csv
BUILD SUCCESSFUL (total time: 3 minutes 31 seconds)

Here is a copy of the file zzzzt.csv.
Anderson,Albert,A,1111 South 11th Street,,Irontown,NH,39876,222,222,2222
Bunny,Bugs,K,1503 Carrot Street,,Big Hill,MN,98765,333,222,9678
Bunny,Bugs,L,1503 Carrot Street,,Big Hill,MN,98765,333,222,9678
Cat,Sylvester,C,3111 Cage Avenue,Cage 3,Honolulu,HI,33333,789,213,4444
Fudd,Elmer,J,2000 North 8th St,,Kalamazoo,MI,49008,555,355,999
Snerd,Mortimer,K,888 Seventeenth Street,,Paducah,TX,48848,333,333,1234

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Address {
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zipcode;

    /**
    * constructor
    */
    public Address() {
       this.address1 = "";
       this.address2 = "";
       this.city = "";
       this.state = "";
       this.zipcode = "";
    }

    /**
    * constructor
    */
    public Address(String address1, String address2, String city, String state, String zipcode) {
       this.address1 = address1;
       this.address2 = address2;
       this.city = city;
       this.state = state;
       this.zipcode = zipcode;
    }...
$50.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats