Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.LinkedList;
  3. import java.util.ListIterator;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * This class encapsulates an album of music. It stores the artist, and the name
  8. * of the album, as well as a list of tracks.
  9. */
  10. public class Album {
  11. private String artist;
  12. private String name;
  13.  
  14. private LinkedList<Track> tracks;
  15.  
  16. /**
  17. * Constructor. Complete the constructor. Initialize the LinkedList.
  18. *
  19. * @param artist
  20. * the album artist
  21. * @param name
  22. * the name of the album
  23. */
  24. public Album(String artist, String name) {
  25. this.artist = artist;
  26. this.name = name;
  27. new LinkedList<Track>();
  28. // YOUR CODE HERE (construct the LinkedList)
  29. }
  30.  
  31. // accessors and mutators
  32. public String getArtist() {
  33. return artist;
  34. }
  35.  
  36. public String getName() {
  37. return name;
  38. }
  39.  
  40. public void setArtist(String artist) {
  41. this.artist = artist;
  42. }
  43.  
  44. public void setName(String name) {
  45. this.name = name;
  46. }
  47.  
  48. /**
  49. * Create a new album by reading it from a file. Return it.
  50. *
  51. * Each data item will be on its own line. The first line will hold the
  52. * artist. The second line will hold the name of the album. Each subsequent
  53. * line will hold a track name.
  54. *
  55. * Example: Radiohead Kid A Everything in Its Right Place Kid A The National
  56. * Anthem How to Disappear Completely Treefingers Optimistic In Limbo
  57. * Idioteque Morning Bell Motion Picture Soundtrack
  58. *
  59. * If an exception occurs, print "An exception has occurred. Unable to load
  60. * the album in filename!" where filename is replaced with the actual
  61. * filename.
  62. *
  63. * @param filename
  64. * the path to the file from which to load album information
  65. * @return the album created from the file
  66. */
  67. public static Album fromFile(String filename) {
  68.  
  69. String artist = "";
  70. String name = "";
  71. new Album(artist, name);
  72. try {
  73. Scanner in = new Scanner(new File(filename));
  74. if (in.hasNextLine()) {
  75. artist = in.nextLine();
  76. name = in.nextLine();
  77. new Album(artist, name);
  78. }
  79. while (in.hasNextLine()) {
  80. Track current = new Track(artist, in.nextLine());
  81. addTrack(current);
  82. }
  83. in.close();
  84. return new Album(artist, name);
  85. } catch (Exception e) {
  86. System.out
  87. .println("An exception has occurred. Unable to load the album in "
  88. + filename + "!");
  89. }
  90. }
  91.  
  92. /**
  93. * addTrack. Add a track to the LinkedList.
  94. *
  95. * @param t
  96. * the track to add
  97. */
  98. public void addTrack(Track t) {
  99. // YOUR CODE HERE
  100. tracks.add(t);
  101. }
  102.  
  103. /**
  104. * addTrackAt. Insert a track into the LinkedList at the position indicated
  105. * by index.
  106. *
  107. * @param index
  108. * where to insert
  109. * @param t
  110. * the track to insert
  111. */
  112. public void addTrackAt(int index, Track t) {
  113. // YOUR CODE HERE
  114. tracks.add(index, t);
  115. }
  116.  
  117. /**
  118. * removeTrackAt. Remove a track at a specific index.
  119. *
  120. * @param index
  121. * the index at which to remove
  122. */
  123. public void removeTrackAt(int index) {
  124. // YOUR CODE HERE
  125. tracks.remove(index);
  126. }
  127.  
  128. /**
  129. * getTrackAt. Return the track at the given index.
  130. *
  131. * @param index
  132. * the index at which to return
  133. * @return
  134. */
  135. public Track getTrackAt(int index) {
  136. // YOUR CODE HERE
  137. return tracks.get(index);
  138. }
  139.  
  140. /**
  141. * printTrackList. Print a numbered list of tracks. YOU MUST USE A
  142. * ListIterator TO GET FULL CREDIT!
  143. *
  144. * Example output: 1. Sweet Tune 2. Great Track 3. Probably a cool song
  145. */
  146. public void printTrackList() {
  147. // YOUR CODE HERE
  148. ListIterator<Track> itr = tracks.listIterator();
  149. int i = 0;
  150. while (itr.hasNext()) {
  151. i++;
  152. System.out.print(" " + i + ". " + itr.next());
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement