Colorado State University

Recitation R11 - The Library Class
Summer 2016

CS160: Foundations in Programming


Data

  1. Declare an array of Music objects called arrayTitles as a public static class variable.

Implement the readLibrary Method

  1. Implement a public static method called readLibrary which takes a String parameter called fileName and returns nothing.
  2. Implement readLibrary as follows:

  3. read in the fhe first line of the file as an integer called size. This indicates the number of Music objects.
  4. Use size it to allocate the class variable arrayTitles.
  5. Add a for or while loop that reads the remaining lines of the file.
  6. The format of the file is 4 lines per entry: title, album, artist, year.
  7. Create a Music object of each entry and store it in arrayTitles.

    NOTE: Even after allocating the array, you must instantiate each individual Music object (by calling the constructor) for each array element.
    WARNING: After each call to nextInt() you must add a nextLine() call to discard the newline that follows the integer, otherwise the next String you read may be empty.
    Add print statements if necessary to make sure you are reading all the data correctly.

Test the readLibrary Method

  1. Call readLibrary from your main with args[0] as the parameter.
  2. Here is the data file that you will read for this program: music.txt
  3. Add a loop that iterates the array of Music objects and prints each element. Add a track number in front of each object, followed by a colon and space. In addition, keep track of the total cost of the titles, and print it at the end, in the format shown. The console output should be as follows:
    1: Brown Eyed Girl, Blowin' Your Mind!, Van Morrison, 1967
    2: Stop Putting the Hurt on Me, Lucille, B. B. King, 1968
    3: Another Brick in the Wall, The Wall, Pink Floyd, 1979
    4: Funeral for a Friend, Goodbye Yellow Brick Road, Elton John, 1973
    5: Beat It, Thriller, Michael Jackson, 1982
    6: Like a Virgin, Like a Virgin, Madonna, 1984
    7: So Far Away, Brothers in Arms, Dire Straits, 1985
    8: Try a Little Tenderness, Swamp Boogie Queen, Katie Webster, 1990
    9: Hey Jack Kerouac, MTV Unplugged, 10,000 Maniacs, 1993
    10: Mockingbird, Encore, Eminem, 2005
    11: Jackdaw, Draw the Line, David Gray, 2009
    12: Someone Like You, 21, Adele, 2011
    Number of Tracks: 12
    Total cost: $13.78
    

Implement the Rest of the Methods

  1. Add a public static method writeLibrary that takes a String parameter with the output filename and returns nothing:
  2. Implement a public static method searchTitles that takes a String parameter and returns nothing. This method should print all the tracks that contain that String.
    (Hint: use the java documentation to look up the .contains method in the String class)
  3. Implement a public static method called sortTitles that has no parameters and returns nothing. This method shoudld sort the array by song title. You can do this by creating a for loop that iterates over all the uppercase letters and prints only the titles that start with that letter. Note that the resulting output is sorted only by the first letter, and is not completely alphabetized.

© 2016 CS160 Colorado State University. All Rights Reserved.