HOMEWORK - FALL 2023
Programming Assignment 4 - due Friday, November 10th by 7PM (China time).
REMINDERS:
- Be sure your code follows the coding style for CSE214.
- Make sure you read the warnings about academic dishonesty. Remember, all work you submit for homework or exams MUST be your own work.
- Login to your grading account and click "Submit Assignment" to upload and submit your assignment.
- You are not allowed to use ArrayList, LinkedList, Vector, Tree classes or any other predefined Java API Data Structure classes to implement this assignment.
- You may use Scanner, InputStreamReader, or any other class that you wish for keyboard input.
Write a menu driven program that handles the mailing list for a club. Your program must receive the mailing records from the keyboard or a text file and allow the user to access the name, address, and the membership status for all members. To find a member, zip code is used as the main key and then a name match will be performed if necessary. To minimize the access time, your program must be implemented using a standard Binary Search Tree. However you are not required to create a balanced tree.
1. Write a fully documented class named Member that contains a name (last name followed by first name), street (string), cityState (string), zipCode (int) and active (bool). Also include the standard accessors, mutuators and constructor(s) of your own design.
2. Write a fully documented class named MemberList that is a list of Member records. The list should always be sorted by name. However, this doesn't mean you run a sorting algorithm on the list. As you insert each Member into the sorted list, traverse the list to figure out where the new record should go and insert there. Then the new list is still sorted without running a sorting algorithm.
NOTE 1: When a member is no longer active, you must simply set the member's "active" flag to false and ignore the record while printing the list of active members. In other words, no record will actually be removed from the list.
NOTE 2: As long as you do not use Java API tree classes, you may implement this list anyway you wish (array, linked list, Java API, etc.).
NOTE 3: Since last name is followed by first name in each instance of Member, you do not need any additional code for comparing two names. Simply use the compare method in String class, i.e. name1.compare(name2).
NOTE 4: To improve efficiency of your program, you can define a counter for number of active records in each list.
3. Write a fully documented class named ZipCodeNode that represents a node for ZipCodeTree. The node should contain a zip code, a reference to a MemberList along with references to the node's two children. Note that the zip code must be equal to all zip codes stored in MemberList. Include the standard accessors, mutuators and constructor(s).
4. Write a fully documented class named ZipCodeTree that implements a standard Binary Search Tree of ZipCodeNode nodes, where zip code is the key for all comparisons in the tree.
- The class should have a private reference variable, "root" which references the root of the tree.
- Once a ZipCodeNode is inserted in the tree, it should not be removed in any case, even if all records in its MemberList are inactive.
- Total number of nodes in ZipCodeTree must be equal to the number of unique zip codes inserted by the user.
- Include methods as needed
NOTE: You must implement this class without using Java API for tree operations.
5. Write a fully documented class named Club that uses ZipCodeTree and contains the main method and presents a menu to the user of the following menu options, along with what to do if the user selects that option:
- I (Insert a new record)
Prompt the user for a name, street, city-state, and zip code on four separate lines. Then insert this entry into the tree. - C (Cancel membership)
Prompt the user for a name and zip code on two separate lines, and if the name is found at the given zip code, cancel the membership for this member (set "active" flag for this member to false). Otherwise if the name is not found, print a message (e.g. "Member Not Found"). Remember, you must first find the zip code in the tree, and then search for the name in the corresponding list. - L (List active members)
Print the list of all active members sorted by zip code and then by name. Keep in mind, inorder traversal of ZipCodeTree produces a sorted list by zip code, and since each MemberList is already sorted by name, you do not need to apply a sorting algorithm. - N (Number of active members)
Print number of active members at each zip code sorted by zip code. You must perform an inorder traversal of the tree to get a sorted list. Do not use a sorting algorithm. - S (Status of a member)
Prompt the user for name and zip code on two separate lines, and if the information is found, print the mailing address for this member along with the membership status (active/inactive). If you can't find the name at the given zip code, print a message. Remember, you must first find the zip code in the tree, and then search for the name in the corresponding list. - A (List active members in a given area)
Prompt the user for a zip code. If the zip code is not found, print a message, otherwise print the list of all active members who have a zip code greater than or equal to the given zip code. Once you find the subtree that holds the mailing records, you can invoke the same method used to list the records in option "L". - D (Depth of the tree)
Print the depth of the ZipCodeTree. - Q (Quit the program)
Quit the program. - OPTIONAL - EXTRA CREDIT(5 points)
R (Read mailing records from a file)
Prompt the user for a filename, then read the mailing records from the file and insert them in the tree. Each mailing record is saved on four separate lines, similar to the input entered from the keyboard. If the file doesn't exist or there is an error reading from the file, you must report an appropriate message and prompt the user for the next option.
6. Write exception class(es) for any exception that need to be thrown.
Note: You may define the tree traversal recursive methods in ZipCodeNode or ZipCodeTree.
INPUT FORMAT
- You may assume that all input values are valid and no error checking is required. However if an invalid option is selected, you must print a message and prompt the user for the next option.
- Names are case-insensitive.
- Each name contains last name followed by first name separated by one blank. All special characters,
except white space, may be used in first or last names, like O'Donnell or Wenz-Guercia. - Zip code is a 5-digit number and may contain leading zeros. For example "06525" or "00123" are valid zip codes.
- Menu options are case-insensitive (e.g. "N" and "n" are the same).
- Each mailing record is saved in the input file on four separate lines in the following order:
- Name
- Street
- City and state
- Zip code
- Our test cases have the following format:
- Name (last name followed by first name) does not exceed 25 characters.
- Street address does not exceed 20 characters.
- City and state are at most 20 characters.
- Zip code is a 5-digit number.
- Members with duplicate names have different zip codes (i.e. names for each zip code are unique).
OUTPUT FORMAT
- All output must be accompanied by additional information explaining the specific operation.
- Each zip code must be printed as a 5-digit number. For example "06525" or "00321".
- All lists must be printed in a nice and tabular form as shown in the sample output. The following example shows different ways of displaying the name and address at pre-specified positions 21, 26, 19, and 6 spaces wide. If the '-' flag is given, then it will be left-justified (padding will be on the right), else the region is right-justified. The 's' identifier is for strings, the 'd' identifier is for integers. Giving the additional '0' flag pads an integer with additional zeroes in front.
String name = "Doe Jane"; String address = "32 Bayview Dr."; String city = "Fishers Island, NY"; int zip = 6390; System.out.println(String.format("%-21s%-26s%19s%06d", name, address, city, zip)); System.out.printf("%-21s%-26s%19s%06d", name, address, city, zip); Doe Jane 32 Bayview Dr. Fishers Island, NY 06390 Doe Jane 32 Bayview Dr. Fishers Island, NY 06390
SAMPLE INPUT/OUTPUT
Program output is in blue. Comments are in green. User input is in black.
I) Insert a new record C) Cancel membership L) List active members N) Number of active members S) Status of a member A) List active members in a given area D) Depth of the tree Q) Quit the program Enter Your Choice: I Enter a Name: Doe Jane Enter a Street: 32 Bayview Dr. Enter the City and State: Fishers Island, NY Enter a Zip Code: 06390 Inserting Doe Jane into List... Enter Your Choice: c Enter a Name: Doe Jane Enter a Zip Code: 11111 Member Not Found! //More // members // are // inserted... Enter Your Choice: L List of all active members: Name Street City and State Zip --------------------- ------------------------- ------------------ ----- Doe Jane 32 Bayview Dr. Fishers Island, NY 06390 Epp Sussana 78 Kirk Ave. Fishers Island, NY 06390 Zhang Brian 19 Penbrook Rd. Fishers Island, NY 06390 Doe Jane 17 Brandywine St. Wood Bridge, CT 06525 Main Michael 12 Harrison Rd. Wood Bridge, CT 06525 Spencer Susan 98 Shen Ct. Wood Bridge, CT 06525 Williams Deirdre 101 Dean Lane Stony Brook, NY 11790 Williams Kevin 101 Dean lane Stony Brook, NY 11790 Williams Shane 178 Stony Brook Rd. Stony Brook, NY 11790 Williams Tara 98 Stony Rd. Stony Brook, NY 11790 Zhang Steven 19 Ridgeway Rd. Stony Brook, NY 11790 Doe Jane 17 Main St. Southampton, NY 11968 Spencer David 101 Halsey Ave. Southampton, NY 11968 Spencer John 75 Raynor Rd. Southampton, NY 11968 Spencer Mark 57 Roman Dr. Southampton, NY 11968 Spencer Mary 91 Broidy Lane Southampton, NY 11968 Spencer Richard 109 Spinack Rd. Southampton, NY 11968 Von-Neuman Chris 65 Hampton Rd. Southampton, NY 11968 Enter Your Choice: n Number of active members at each zip code: Zip Code Number of Members -------- ----------------- 06390 3 06525 3 11790 5 11968 7 Enter Your Choice: S Enter a Name: Doe Jane Enter a Zip Code: 06390 Mailing address and status for user: 32 Bayview Dr. Fishers Island, NY 06390 (active) Enter Your Choice: a Enter a Zip Code: 11968 List of all active members with zip code greater than or equal to 11968: Name Street City and State Zip --------------------- ------------------------- ------------------ ----- Doe Jane 17 Main St. Southampton, NY 11968 Spencer David 101 Halsey Ave. Southampton, NY 11968 Spencer John 75 Raynor Rd. Southampton, NY 11968 Spencer Mark 57 Roman Dr. Southampton, NY 11968 Spencer Mary 91 Broidy Lane Southampton, NY 11968 Spencer Richard 109 Spinack Rd. Southampton, NY 11968 Von-Neuman Chris 65 Hampton Rd. Southampton, NY 11968 Enter Your Choice: D The depth of the Tree is 4. //Print out the appropriate depth. Enter Your Choice: C Enter a Name: Doe Jane Enter a Zip Code: 06390 Cancelling Membership for Doe Jane... Enter Your Choice: q Exiting Program...