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
- (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 points) Explain, in a few sentences, why the concept of
abstraction is important when designing and implementing
data structures.
- (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;
}
}
-
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.)
-
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?
- (2 points, 1 point per part)
You're given an ordered array of words.
abandon |
affable |
befuddle |
common |
desktop |
electric |
zone |
- 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.)
- 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.
-
(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:
-
Add: Adds a new song to the list. This method will take one
parameter: the MP3's filename. Inside the method, create a Song
object and supply the filename to it (note that it will do the file
scanning at this time), and take
the resulting Song object and add it to the list. Finally, return
a boolean that is true if the Song was added successfully or false if it
failed (for example, if the Song constructor threw an IOException or if
the array is full). The add method does not need to print anything
to the screen itself.
-
Search: Given a song name as a parameter, return the
Song object associated with that title, or null
if no such Song exists in the database. (You can assume there
won't be multiple Songs with the same title.)
-
Remove: Given the name of a song title as a parameter, remove a
Song from the list. Return a boolean indicating
success -- if the song exists and was removed, return true, otherwise
return false.
-
Dump: Print out all the Songs. This method takes no parameters
and has no return values. Just enumerate through the array and
literally print out every Song. (The Song class has a toString
method that automatically deals with the output formatting of an
individual Song.)
-
Total: Add up all the times of the Songs in the database, and
return an int representing the total
play time. (Note that the Song class only computes approximate
times; if they differ from the results you get when you load the MP3s in
your favorite music player, that's fine -- the result should match the
sum of the individual times printed in the Dump method). This
method does not take any parameters, either. If the list is empty,
this method should return 0.
-
(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 <filename>: Add a song to the Database
associated with filename. If it succeeds, print out "filename
successfully loaded". If it fails, print out a brief error
message.
- r <name>: Remove the song titled name, or print
out "not found" if it's not found.
- s <name>: Find and print out the
information associated with the song titled name. Print out "not
found" if it's not found.
- d: Dump the entire database of songs to the screen;
- t: Print out the playback time (in seconds);
- q: Quit the program.
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:
- The book spends a lot of time arranging programs like this,
and in fact, I strongly recommend you check out the program
detailed on pages 66-69 in chapter 2 before starting; it's very similar in
structure to this program, and may be a useful
springboard if you don't feel comfortable with starting from a
blank editor. If you want to play with the book's examples, you don't even
have to type it out -- you can download code straight from the book's homepage.
-
Obviously, you'll need some MP3s to test this out with. Try it out
with your MP3 music collection, if you have one! If you need some MP3s
to work with, you can legally download a few free ones from Amazon
here.
Alternatively, I've made some MP3s available in
~cs3134/mp3 on CUNIX -- these are "fake", and in fact won't have any
useful music, but they should work fine for the purposes of our program.
-
If you encounter what you think is a bug in the Song class because it fails
on one of your MP3s, let me know. MP3s can be "badly formatted"
sometimes and can cause the parsing code I've written to get confused.
The sample ones from Amazon and the ones on CUNIX should work fine.
-
If you want to test your code with a bunch of MP3s, type in "java
MusicApp *.mp3". The computer will perform wildcard
expansion, whereby it actually scans your hard disk for all matching
MP3s (in the current directory) and splits them into separate command-line
arguments in one step. Similarly, if you're on CUNIX and want to work
with the "sample" MP3s I've provided, you can type in "java
MusicApp ~cs3134/mp3/*.mp3".
-
Note that each command in the UI has two parts: the command, and the data
("parameter") to operate with.
To separate the parameter from the command in the UI, use the
substring
method in the String class to extract the command (the zeroth character) and
to extract filename or title portion of the
command (the 2nd to nth characters). Do not use the
StringTokenizer class, as it won't be able to
handle filenames and title tracks with spaces!
-
The only method in Database that should actually print anything is
the Dump method. The rest return results to the main() method in App,
which then prints out the result where appropriate.
- You cannot use collections, like ArrayList or Vector,
in this assignment. In fact, we will avoid all "java.util"
data structures for the majority of the class, as the goal is
to become familiar with data structures by writing them
yourself.
- One strategy to help you along would be to get the program working without
interactive user input (for example, load all the songs and do a dump), and then focus on the input aspects,
all of which should fit straight inside your main()
method. Use the Javadocs (API documentation; see
Resources page) to your benefit
in learning these classes!