Lab 10 FAQ

Return to Lab 10

Why do I need a __str__ and a display method for the MusicCollection class?

The display method is easier to write because you can print out the information, nicely formatted, rather than trying to accumulate all of that information in a string.

The __str__ method can just return the string representation of the MusicCollection's filename, number of songs, and the total playtime. That should give you enough information about the MusicCollection most of the time. When you need more info, use the display method.

What parameters should the MusicCollection constructor take?

Ask yourself what you need to tell the MusicCollection object and what the MusicCollection object needs to figure out on its own. Make your best guess and then discuss your intuition with me.

Why do I need separate test functions for my classes?

So that you can make sure your class is working "in the small" before you try to use it when you're reading the library from a text file. Otherwise, if you're facing a problem (as below), you wouldn't know if there is a problem with your class or with the reading from a file. The test functions help you to isolate the problems and debug more easily.

I tested my Song class and the song objects are printed out correctly, but when I read in from the file and display the songs, a song's title, album, artist name, and length are on separate lines. What's going wrong?

When you get the line from a file using the readline method, the string includes the newline character ("\n"). You need to use the strip method to remove that character from the string before creating your Song object.

Why does the Song class's writeSong method take a file as a parameter instead of a filename?

Basically, to make your life easier. Since you're writing a bunch of songs to a file in the Music Collection class, you don't want to keep opening and closing the file. (From what we've learned so far, that would also result in writing over the file so that only the last song you write would be in the library.)

How do I check if a filename begins with "libraries/" or "albums/"?

You can use a string method to see if the filename (which is a string) begins with those things. Look at the string method sheet or use help to remind yourself of the appropriate method.

When I read in the library file, I get a bunch of error messages about being outside the range. What is the cause of that?

That error message is from the Counter class, which is used by the PlayTime class. When you constructed the PlayTime object, what were the datatypes of the parameters that you passed in? (What did you think they were, and what are they actually?)

What if the user gives me a file that doesn't exist?

The beauty of our "pre" condition is that it says that that file must exist. Our program will crash, but we warned them in the pre condition that the file had to exist. We make no guarantees otherwise. (We'll soon discuss a nicer way to handle files that don't exist, but it's still not beautiful.)

In the UI driver information, it says to create a MusicLibrary object and then to read in the library. My constructor reads in the library. What should I do?

In this case, then you just need to create the MusicLibrary object. You don't need to have a separate "read library" method call.


Return to Lab 10