CS 314 Assignment 4: Merging and Stacks

Due: October 1, 2015.

Files: Cons.java   Account.java   test4.lsp

This assignment may be done in Java or in Lisp (except item 2) .

  1. Write the functions union and setDifference, using the merge technique, to perform these functions on sets represented as lists. You may use the llmergesort function to sort the lists first. We will assume that the elements of the lists support the operations .compareTo() and .equals().

  2. Write a function Cons bank(Cons accounts, Cons updates) that uses the merge technique to update a list of bank accounts. Both accounts and updates have elements of type Account, with access functions String name() and Integer amount(). We will assume that accounts is already sorted by name, but updates is not sorted. A given name will appear only once in accounts, but may have multiple entries in updates.

    The function bank should update a customer's bank balance by adding the amount of each update to it. The output should be a new sorted list of accounts with updated balances; make a new Account with the new information.

    You will need to write the .compareTo() method for an Account to properly sort the updates. The first criterion for sorting is the name, which should be in alphabetic order. Within the same name, updates must be sorted by amount. In order to avoid falsely signaling overdrafts, the bank must process all deposits before any withdrawals. Therefore, any positive amount must be sorted before any negative amount. The bank also wants to sort the negative amounts so that the largest withdrawals (most negative amounts) are processed first, to cause the largest possible number of overdrafts and overdraft fees for the bank. (Yes, this is evil; but it is what the banks actually have been doing. http://www.huffingtonpost.com/2010/08/11/wells-fargo-overdraft-law_n_679178.html )

    For each update that causes an overdraft, print a message stating so, with the name and final amount. Go ahead and update the account, making the account balance negative, and subtract 30 from the balance as an overdraft fee for each overdraft transaction.

    If a name does not exist for an account and the total amount of all updates for that name is positive, create a new account for that name. Print a message that there is a new account, with a name and amount.

    If a name does not exist for an account and the total amount of all updates for that name is zero or negative, print a message that there is no account and the name and amount, but otherwise ignore the update.

  3. Write a program String [] mergearr(String [] x, String [] y) that will merge two sorted arrays x and y to produce a new array in ascending sorted order. If there are duplicated items, the duplicates should all appear in the output.

  4. Write a program boolean markup(Cons text) that will check a text in a markup language (such as HTML or XML) to see if it is well-formed. The list text is a list of String. Some strings are markup tags, such as "<TT>" or "</TT>", and other strings are ignored for checking purposes. You may assume that any string that begins with < is a tag. Every opening tag should be matched by a closing tag, indicated by / ; we will assume that tag pairs may be nested to a depth up to 100.

    If a list is well-formed, return true; if there is a tag out of place, print a message with the offending tag, its numeric position in the list (starting with 0), and the correct tag that was expected, and return false.

    An example of XML is: http://www.comptechdoc.org/independent/web/xml/guide/xmlexample.html