Due: Monday, April 30, 2018 @ 8:00PM
NOTE THE MODIFIED DUE DATE
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.
Use at least the REQUIRED METHODS listed below, with the exact same SPELLING for method names and parameters. You are free to add any other methods.
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 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
SONGS
-----
Cherub Rock
Float On
Portions for Foxes
Teen Age Riot
The Price You Pay
// from the Arrays class
sort()
// from the String class
split()
// from the Integer class
parseInt()
public static void main(String[] args) throws FileNotFoundException
/**
* Prints a table of songs
* @param param1 the song title array
* @param param2 the song artist array
* @param param3 the song length array
*/
public static void printTable(/* determine parameter types and names */)
/**
* Prints a single song
* @param param1 the song title array
* @param param2 the song artist array
* @param param3 the song length array
* @param param3 the zero-based index of the song to print
*/
public static void printSong(/* determine parameter types and names */)
/**
* Calculates the total time of all songs
* @param param1 the array of song lengths
* @return the sum of the times for all songs passed
*/
public static int getTotalTime(/* determine type and name */)
/**
* Finds the zero-based index of the longest song in the library
* @param param1 the array of song lengths
* @return the index of the song with the longest length
*/
public static int longestSongIndex(/* determine type and name */)
/**
* Finds the zero-based index of the shortest song in the library
* @param param1 the array of song lengths
* @return the index of the song with the shortest length
*/
public static int shortestSongIndex(/* determine type and name */)
All arrays should be declared and initialized in main()
. Several methods may require arrays to be used as parameters.
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 (":");
Please name your program MUTunes.java
Submit the Java source code file on Autolab under the "Lab 10" assignment.
longestSongIndex
properly implementedshortestSongIndex
properly implemented