Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


Intro to Java

Lab 8: Let’s Make a Playlist 

Background

An exciting thing about processing files in Java is that we can now process tons of data all at once. For the rest of the semester, we’ll be using files to process large amounts of data. Today will be the first day, where we will create a program that generates a playlist of varying length based on user input.

Exercise

For this exercise, we’re going to use Billboard’s Top 100 songs for each year over 51 years (1965-2015) (source: walkerkq on github). The songs are located in a file stored on Drive: songs.txt. We’re going to start with copying the file into our Cloud Editors, and then move on to create a playlist generating application.

1. Open Lab8.java in your cloud shell. Make sure to use this starter file as there are some new things included that we haven’t covered yet!

2. Download the songs.txt file from drive by clicking this link. Upload or copy/paste it to Cloud Shell. If you usually copy and paste your files, make sure to copy and paste all of songs.txt! Make sure your songs.txt file is located in the same folder as Lab8.java! Otherwise, your program won’t work.

3. Our program is going to take in two command line arguments. The first is the name of the file, and the second is the desired length of the playlist. Go ahead and save the filename to a String variable, and parse the second argument into an int.

4. Now that we have our command line args saved in the appropriate types, we need to create a data structure to hold all of our songs. Let’s use an ArrayList of Strings. Once you declare and initialize the ArrayList variable, move on to step 5.

5. Use the filename variable that we saved in step 3 to open a new BufferedReader and read the contents of our file into the ArrayList. You should save each line as its own String inside the ArrayList. Check the slides if you aren’t sure how to do this step!

a. To ensure you successfully saved all of the data, print out the ArrayLists’s size (using .size()) and make sure it is 5100.

6. Once the ArrayList is full of our data, we need to choose a number of songs out to create the playlist. You can do this multiple ways. Here are some suggestions:

a. Randomly shuffle your main ArrayList and print the first X songs out (where X is the second command line argument from Step 3)

b. Randomly choose X songs from the list (hint: there’s a chance you pick the same song twice. How can we make sure that doesn’t happen?)

7. Once we are in a position to select songs for the final playlist, choose them and print each out. Number the songs so our user knows we actually gave them the number of songs they asked for!

8. (optional) If you have extra time. Try writing the playlist you made to a file called playlist.txt.

9. (optional) Done with #8? Try taking a year (i.e. 2002) as a command line argument and building a playlist from songs in 2002