Guest User

Untitled

a guest
Oct 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. private String author;
  2. private String name;
  3. private String year;
  4.  
  5. public Album(String a, String n, String y) { // constructor
  6.  
  7. author = a;
  8. name = n;
  9. year = y;
  10.  
  11. }
  12.  
  13. public String toString()
  14. {
  15.  
  16. return author +","+ name + "," + year;
  17.  
  18. }
  19.  
  20. public int compareTo(Album a) {
  21. // usually toString should not be used,
  22. // instead one of the attributes or more in a comparator chain
  23. return toString().compareTo(a.toString());
  24.  
  25. }
  26.  
  27. public Album collection[]= new Album[10];
  28.  
  29. private int numAlbums = 0;
  30.  
  31. public void Add (Album a){
  32.  
  33. if (numAlbums >= collection.length){
  34.  
  35. Album newcollection[]= new Album [collection.length * 2];
  36.  
  37. for (int n = 0; n < numAlbums; n ++){
  38.  
  39. newcollection[n] = collection[n];
  40. }
  41.  
  42. newcollection = collection;
  43.  
  44. }
  45.  
  46. collection[numAlbums] = a;
  47.  
  48. numAlbums = numAlbums + 1;
  49.  
  50. }
  51.  
  52. public String toString()
  53. {
  54. String details = "";
  55.  
  56. for ( int p = 0; p < collection.length ; p ++)
  57.  
  58. {
  59. details = details + collection[p] + "n" ;
  60.  
  61. }
  62.  
  63. details += "n";
  64.  
  65. return details;
  66. }
  67.  
  68. public static void main(String[] args) {
  69.  
  70. Collection c = new Collection();
  71.  
  72. c.Add( new Album("DaftPunk","Discovery","2001"));
  73. c.Add( new Album ("Pink Floid","The Dark Side Of The Moon","1973"));
  74. c.Add( new Album( "The Clash", "London Calling", "1979"));
  75.  
  76. System.out.print(c);
  77. }
Add Comment
Please, Sign In to add comment