In this project, you will experiment with building your own Python classes. Many of you have
music files on your computer and organize them into playlists.
You will build classes to represent your music files and playlists to organize them.
You may do this project with a partner.
If you choose to work with a partner, please only submit one copy of the code with both names written in each of the Python files as a comment.
Implement the Song
class in song.py
such that:
Each Song
object contains the artist, album title, song title, mp3 file name, and genre.
The Song
class provides accessor and mutator methods for all instance variables.
Blank strings (those containing only white space) are not valid for any instance variables. Mutator methods must print an error messsage and not modify the corresponding instance variable if a blank string is provided.
When a Song
object is printed, it shows the artist, album title, and song title. For example, if you print the song "Just Dance" in The Fame by Lady Gaga, you should see:
Just Dance in The Fame by Lady Gaga
Implement the Playlist
class in playlist.py
such that:
Each playlist contains a name, a list of Song
objects, and an indication whether the playlist is to be played in a random order.
The Playlist
class provides accessor and mutator methods for the instance variables you use for the name, list of songs, and random-play indication.
A blank string is not valid for the instance variable you use for the name. The corresponding mutator method must print an error messsage and not modify the instance variable if a blank string is provided.
When a Playlist
object is printed, it shows the playlist name and all the songs in the playlist. For example, if you print the playlist whose name is "Lady Gaga Mix" and which contains two songs: "Just Dance" in The Fame by Lady Gaga and "Americano" in Born This Way by Lady Gaga, you should see:
Playlist: Lady Gaga Mix
Just Dance in The Fame by Lady Gaga
Americano in Born This Way by Lady Gaga
The Playlist
class provides a method called nextSong
which returns the next Song
object to play in the playlist. If the playlist is played in the original order, it should return the next song in the list. If the playlist is played in a random order, it should return a song that has not been played yet. It should only repeat a song if all songs in the playlist have been played. This ensures that the playlist does not keep returning same two songs over and over again.
Notes and considerations
The list of songs is re-permuted after every song is played once. This means that the same song can potentially played twice in a row if the song is played as the last song in one permutation and it is picked as the first song in the next permutation. Also, you could in principle keep getting the same permutation repeatedly, but if you see this happening, you ought to suspect that you aren't re-permuting the list when you ought to be.
Think about what would happen if a user uses the mutator method to toggle the play order. If the play order is changed from random to non-random, then the nextSong
method should return the first song in the original list of songs. If the play order is changed from non-random to random, it should start returning songs in a random order as described above.
Also think about what would happen if a user uses the mutator method to change the list of songs. If the play order is non-random, the nextSong
method should return the first song in the new list of songs. If the player order is random, it should return a random song in the new list of songs.
Example of a playlist of 3 songs (A, B, C)
Non-random order | Random Order | ||
Correct | Incorrect | Suspicious | |
Song A Song B Song C Song A Song B Song C Song A Song B Song C . . . |
Song C Song B Song A Song B Song A Song C Song C Song A Song B . . . (Notice how each song appears only once in songs 1-3, 4-6, etc.) |
Song C Song B Song C Song A Song B Song A Song B Song C Song A . . . (Notice how Song C reappears before all 3 songs are played.) |
Song B Song C Song A Song B Song C Song A Song B Song C Song A . . . (Notice how the same permutation (B, C, A) keeps repeating.) |
Make at least two sample playlists, each with at least 3 songs, by placing appropriate assignment statements in sample.py
. (Make sure there are at least 5 unique songs in your examples.)
NOTE: This only works on Macs (Mac OS X).
Place all your Python files (song.py, playlist.py, and sample.py) and your MP3 files that you used in the samples in the same directory.
Download play.py
into the same directory.
If in your Song
class, you used a name other than getFilename
for the accessor method that returns the MP3 filename, edit play.py
, replacing getFilename
with your name for the accessor.
Open Terminal
by typing "Terminal" (without quotation marks) in Spotlight (the magnifying class at the top right corner of the screen).
Navigate to the folder where you have all your files using the following steps:
In the Terminal window, type the two letters cd
followed by a space. (Do not press the return key yet.)
Drag the folder containing your files onto the Terminal window and drop it there. This should type the full name of the folder into the Terminal window after the cd
and space.
Press the return key in the Terminal window to execute the command.
Type python
, followed by return, to enter the Python shell.
Type from play import *
into the Python shell.
Type play(myPlaylist1)
into the Python shell (or use any other playlist you created in sample.py
), and enjoy your music!
You can press ctrl-c
(that is, with the CTRL key held down, press the C key) to stop playing the playlist.
You will be submitting your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following files:
Song
class.Playlist
class.You will earn one point for each of the following accomplishments:
You have all the instance variables necessary for the Song
class.
You have accessor methods for all the specified instance variables in the Song
class.
You have mutator methods for all the specified instance variables in the Song
class.
The mutator methods in the Song
class respond to a blank string by printing an error message and not changing the instance variable.
The Song
class provides a special method such that you can convert a Song
object into a string, which would automatically be used for printing.
You have all the instance variables necessary for the Playlist
class.
You have accessor methods for all the specified instance variables in the Playlist
class.
You have mutator methods for all the specified instance variables in the Playlist
class.
The name mutator method in the Playlist
class responds to a blank string by printing an error message and not changing the instance variable.
The Playlist
class provides a special method such that you can convert a Playlist
object into a string, which would be automatically used for printing.
The nextSong
method returns a Song
object.
The nextSong
method returns the correct Song
object when the Playlist
is set to non-random order.
The nextSong
method returns the correct Song
object when the Playlist
is set to random order.
The nextSong
method does not return the same Song
object until all Song
objects have been returned by the nextSong
.
You wrote the correct contract for each accessor method in the Song
and Playlist
classes.
You wrote the correct contract for each mutator method in the Song
and Playlist
classes.
You wrote the correct contract for the nextSong
method in the Playlist
class.
Your code is not unneccessarily complex.
You construct at least 5 Song
objects in sample.py
.
You construct at least 2 Playlist
objects with different names and songs in sample.py
.