Colorado State University

Recitation R20 - Classes, Objects, Methods
Fall 2013

CS160: Foundations in Programming

The goal of this lab is:

Classes, Objects, Methods

Write the following program from scratch in the following phases:

Phase 1

  1. Create a new project in Eclipse called R20.
  2. Define a new class called Music.java, without 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 initializes all variables and methods to get individual variables, as follows:
        // 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. Implement the following method to get the price of the title:
        public double getPrice() { }
        
    The algorithm for determining the price is $1.09 for titles released before 1970, $1.59 for titles released in the 1970's, $0.59 for titles released in the 1980's, $1.19 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!

Phase 2

  1. Create a new class called R20.java, with a main method.
  2. Declare (but do not allocate) an array of Music objects as a class variable (static).
  3. Implement a static method readFile which takes a String parameter with the filename.
  4. Call readFile from your main with args[0] as the parameter.
  5. Here is the data file that you will read for this program: music.txt
  6. Implement readFile as follows:


  7. Store the data read from the file in the array of Music objects.
  8. NOTE: Even after allocating the array, you must instantiate an individual Music object (by calling the constructor) for each array element. The TA will explain why.
  9. NOTE: 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. The TA will explain why.

Phase 3

  1. After calling readFile in the main method, iterate the array of Music objects and print out each entry, as follows:
        0: <title 0>, <album 0>, <artist 0>, <year 0>
        1: <title 1>, <album 1>, <artist 1>, <year 1>
        2: <title 2>, <album 2>, <artist 2>, <year 2>
        ...
        n: <title n>, <album n>, <artist n>, <year n>
        
  2. Make sure that your program correctly reports all titles in the file.
  3. Figure out how much it would cost to buy all titles in the file and print it out:
        Total cost: $13.68
        

Show your R20.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2013 CS160 Colorado State University. All Rights Reserved.