CSCI 161 — Lab 11

Due: Friday, May 4, 2018 @ 11:59PM

NOTE THE MODIFIED DUE DATE

Overview

Extend the last lab (MUTunes) to use Objects

  1. Output the song titles, artists, and times in a table.
  2. Determine and output the total length of all songs.
  3. Determine and output the longest song.
  4. Determine and output the shortest song.

Input Specification

The name of the input file MUST BE SPELLED EXACTLY AS Music.txt. Any deviations will result in a ZERO as none of my test cases will work. The file starts with an integer on the first line that specifies the song count. Each line afterwards represents a song. The song lines will be formatted thus:

Time:Title:Artist

For example, the file might contain the following data:

5
212:Float On:Modest Mouse
259:Cherub Rock:Smashing Pumpkins
351:The Price You Pay:Bruce Springsteen
417:Teen Age Riot:Sonic Youth
285:Portions for Foxes:Rilo Kiley

Use PRECISELY the file format described above.

The colons delimit the data on each line, which allows us to have spaces in artist and song names. You can use the colons to separate the three fields so you can place each one in the proper array (see the Hints section).

Output Specification

Output the data from the file in three columns, in the order of title, artist, and time. Left justify the title and artist in a field of 24 characters (%-24s). Right justify the time in a field of 4 characters (%4d).

Include a total for the times. Then print the longest and shortest songs. Finally, output the song titles in alphabetical order. MATCH the output format below EXACTLY.

For the file above, you would output the following.

TITLE                   ARTIST                  TIME
-----                   ------                  ----
Float On                Modest Mouse             212
Cherub Rock             Smashing Pumpkins        259
The Price You Pay       Bruce Springsteen        351
Teen Age Riot           Sonic Youth              417
Portions for Foxes      Rilo Kiley               285

TOTAL TIME                                      1524

LONGEST SONG
------------
Teen Age Riot           Sonic Youth              417

SHORTEST SONG
-------------
Float On                Modest Mouse             212

Required Code

Song.java

/* Filename:    Song.java
 * Author:      Your name
 * Course:      CSCI 161 (Section 00)
 * Date:        Today's date
 * Assignment:  Lab 11
 * Description: A simple Song class used for a Music Library
 */

public class Song {

    // TODO: three fields named "title", "artist", and "length"

    /**
     * Creates a new song from a title, artist, and length
     * @param trackTitle the track title
     * @param trackArtist the track artist
     * @param trackLength the track length (duration in seconds)
     */
    public Song(String trackTitle, String trackArtist, int trackLength) {
        // TODO: initialize each member
    }

    /**
     * prints a song to System.out using formatted output (printf)
     */
    public void print() {
        // TODO: use System.out.printf -- remember the trailing newline character!
    }
}

MusicLibrary.java

/* Filename:    MusicLibrary.java
 * Author:      Your name
 * Course:      CSCI 161 (Section 00)
 * Date:        Today's date
 * Assignment:  Lab 11
 * Description: A simple MusicLibrary class which manages a collection of Songs
 */

import java.util.Scanner;

public class MusicLibrary {

    // TODO: there is one field of type Song Array -- name it "songs"

    /**
     * Constructs a new music library from a file
     * @param fileScanner the Scanner to read library information from
     */
    public MusicLibrary(Scanner fileScanner) {
        // TODO: grab song count from fileScanner and advance to next line

        // TODO: create song array

        // TODO: process line-by-line and add parsed data to the song array
    }

    /**
     * prints the library as a table
     */
    public void print() {
        // TODO: print out the header first, then have a for-loop which
        //       will tell each song to print itself
    }

    /**
     * Accumulates the sum of all of the songs in the library
     * @return the total time (in seconds)
     */
    public int getTotalTime() {
        // TODO: similar to last lab
    }

    /**
     * Finds the longest song in the library
     * @return the song with the longest track length
     */
    public Song getLongestSong() {
        // TODO: similar to last lab, but return the song instead of the index
    }

    /**
     * Finds the shortest song in the library
     * @return the song with the shortest track length
     */
    public Song getShortestSong() {
        // TODO: similar to last lab, but return the song instead of the index
    }
}

Sample MUTunes.java

/* Filename:    MUTunes.java
 * Author:      Professor Killian
 * Course:      CSCI 161
 * Date:        April 23, 2018
 * Assignment:  Lab 11
 * Description: A sample MUTunes driver program
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MUTunes {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("Music.txt"));
        MusicLibrary library = new MusicLibrary(sc);

        library.print();
        System.out.println();

        int totalTime = library.getTotalTime();
        System.out.printf("%-48s%4d\n\n", "TOTAL TIME", totalTime);

        Song longest = library.getLongestSong();
        System.out.println("LONGEST SONG");
        System.out.println("------------");
        longest.print();
        System.out.println();

        Song shortest = library.getShortestSong();
        System.out.println("SHORTEST SONG");
        System.out.println("-------------");
        shortest.print();
        System.out.println();
    }
}

Hints

Everything you have to implement is marked with TODO

Please test that everything is working with the provided MUTunes.java file

Submission

Please update both Song.java and MusicLibrary.java

Due to multiple files, I am modifying the submission directions slightly.

  1. File > Export...
  2. General > Archive File
  3. Click "Next"
  4. Expand the current project folder (Lab 11) and Click the "src" folder
  5. Check "MusicLibrary.java" and "Song.java" on the right side
  6. IMPORTANT: ensure that "Create only selected directories" is selected
  7. Click "Browse..." and save the ZIP file somewhere
  8. Upload that ZIP file to Autolab under the "Lab 11" assignment.

Grading