Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. public class Song
  2. {
  3. private String name;
  4. private String author;
  5. private String preformer;
  6. private int length;
  7.  
  8. public Song(String name, String author, String preformer,int length)
  9. {
  10. this.name = name;
  11. this.author = author;
  12. this.preformer = preformer;
  13. this.length = length;
  14. }
  15.  
  16. public Song(Song s)
  17. {
  18. this.name = s.name;
  19. this.author = s.author;
  20. this.preformer = s.preformer;
  21. this.length = s.length;
  22. }
  23.  
  24. public String getName()
  25. {
  26. return this.name;
  27. }
  28.  
  29. public String getAuthor()
  30. {
  31. return this.author;
  32. }
  33.  
  34. public String getPreformer()
  35. {
  36. return this.preformer;
  37. }
  38.  
  39. public int getLength()
  40. {
  41. return this.length;
  42. }
  43.  
  44. public void setName(String name)
  45. {
  46. this.name = name;
  47. }
  48.  
  49. public void setAuthor(String author)
  50. {
  51. this.author = author;
  52. }
  53.  
  54. public void setPreformer(String preformer)
  55. {
  56. this.preformer = preformer;
  57. }
  58.  
  59. public void setLength(int length)
  60. {
  61. this.length = length;
  62. }
  63.  
  64. public String toString()
  65. {
  66. return this.name + "," + this.author + "," + this.preformer + "," + this.length;
  67. }
  68.  
  69. }//End of class song
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. public class RadioProgram
  78. {
  79. private int hour;
  80. private Song [] songArr;
  81. private int numSong;
  82. public final int arraySize = 30;
  83. public RadioProgram()
  84. {
  85. this.hour = 0;
  86. this.songArr = new Song[arraySize];
  87. this.numSong = 0;
  88. }
  89.  
  90. public boolean addSong(Song s)
  91. {
  92. if(numSong <30)
  93. {
  94. songArr[numSong] = new Song(s);
  95. return true;
  96. }
  97. return false;
  98. }
  99.  
  100. public void removeSong(Song s)
  101. {
  102. for(int i = 0; i < numSong; i++)
  103. if(songArr[i].getName() == s.getName())
  104. {
  105. songArr[i] = new Song(songArr[numSong]);
  106. songArr[numSong] = null;
  107. break;
  108. }
  109. numSong--;
  110. }
  111.  
  112. public int getHour()
  113. {
  114. return this.hour;
  115. }
  116.  
  117. public int getNumSong()
  118. {
  119. return this.numSong;
  120. }
  121.  
  122. public Song[] getSongArr()
  123. {
  124. Song [] a = new Song[arraySize];
  125. for(int i = 0; i < this.numSong; i++)
  126. a[i] = new Song(this.songArr[i]);
  127. return a;
  128. }
  129.  
  130. public void setHour(int hour)
  131. {
  132. this.hour = hour;
  133. }
  134.  
  135. public void setNumSong(int numSong)
  136. {
  137. this.numSong = numSong;
  138. }
  139.  
  140. }//End of class RadioProgram
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. import java.util.*;
  148. public class TestRadio
  149. {
  150. static Scanner reader = new Scanner(System.in);
  151. public static int castTime(RadioProgram rp)
  152. {
  153. int sum = 0 ;
  154. for(int i = 0; i < rp.getNumSong(); i++)
  155. sum += rp.getSongArr()[i].getLength();
  156. return sum;
  157. }
  158.  
  159. public static RadioProgram buildSongArray(int hour)
  160. {
  161. int sum = 0;
  162. RadioProgram rp = new RadioProgram();
  163. rp.setHour(hour);
  164. while(sum < 3600)
  165. {
  166. Song s = new Song(reader.next(),reader.next(),reader.next(),reader.nextInt());//Gets a song to input the array.
  167. rp.addSong(s);
  168. sum += s.getLength();
  169. }
  170. int temp = rp.getNumSong();
  171. Song [] a = rp.getSongArr();
  172. rp.removeSong(a[temp]);
  173. return rp;
  174. }
  175.  
  176.  
  177.  
  178.  
  179. public static void main(String[]args)
  180. {
  181. RadioProgram [] dayArray = new RadioProgram[24];
  182. for(int i = 0; i < dayArray.length; i++)
  183. dayArray[i] = buildSongArray(i);
  184. for(int i = 0; i <dayArray.length; i++)
  185. System.out.println(castTime(dayArray[i]));
  186.  
  187. }//End of main method
  188. }//End of class TestRadio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement