CS 160, Spring 2015
Programming Assignment P11
Music Library: Classes, Objects, and Arrays

Programming due Monday, Apr. 27 at 6:00pm; late deadline Apr. 27 at 11:59pm.

This programming assignment has four objectives:
  1. Declare a class with data and methods
  2. Write your first constructor
  3. Allocate and instantiate an array of objects
  4. Continue practicing file input and output


Write the following program from scratch in the following phases:

Part One

  1. Create a new project in Eclipse called P11.
  2. Define a new class called Music.java, with a main method.
  3. Define the following as private instance (non-static) data in Music.java:
        private String songTitle;
        private String albumName;
        private String artistName;
        private int releaseYear;
  4. Define a constructor that is passed data used to initialize all the instance variables. A constructor is just like a normal method except it has no return value. Next write methods to get individual variables, as shown below. The methods should be public and non-static, and should have the exact signatures shown below:
        // Constructor
        public Music(String title, String album, String artist, int year) { }
        // Get methods
        public String getTitle() { }
        public String getAlbum() { }
        public String getArtist() { }
        public int getYear() { }
  5. Write a toString method that returns a string with the title, album, artist, and year, separated by commas and a space, with no trailing comma or space. The string should match the the sample output shown in Part Three. This method should NOT print anything, instead it creates a string and returns it!
    	// toString method
    	public String toString() {}
  6. Implement the following method to get the price of the title:
        public double getPrice() { }
    The algorithm for determining the price is $1.29 for titles released before 1970, $1.89 for titles released in the 1970's, $0.69 for titles released in the 1980's, $0.99 for titles released in the 1990's, and $1.39 for titles released on 2000 and later. The reasoning for this is that some decades have better music than others!

Part Two

  1. In the class you have already made called Music.java:
  2. Declare (but do not allocate) an array of Music objects called arrayTitles as a public static class variable.
  3. Implement a public static method readLibrary which takes a String parameter with the input filename:
        public static void readLibrary(String inputFile);
  4. Call readLibrary from your main with args[0] as the parameter.
  5. Do not hard-code the file name, use the name passed in as a parameter.
  6. Here is the data file that you will read for this program: music.txt
  7. Implement readLibrary as follows:

  8. NOTE: Even after allocating the array, you must instantiate each individual Music object (by calling the constructor) for each array element.
  9. 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.

Part Three

  1. Implement a public static method writeLibrary which takes a String parameter with the output filename:
        public static void writeLibrary(String outputFile);
  2. Call writeLibrary from your main (after readLibrary) with args[1] as the parameter.
  3. Do not hard-code the file name, use the name passed in as a parameter.
  4. To implement writeLibrary, open a PrintWriter and write a loop to iterate the array of Music objects, writing out each entry as shown by the example output file below:
    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
    Total cost: $14.58
  5. Your output file should match this exactly.
  6. The format above has a number for each title, starting at 1 and ending at the last number. Each field is separated by a comma and space, but there is no trailing comma or space.
  7. Make sure that your program correctly writes all of the data in the input file to the output file.
  8. Add code to writeLibrary figure out how much it would cost to buy all the titles in the file by iterating the array of Music objects and calling getPrice for each element. You must accumulate the total cost in a variable. Write the message below to the output file using PrintWriter.printf() to ensure 2 digits after the decimal point in the total cost.


NOTE: Test your code by running it on the input file and checking the output file. To really ensure your program is correct, create another test file with the same format as music.txt, but a different number of titles and different data. For final testing we will test your code with a different file than music.txt, so you should not hard-code the filename, and your methods should work on an arbitrary number of titles. However, the file format will not change.

Please follow the usual rules for submitting Java programs.

Grading Criteria


Submit your source file named Music.java to the the Checkin tab on the course web site.

© 2015 CS160 Colorado State University. All Rights Reserved.