// Template for COMPSCI 220 Assignment, University of Auckland // Mark Wilson // May be freely used and modified - please keep this header intact import java.util.*; import java.io.*; public class ChooseSongs { public static void main(String [] args) { // input songs String f = args[0]; int k = Integer.parseInt(args[1]); ArrayList l = getSongsFromFile(f); chooseSongs(l, k); // you should write this method } //end main public static ArrayList getSongsFromFile(String f) { ArrayList l = new ArrayList(); try { BufferedReader reader = new BufferedReader(new FileReader(f)); String line = new String(); while (true) { line = reader.readLine(); if (line == null) break; String [] s = line.split("&",3); String t = s[0]; String c = s[1]; int r = Integer.parseInt(s[2]); Song sg = new Song(t,c,r); l.add(sg); } // end while loop } catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); } catch(IOException e) { System.err.println("IOException: " + e.getMessage()); } finally { } return l; } //end getSongsFromFile public static void printSongs(ArrayList l) { for (Song s : l) { System.out.println(s.toString()); } return; } // end printSongs // FILL IN ANY OTHER CLASSES YOU NEED public static void chooseSongs(ArrayList l, int k) { // FILL IN WITH YOUR NICE ALGORITHMIC SOLUTION, INCLUDING OUTPUT } //end chooseSongs } // end ChooseSongs // FILL IN ANY OTHER CLASSES YOU NEED class Song { String _title = ""; String _composer = ""; int _length = 0; // Constructor public Song(String t, String c, int r) { _title = t; _composer = c; _length = r; } // Accessor methods String gettitle() { return _title; } String getcomposer() { return _composer; } int getlength() { return _length; } // Mutator methods void settitle(String t) { _title = t; } void setcomposer(String c) { _composer = c; } void setlength(int r) { _length = r; } // Output public String toString() { return _title + "&" + _composer + "&" + _length; } } // end of Song class