A Donation to the Music Archive

Lab Assignment 4
to prepare for Lab Exam 4

This lab give you practice with simple nested data structures and formatted console output. You also gain additional practice in using ArrayLists and routines others have written.


Program requirements

A serious collector of recorded music has left his collection to a music archive. You’ve been hired to create a title index and some “media counts” for the collection, using the information from a file that lists the donated items. The archive needs the title index to easily find items; it needs the counts to determine how many storage shelves it needs for each kind of media.

The archive has begun making a catalog of the donated items. A catalog entry consists of an accession number (a unqiue identifier), the work’s title, and the kind of media the music is on, such as paper (a book or pages of sheet music), vinyl record(s), etc. An accession number is no more than 10 characters, and can be made up of digits and/or letters (and no other symbols). The title is no longer than 50 characters and always starts with a capital letter; it never contains a semicolon, for reasons that will become clear shortly. The media categories are each designated by a single uppercase letter:

Letter Category
C      compact media
P      paper (sheet music, books)
V      shellac & vinyl records
W      wax cylinder

Technical details

You’ve worked out with your archieve contact that the input file given to you will be a text file, called music.txt. Each donated item’s information is on one line (and there is only donated item per line). Each line will have the form

acccession number; title; media category code

that is, a line is formed by an accession number, a title, and a media category code, with these three fields separated by a semicolon (;) and a space. (There is no ";" or space at the end of the line.) Each line will end with the standard PC end-of-line mark--that is, a carriage return character followed by a line feed character--in Java terms, the characters \r\n.

The music file will have been run though a testing program to ensure its format is correct and that its fields follow the specifications given for them. You can be confident that the music file will be in the correct format to be fed into your program. To do this testing, a MusicFile class was written that you can use; it contains the methods needed to properly process the music file—to open it, read a line of information from it, and close it.

The index is to be placed into a text file called index.txt. It will have the information about each musical item on one line, nicely formatted, and be in alphabetical order by title. The details of writing to and formatting the lines of the index file have been encapsulated in the class IndexFile, which has already been written and is being made available to you.

The MusicFile and IndexFile routines are documented in the file MusicFile.txt (which is provided to you in the ZIP file for this assignment; see below).

The counts are to be displayed on the screen in a neat, easy-to-read arrangement, after the index file is constructed (and stored). Each count is labeled so the user knows the media to which the count refers.

As the program runs, occasionally print status messages to the user, so s/he does not worry there is a problem (the real list is very long, and can take quite a while to process)—use messages such as "building the index and computing media counts", "now writing the index", "Done! The index is in the file index.txt", and so on. Only write messages when the computer may otherwise be churning away for some time with no output to the user, and when the program finishes, to tell the user the name of the index file. For example, you won’t need to say "printing counts" since the counts themselves would appear immediately after such a message.

The actual list of music will be provided to you at a later date: the cataloging of the collection is ongoing. So, you will have to test your program using a “fake” list. We’ve provided one for you in music.txt; that file is also in the ZIP archive for this assignment. You should also test your program on other test lists you put together, ones designed to try and “break” your program. (And if they do not break your code, then you have reasonably certainty that the indexing part of your program works correctly.) Especially, try music files that are empty; have one, two or three items; place one item in every bucket; add a new item before all items in a bucket or after all items in a bucket...any and all cases that are not “everyday” situations.

The program’s major steps are straight-forward: construct the music file, index file, and structure to sort the music items, the MusicList. Open the music file; for each line of the music file, read in its fields and make a MusicItem object out of them. Place that item into the intermal MusicList structure; MusicList is cleverly structured so that when an item is added to it, that item is in title order. Also, as you read each item, examine its media code and update the appropriate count. Once all items are read, open the index file. Then, for each item in the music list, pass it to the method that writes a line to the index file. Close the files when you process the last item on the music list. Then, print out the media counts, and compute and print the count of all items.

As usual, to help you get started, we’ve prepared a number of Java skeleton programs for you. Download Lab4.zip to obtain them, along with the documentation and test music files discussed above.

In every file in which they appear, replace all spaces marked //your code goes here with your own Java code to produce a complete, correctly working program.

A hint: You will probably find the String method compareTo() quite useful. s.compareTo(t), with s and t being Strings, returns 0 if s and t have the same value, a number less than 0 if s comes before t in alphanumeric order, and a number greater than 0 if s comes after t. This method comes in handy when figuring out where to place a music item in the music list so that ordering by title is maintained.

An admonition: the ArrayList method that adds an item to the end of an ArrayList is add(item). The ArrayList metod that adds an item at position i of an ArrayList, moving all items from i to the end of the list "over by one," is add(i, item). You will likely need to use both of these in your program; do not confuse them!

And a caution: Text files, such as the music file used in this assignment, have end of line marks that differ on different computers. In particular, the PC marks the end of a line one way (with a carriage-return/line-feed character pair) and Mac and Unix a different way (with just a carraige return character). If you get weird groupings of music information when reading the music file, check that the file has been saved properly for use on a PC. One easy way to do this is to open the file in TextPad and click on Save As...; look at the bottom of the dialog box, at File format:. If it says PC you kow the file has its ends of lines correctly marked. If it says UNIX or Mac, it is not formatted correctly; change the format to PC and save the file. If the format is No Change, it’s best to resave the file with the PC format, just in case.


About lab exam #4

You will be required to complete one or more of the following methods:



Written by Norman Jacobson for ICS 21 Fall 2004, July 2004
Minor revisions by Norman Jacobson for ICS21 Winter 2005, December 2004
Rvised to reflect encapsulation of file handling routines by Norman Jacobson for ICS21 Fall 2006, November 2006
Minor edits for clarity by Norman Jacobson, December 2006 and September 2008