CSCI 161: Introduction to Programming 1
Lab 10


Overview

Write a program to process a file containing information about your music collection. Each song in your song library will have a name, artist, and length (in seconds). Read this data into three arrays, two for strings (for title and artist) and one for integers (for length). Then perform the processing below.

  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.
  5. Output the song titles in alphabetical order, WITHOUT breaking the correspondence between titles, artists, and times.

You are free to structure your solution as desired. However, your program will be graded based on DESIGN as well as functionality (i.e., whether or not you correctly compute each item in the list above and format it properly). With regard to DESIGN, use reasonable methods, parameters, and call hierarchy. If you code everything in the main method you'll receive ALMOST NO credit. Exercise your design skills.

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

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. Right justify the time in a field of 4 characters.

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

SONGS
-----
Cherub Rock
Float On
Portions for Foxes
Teen Age Riot
The Price You Pay

Required Methods

// From the Arrays class
sort 

// From the String class
split

// From the Integer class
parseInt

// All other methods are up to you

Extra Credit (+10)

In addition to the output above, print the data a second time in a table that is sorted by ARTIST. For the input above, the output would be as follows:

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

You may use the String class's "compareTo" method to determine if one string is "less than" another:

int compareResult = s1.compareTo(s2);
if (compareResult < 0) {
  // s1 precedes s2, alphabetically
} else if (compareResult == 0) {
  // s1 equals s2
} else if (compareResult > 0) {
  // s1 follows s2, alphabetically
}

Hints

Reading the first integer (which gives the number of songs in the file) must be done in two stages to ensure the rest of the data can be read properly. In stage one, read the number as you would any integer, e.g.,

int x = input.nextInt ();

In stage two, discard the newline character that is still embedded in the file. This can be done with

input.nextLine ();

The rest of the input should be read as one string per line. Use the String split method to separate each line into an array of three strings. Note, however, that the first string is representing an integer. You must convert this string into an integer before storing it in the integer array. This can be done using the parseInt method of the Integer class. For example, the following code takes a string value and converts it into an integer:

String intAsString = "159";
int x = Integer.parseInt (intAsString);

The "split" method of the String class returns an ARRAY OF STRINGS by breaking the string apart at the specified delimeter. In this case the delimeter is a colon (":"). The split method can be called as follows:

String[] parts = stringToSplit.split (":");

In order to do the EXTRA CREDIT sorting, you'll need to write your OWN sort routine because you must rearrange all three arrays at the same time, based on the artist string.


Gary M. Zoppetti, Ph.D.