w3resource

Java: Manage a music library of songs

Java OOP: Exercise-15 with Solution

Write a Java program to create a class called "MusicLibrary" with a collection of songs and methods to add and remove songs, and to play a random song.

Sample Solution:

Java Code:

//MusicLibrary.java

import java.util.ArrayList;
import java.util.Random;

public class MusicLibrary {
  private ArrayList < Song > songs;

  public MusicLibrary() {
    songs = new ArrayList < Song > ();
  }
  public void addSong(Song song) {
    songs.add(song);
  }
  public void removeSong(Song song) {
    songs.remove(song);
  }
  public ArrayList < Song > getSongs() {
    return songs;
  }
  public void playRandomSong() {
    int size = songs.size();
    if (size == 0) {
      System.out.println("No songs in the library.");
      return;
    }
    Random rand = new Random();
    int index = rand.nextInt(size);
    System.out.println("Now playing: " + songs.get(index).getTitle() + " by " + songs.get(index).getArtist());
  }
}

The above “MusicLibrary” class represents a library of songs. It uses an ArrayList to store the songs, and provides methods to add and remove songs from the library. It also has a method to get a list of all the songs in the library, and a method to play a random song from the library.

//Song.class
public class Song {
    private String title;
    private String artist;

    public Song(String title, String artist) {
        this.title = title;
        this.artist = artist;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }
}

The “Song” class represents a song in a music library. It has two private instance variables, the title and the artist, and a constructor that takes these two variables as parameters. The class also provides getters and setters for the title and artist variables, allowing the client code to access and modify the song information.

//Main.java
import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    MusicLibrary library = new MusicLibrary();
    library.addSong(new Song("Midnight Train to Georgia", "Gladys Knight & the Pips"));
    library.addSong(new Song("Stairway to Heaven", "Led Zeppelin"));
    library.addSong(new Song("Imagine", "John Lennon"));
    library.addSong(new Song("All by Myself", "Eric Carmen"));
    library.addSong(new Song("What'd I Say", "Ray Charles"));
    library.addSong(new Song("Walking in Memphis", "Marc Cohn"));
    library.addSong(new Song("In the Air Tonight", "Phil Collins"));

    System.out.println("All songs:");
    for (Song song: library.getSongs()) {
      System.out.println(song.getTitle() + " by " + song.getArtist());
    }
    System.out.println("\n**Playing Random Song**");
    library.playRandomSong();
    System.out.println();
    library.playRandomSong();
    System.out.println();
    library.playRandomSong();
  }
}

The Main class creates an instance of MusicLibrary and adds several Song objects to it. It then retrieves and prints out the list of all songs in the library. Finally, it calls the playRandomSong() method of the MusicLibrary object three times to play a random song each time, printing out the details of the played song.

Sample Output:

All songs:
Midnight Train to Georgia by Gladys Knight & the Pips
Stairway to Heaven by Led Zeppelin
Imagine by John Lennon
All by Myself by Eric Carmen
What'd I Say by Ray Charles
Walking in Memphis by Marc Cohn
In the Air Tonight by Phil Collins

**Playing Random Song**
Now playing: Imagine by John Lennon

Now playing: Midnight Train to Georgia by Gladys Knight & the Pips

Now playing: What'd I Say by Ray Charles

Flowchart:

Flowchart: Java  OOP Exercises: Manage a music library of songs.
Flowchart: Java  OOP Exercises: Manage a music library of songs.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: School Management System.
Java OOP Next: Calculate the area and perimeter of shapes.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.