Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
ICS 21 / CSE 21: Introduction to Computer Science I
Lab 4: A Donation to the Music ArchivePurpose
This lab will give you practice with simple nested data structures and formatted console output. You will also gain additional practice in using ArrayLists and classes written by others.
Program requirements
A serious collector of recorded music has donated his collection to a music archive. You've been hired by the archive 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 type of media.
The archive has begun making a catalog of the donated items. A catalog entry consists of an accession number (a unique identifier), the work's title, and the type of media the music is on (i.e., 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). A 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 types are each designated by a single uppercase letter, according to the following table:
Letter | Category |
C | Compact media |
P | Paper (sheet music, books) |
V | Shellac and vinyl records |
W | Wax cylinder |
Technical details
You've worked out with your archive 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 one donated item per line). Each line will have the form:
accession number; title; media type code
that is, a line is formed by an accession number, a title, and a media type 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 provided will have been run through a testing program to ensure that 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, which you'll be able to 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 the index file and formatting the lines properly have been encapsulated in a class called IndexFile, which has already been written and is being made available to you.
You can find documentation on the MusicFile and IndexFile classes in the files MusicFile.txt and IndexFile.txt, which are part of the Eclipse project provided 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 it will be evident that the program is still making progress, even if it has a lot of work to do. (The real list of music will be 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 your program may otherwise by 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, complete list of donated 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 a file called music.txt in the Eclipse project provided. You should also test your program on other test lists that you put together, ones designed with the intention of trying to "break" your program. (And if they do not break your code, then you have reasonable certainty that the indexing part of your program works correctly.) Be sure to try music files that are empty; that have one, two, or three items; that place one item into every bucket; that add new items before or after all items in a bucket; and any and all cases that might uncover a potential mistake.
The program's major steps are straightforward:
- Construct the music file, index file, and structure to sort the music items (a 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 internal MusicList structure; MusicList is cleverly structured so that when an item is added to it, that item is in the correct order with respect to the others.
- Also, as you read each item, examine its media type and update the appropriate count.
- Once all items are read, open the index file. Then, for each item in the music list, write it into the index file.
- Don't forget to close the files once you've finished with them!
- 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 skeleton Java classes for you, along with additional documentation, which you can obtain at the following link:
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 alphabetically, or a number greater than 0 is s comes after t. This method comes in handy when figuring out where to place a music item into the music list so that ordering by title is maintained.
An admonition: There are two methods in the ArrayList class called add. add(item) adds an item to the end of an ArrayList. add(i, item) adds an item at position i of an ArrayList, moving all items from i to the end of the list "over by one." You will likely need to use both of these in your program; do not confuse them!
About Lab Exam 4
Lab Exam 4 will be very similar, but perhaps not identical, to the program you have been asked to write for this exercise. The lab exam will ask you to complete one or more of the following methods:
- The Bucket constructor
- Bucket.addItem
- The MusicItem constructor
- The MusicList constructor
- MusicList.addItem
- MusicList.updateCounts
- The MusicManager constructor
- MusicManager.makeMusicIndex
- MusicManager.writeMusicIndex
- MusicManager.displayCounts