CS3134 Homework #1
Due on September 30, 2004 at 11:00am

There are two parts to this homework: a written component worth 12 points, and a programming assignment worth 13 points. See the homework submission instructions on how to hand it in and for important notes on programming style and structure.

Written questions

  1. (2 points) Register on the newly-created class webboard.  You can do so by navigating to http://home.janak.net/cs3134/webboard.html and following the instructions.  (You do not need to make any posts, but the registration will be important later on when grades are uploaded to the webpage.)
  2. (2 points) Explain, in a few sentences, why the concept of abstraction is important when designing and implementing data structures.
  3. (6 points, 3 points per part) You're given the following code that manipulates an array of ExampleData objects:
    public class ExampleRunner {
      public static void main(String[] args) {
        ExampleData[] dataArray = new ExampleData[5];
    
        for(int i=0; i < dataArray.length; i++) {
          System.out.println(dataArray[i].getString() + "," + 
    			 dataArray[i].getInteger());
        }
      }
    }
    
    class ExampleData {
      private int testInteger = 0;
      private String testString = null;
    
      public ExampleData() {
        testString = "testing";
        testInteger++;
      }
    
      public String getString() {
        return testString;
      }
    
      public int getInteger() {
        return testInteger;
      }
    }
    
    1. This code will compile, but it will immediately crash. State what error is generated, and explain in 1-2 sentences why this error is being generated. (Saying something generic like "a line of code is missing to do something" does not qualify as an explanation.) Explain in 1-2 sentences what needs to be added to the main() method to fix this. (Hint: if you want to, try typing in and compiling the code, and play around with it interactively.)
    2. Assuming you fix the problem in the previous section, running the program will give you five lines of output -- each containing a String and an integer. What is the output the program will generate? Also, if we wanted to change the behavior of the integer such that it gives us the total number of ExampleData objects that exist, what one Java keyword can we add to the ExampleData class, where, and why (1-3 sentences) would it work?
  4. (2 points, 1 point per part) You're given an ordered array of words.
    abandon
    affable
    befuddle
    common
    desktop
    electric
    zone
    1. Give an example of a search term (does not have to be a word listed above, although it can) which would be faster via linear search, and an example of a search term which would be faster via binary search. For each of the two search terms, show why the one search is faster than the other by enumerating the steps that would be taken. (You can assume that we're using the Java String.compareTo method to perform the inequality comparison, which does a lexicographical analysis of two words and returns negative, zero, or positive if the first word is comes before, is equal to, or comes after the second word.)
    2. Explain in 1-3 sentences how these performance results can be reconciled with the assertion that, in general, a binary search is "faster". You may use big-Oh notation if you want, although I'll accept an informal argument as well.

Programming problem

In class, I used iTunes (and music databases in general) as a motivating technology for Data Structures.  Now, you'll have the opportunity to prove it: the first programming assignment consists of building a small music database system (well, a playlist system, to be precise).

For simplicity's sake, you can assume that you're going to need to deal with at most 100 tracks.  A command-line interface is sufficient, and we're not actually going to play the music; we're simply going to build a list of tracks that we can do simple manipulations with.  You'll use an array-backed list to store the information in memory.

Populating the array in the first place will be done by actually scanning real MP3 files on disk and gleaning a MP3 track's title, artist, genre, year, and song length.  Since I can't safely assume all of you are experts at the binary MP3 format, I've written a special Song class in Java that will do most of this work for you.  You can download the Song class here, and you can see the associated Javadoc documentation here.  I've gone out of my way to document the class heavily, so it should be reasonably easy to read, but make sure you go through all the methods and understand what is going on.  The one method you don't need to understand is the constructor -- it does some file I/O and bit manipulations you do not have to worry about at this time.  Your task will be to write two other classes to allow us to manipulate a list of these Song objects.

  1. (7 points) Write a class, called Database, that contains an array-backed list which will be composed of many individual Song objects. You can hardcode the length of the array that this list is composed of (see above for a clue as to what the length should be). This class must have methods to support the following operations:
  2. (6 points) Write a class called MusicApp which contains one instance of the Database list and has a main method. The main method will do two things: on startup, it will process all of the standard command-line arguments (which will be the MP3 files of interest) and repeatedly call the Add method of the Database object to add those files to the Database.  It should then prompt the user for input and allow manipulation of the list, accordingly. Here's a list of the commands it should support:

A sample execution of the program would look like the following. Input is italicized.

$ java MusicApp empty.mp3
Welcome to Janak's Music Application!
Loading songs...
empty.mp3 successfully loaded.
> a short.mp3
short.mp3 successfully loaded.
> a invalid.mp3
Error: could not add invalid.mp3
> a longer.mp3
longer.mp3 successfully loaded.
> s Empty Track
Artist: Janak Parekh
Album: CS3134 HW1
Title: Empty Track
Year: 2004
Bitrate: 128kbps
Length: 0 seconds
Filesize: 5560 bytes

> s Invalid Song
Not found
> d
Artist: Janak Parekh
Album: CS3134 HW1
Title: Empty Track
Year: 2004
Bitrate: 128kbps
Length: 0 seconds
Filesize: 5560 bytes

Artist: JP
Album: COMS W3134 HW1
Title: A Short Track!
Year: 2004
Bitrate: 128kbps
Length: 5 seconds
Filesize: 86644 bytes

Artist: Janak J. Parekh
Album: CSW3134
Title: A Longer Track!
Year: 2004
Bitrate: 128kbps
Length: 9 seconds
Filesize: 163131 bytes

> r Empty Track
> t
14 seconds
> q

The intent of this programming problem is to practice your knowledge of lists and to get accustomed to writing multi-class programs from scratch. I'm not trying to "trick" you here -- the implementation is fairly straightforward. I will not give you invalid input.

Extra credit (4 points): One very cool feature of iTunes is that there's a search field in which you can type any phrase and it will search the entire database in every field to see if there's a match.  For example, for "The Beastie Boys" if I were to search for "boy" I'd get a match.  Modify your search function to support this functionality.  In order to do so, you'll need to search each item in the list more thoroughly, and you'll have to support case-insensitive, partial matches.  Hint: look at the Java String documentation to see if there are methods that can help you in doing so.  If you implement this feature, make sure to mention that you've done so in your README.

Some notes: