Class Album


public class Album extends Object
Represents an album consisting of a stack of songs. The Album class allows adding and removing songs in LIFO order.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private String
    The name of the album.
    private int
    The number of songs currently in the album.
    private LinkedStack<Song>
    Stack to store songs in the album.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Album(String albumName)
    Constructs an empty Album with a new LinkedStack to store song and size as zero.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addSong(Song s)
    Adds a song to the top of the album's track list and adds the Album reference to the song.
    Song
    Retrieves the song that is currently at the top of the album's track list, without removing it from the stack.
    Retrieves the name of the album.
    Song
    Removes the most recently added song from the album.
    int
    Returns the number of songs currently in the album.
    Returns a string representation of the album, with the name of the album as the first line and listing all songs from the top of the stack to the bottom.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • trackList

      private LinkedStack<Song> trackList
      Stack to store songs in the album.
    • albumName

      private String albumName
      The name of the album.
    • size

      private int size
      The number of songs currently in the album.
  • Constructor Details

    • Album

      public Album(String albumName)
      Constructs an empty Album with a new LinkedStack to store song and size as zero.
      Parameters:
      albumName - the name of the album
      Throws:
      IllegalArgumentException - if the name is null or empty
  • Method Details

    • addSong

      public void addSong(Song s)
      Adds a song to the top of the album's track list and adds the Album reference to the song.
      Parameters:
      s - the Song object to be added to the album
      Throws:
      IllegalArgumentException - if the song already exists in the album.
    • removeSong

      public Song removeSong()
      Removes the most recently added song from the album.
      Returns:
      the Song object removed from the top of the album
      Throws:
      NoSuchElementException - if the album is empty
    • firstSong

      public Song firstSong()
      Retrieves the song that is currently at the top of the album's track list, without removing it from the stack.
      Returns:
      the Song object at the top of the album, or null if the album is empty
    • getAlbumName

      public String getAlbumName()
      Retrieves the name of the album.
      Returns:
      the the name of the album.
    • size

      public int size()
      Returns the number of songs currently in the album.
      Returns:
      the number of songs in the album
    • toString

      public String toString()
      Returns a string representation of the album, with the name of the album as the first line and listing all songs from the top of the stack to the bottom. The output string should separate Songs using a new line (\n). Top of the stack should be the first line of the string.
      Overrides:
      toString in class Object
      Returns:
      a string listing all songs in the album in LIFO order (top of stack comes FIRST)