Homework assignment 01
Due 2025-Sept-11
An online music streaming platform allows users to create accounts, listen to songs,
and download tracks.
The development team plans to redesign the system using object-oriented programming.
The following classes are considered in the initial design:
public class User {
private int userID;
private String userName;
private Playlist[] playlists = new Playlist[5];
private int playlistCount = 0;
public User(int userID, String userName) {
[Link] = userID;
[Link] = userName;
}
public void addPlaylist(Playlist playlist) {
playlists[playlistCount] = playlist;
playlistCount++;
}
public Playlist getPlaylist(int index) {
return playlists[index];
}
public String getUserName() {
return userName;
}
}
A Playlist contains multiple Song objects, and there are two kinds of songs:
- FreeSong – can be streamed without payment
- PremiumSong – requires a subscription to play
(a) State two advantages of using an OOP approach for developing this system.
(b) Explain how encapsulation is applied in the User class to enhance security.
The design team decides to introduce a superclass Song and two subclasses FreeSong
and PremiumSong.
(c) Construct a UML diagram showing the inheritance relationship between these three
classes.
(d) Add a method play() to the Song superclass and use polymorphism to ensure that:
- [Link]() simply displays “Playing: <title>”.
- [Link]() first checks if the user has a valid subscription; if not, display
“Subscription required”.
Write the method for the Song superclass and its subclasses.
The platform tracks the total number of songs ever streamed across all users.
(e) Using the static keyword, explain how to implement this in the Song class so that
the count can be displayed without processing the entire user database.
(f) (HL) Currently, each playlist stores songs using an array. The development team
decides to replace it with an ArrayList<Song> to allow dynamic resizing.
(i) Rewrite the relevant part of the Playlist class to declare an
ArrayList<Song> instead of a fixed-size array.
(ii) Implement a method addSong(Song s) that adds a song to the playlist.
(iii) Explain one advantage of using an ArrayList instead of a fixed-size array
in this context.