fork download
  1. //Alex Silva copyright (c) 2013
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Scanner;
  7.  
  8. public class puzzle2Driver {
  9.  
  10. //DRIVER FIELDS
  11. private static Scanner scn;
  12. private static ArrayList<Song> arrInput;
  13. private static String[] tokens;
  14. private static String strIn;
  15. private static int nSongs;
  16. private static int nSelects;
  17.  
  18. //MAIN METHOD
  19. public static void main(String[] args) throws IOException {
  20.  
  21. scn = null;
  22.  
  23. try {
  24. scn = new Scanner(System.in);
  25.  
  26. //accept first line of input
  27. //split and parse entries; add to values to variables
  28. if(scn.hasNext()){
  29. strIn = scn.nextLine();
  30. tokens = strIn.split(" ");
  31. nSongs = parseToInt(tokens[0].trim());
  32. nSelects = parseToInt(tokens[1].trim());
  33. arrInput = new ArrayList<Song>(nSongs);
  34. }
  35.  
  36. //accept and record first song info in input array
  37. if(scn.hasNext()){
  38. strIn = scn.nextLine();
  39. tokens = strIn.split(" ");
  40. //add first song to array
  41. arrInput.add((new Song(parseToInt(tokens[0].trim()), tokens[1].trim(), 0)));
  42. //set quality value to 1
  43. arrInput.get(0).setnQuality(1.0);
  44. //if first song has 0 plays
  45. if(arrInput.get(0).getnPlayNum()==0){
  46. arrInput.get(0).setnPlayNum(1);
  47. }
  48. }
  49.  
  50. //if only one song is added
  51. if(nSongs==1){
  52. System.out.println(arrInput.get(0).getSongName());
  53. }
  54.  
  55. //otherwise, proceed:
  56. else{
  57.  
  58. Song songIn;
  59. double tempQ;
  60. int i=1;
  61. while (scn.hasNext()) {
  62. strIn = scn.nextLine();
  63. tokens = strIn.split(" ");
  64. //create and add song to array
  65. songIn = new Song(parseToInt(tokens[0].trim()), tokens[1].trim(), i+2);
  66. arrInput.add(songIn);
  67. //calculate quality using:
  68. //q(i) = song(i).plays / (song(0).plays / i )
  69. tempQ = (double)arrInput.get(i).getnPlayNum() / ((double)arrInput.get(0).getnPlayNum() / (double)(i+1));
  70. //set Quality value to current song
  71. arrInput.get(i).setnQuality(tempQ);
  72. i++;
  73. if(i==nSongs){
  74. break;
  75. }
  76. }
  77.  
  78. //sort array of songs by quality value
  79. Collections.sort(arrInput);
  80.  
  81. //prints only the number of songs defined earlier
  82. for (i = 0; i < nSelects; i++) {
  83. System.out.println(arrInput.get(i).getSongName());
  84. }
  85. }
  86. }
  87.  
  88. catch(Exception e){
  89. System.out.println("Exception caught");
  90. System.out.println(e.getStackTrace());
  91.  
  92. } finally {
  93. if (scn != null) {
  94. scn.close();
  95. }
  96. }
  97.  
  98. }
  99.  
  100. //helper method for parsing from String to integer
  101. private static int parseToInt(String strIn) {
  102. int nRes = -1;
  103. try{
  104. nRes = Integer.parseInt(strIn.trim());
  105. }
  106. System.out.println("Error parsing to Integer");
  107. }
  108. return nRes;
  109. }
  110.  
  111. //PRIVATE SONG UTILITY CLASS
  112. private static class Song implements Comparable<Song> {
  113.  
  114. //Song Fields
  115. private int nPlayNum;
  116. private String songName;
  117. private int nTrackNum;
  118. private double nQuality;
  119.  
  120. //Song constructor
  121. public Song(int nPlayNum, String songName, int nTrackNum){
  122. this.nPlayNum = nPlayNum;
  123. this.songName = songName;
  124. this.nTrackNum = nTrackNum;
  125. }
  126.  
  127. @Override
  128. public int compareTo(Song o) {
  129.  
  130. if(getnQuality() > o.getnQuality()){
  131. return -1;
  132. }
  133.  
  134. if(o.getnQuality() > getnQuality()){
  135. return 1;
  136. }
  137.  
  138. //if quality values are equal, put songs with lower track numbers first
  139. if(getnTrackNum() < o.getnTrackNum()){
  140. return -1;
  141. }
  142.  
  143. return 1;
  144.  
  145. //note: no way to return 0, since songs can't have the same track number
  146.  
  147. }
  148.  
  149. //GETTERS SETTERS
  150. public int getnPlayNum() {
  151. return nPlayNum;
  152. }
  153.  
  154. public void setnPlayNum(int nPlays) {
  155. nPlayNum = nPlays;
  156. }
  157.  
  158. public String getSongName() {
  159. return songName;
  160. }
  161.  
  162. public double getnQuality() {
  163. return nQuality;
  164. }
  165.  
  166. public void setnQuality(double nQuality) {
  167. this.nQuality = nQuality;
  168. }
  169.  
  170. public int getnTrackNum() {
  171. return nTrackNum;
  172. }
  173. }
  174. /* comment */
  175.  
  176.  
  177. }
  178.  
Compilation error #stdin compilation error #stdout 0.08s 380608KB
stdin
15 3
197812 re_hash
78906 5_4
189518 tomorrow_comes_today
39453 new_genious
210492 clint_eastwood
26302 man_research
22544 punk
19727 sound_check
17535 double_bass
18782 rock_the_house
198189 19_2000
13151 latin_simone
12139 starshine
11272 slow_country
10521 m1_a1
compilation info
Main.java:8: error: class puzzle2Driver is public, should be declared in a file named puzzle2Driver.java
public class puzzle2Driver {
       ^
1 error
stdout
Standard output is empty