Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1) The Song/ User Classes Here are simple Song and User classes that represent

ID: 3734730 • Letter: #

Question

(1) The Song/ User Classes Here are simple Song and User classes that represent a song that is available at the Music Exchange Center and a user of the Music Exchange Center that logs in to download music: public class Song t private String private String private int title; artist; duration; public Song ) this("", "", 0, 0) public Song (String t, String a, int m, int s) owner- null; title-ti artist = a; duration= m * 60 + s; public String getTitle) return title; public String getArtist) return artist; public int getDuration) return duration; ) public int getMinutes) return duration 60 public int getSeconds ) I return duration % 60;

Explanation / Answer

public class Song {
private String title;
private String artist;
private int duration;

public Song() {
this("", "", 0, 0);
}

public Song(String t, String a, int m, int s) {
title = t;
artist = a;
duration = m * 60 + s;
}

public String getTitle() { return title; }
public String getArtist() { return artist; }
public int getDuration() { return duration; }

public int getMinutes() {
return duration / 60;
}

public int getSeconds() {
return duration % 60;
}

public String toString() {
return(""" + title + "" by " + artist + " " + (duration / 60) + ":" + (duration%60));
}
}

public class User {
private String userName;
private boolean online;
private Song[] songList= new Song[20]; // array of Song objects
int i,count=0; // variable i to To manage loops and variable Count to keep count of number of songs
  
public User() { this(""); }
  
public User(String u) {
  
userName = u;
> for(i=0;i<20;i++)
{
songList[i]= new Song();

// modified the constructor so that songList is initialised properly, since mentioned in the question explicitly;

// This modification is not required as Song() constructor is invoked implicitly when the array objects are created
}
  
}
  
public void getsongList(){ // get method to get the songList
  
for(i=0;i<20&&(songList[i].getTitle()!="");i++){
songList[i].toString();
}
  
}
  
public int totalSongTime(){ // a method to get the total duration of playlist
  
int total_dur=0;
  
for(i=0;i<20&&(songList[i].getTitle()!="");i++)
{
total_dur+=songList[i].getDuration();
}
  
return total_dur;
}
  
public String getUserName() { return userName; }
public boolean isOnline() { return online; }
  
public String toString() { // Modified to display the number of songs in a playlist;
  
for(i=0;i<20&&(songList[i].getTitle()!="");i++){
count++;
}
String s = "" + userName + ": "+ count +" songs (";
if (!online) s += "not ";
return s + "online)";
}
  
void addSong(Song s){ // A method to add a song to the playlist
for(i=0;i<20&&(songList[i].getTitle()=="");i++){
songList[i]= new Song(s.getTitle(),s.getArtist(),s.getDuration()/60 ,s.getDuration()%60);
break;
}
}
  
// Various Users for test purposes
public static User DiscoStew() {
User discoStew = new User("Disco Stew");
discoStew.addSong(new Song("Hey Jude", "The Beatles", 4, 35));
discoStew.addSong(new Song("Barbie Girl", "Aqua", 3, 54));
discoStew.addSong(new Song("Only You Can Rock Me", "UFO", 4, 59));
discoStew.addSong(new Song("Paper Soup Cats", "Jaw", 4, 18));
return discoStew;
}
  
public static User SleepingSam() {
User sleepingSam = new User("Sleeping Sam");
sleepingSam.addSong(new Song("Meadows", "Sleepfest", 7, 15));
sleepingSam.addSong(new Song("Calm is Good", "Waterfall", 6, 22));
return sleepingSam;
}
  
public static User RonnieRocker() {
User ronnieRocker = new User("Ronnie Rocker");
ronnieRocker.addSong(new Song("Rock is Cool", "Yeah", 4, 17));
ronnieRocker.addSong(new Song("My Girl is Mean to Me", "Can't Stand Up", 3, 29));
ronnieRocker.addSong(new Song("Only You Can Rock Me", "UFO", 4, 52));
ronnieRocker.addSong(new Song("We're Not Gonna Take It", "Twisted Sister", 3, 9));
return ronnieRocker;
}
  
public static User CountryCandy() {
User countryCandy = new User("Country Candy");
countryCandy.addSong(new Song("If I Had a Hammer", "Long Road", 4, 15));
countryCandy.addSong(new Song("My Man is a 4x4 Driver", "Ms. Lonely", 3, 7));
countryCandy.addSong(new Song("This Song is for Johnny", "Lone Wolf", 4, 22));
return countryCandy;
}
  
public static User PeterPunk() {
User peterPunk = new User("Peter Punk");
peterPunk.addSong(new Song("Bite My Arms Off", "Jaw", 4, 12));
peterPunk.addSong(new Song("Where's My Sweater", "The Knitters", 3, 41));
peterPunk.addSong(new Song("Is that My Toenail ?", "Clip", 4, 47));
peterPunk.addSong(new Song("Anvil Headache", "Clip", 4, 34));
peterPunk.addSong(new Song("My Hair is on Fire", "Jaw", 3, 55));
return peterPunk;
}
}