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 [ 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 t; artist - a; duration -m * 60 + s; public String getTitle) return title public String getArtist) f return artist; public int getDuration() ( return duration; ) public int getMinutes) return duration / 60 public int getSeconds ) f return duration % 60; public String toString ) return (\ + title \ by + artist + (duration / 60) + : + (duration% 60));

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;
    
    public User()  { this(""); }
    
    public User(String u)  {
        userName = u;
        online = false;
    }
    
    public String getUserName() { return userName; }
    public boolean isOnline() { return online; }
    
    public String toString()  {
        String s = "" + userName + ": XXX songs (";
        if (!online) s += "not ";
        return s + "online)";
    }
    
    // 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;
    }
}
public class MusicExchangeTestProgram {
    public static void main(String args[]) {
        // Create a new music exchange center
        MusicExchangeCenter   mec = new MusicExchangeCenter();

        // Create some users and give them some songs
        User discoStew = User.DiscoStew();
        User sleepingSam = User.SleepingSam();
        User ronnieRocker = User.RonnieRocker();
        User countryCandy = User.CountryCandy();
        User peterPunk = User.PeterPunk();

        // Register the users, except SleepingSam
        discoStew.register(mec);
        ronnieRocker.register(mec);
        countryCandy.register(mec);
        peterPunk.register(mec);

        // Display the state of things before anyone logs on
        System.out.println("Status: " + mec);
        System.out.println("On-Line Users: " + mec.onlineUsers());
        System.out.println("Available Songs: " + mec.allAvailableSongs() + "\n");

        // Attempt to log on two registered users and one unregistered user
        discoStew.logon(mec);
        sleepingSam.logon(mec); // Should not work
        ronnieRocker.logon(mec);
        System.out.println("Status: " + mec);
        System.out.println("On-Line Users: " + mec.onlineUsers());
        System.out.println("Available Songs: " + mec.allAvailableSongs() + "\n");

        // Log on two more users
        countryCandy.logon(mec);
        peterPunk.logon(mec);
        System.out.println("Status: " + mec);
        System.out.println("On-Line Users: " + mec.onlineUsers());
        System.out.println("Available Songs: " + mec.allAvailableSongs());
        System.out.println("Available Songs By Jaw: " +
                mec.availableSongsByArtist("Jaw") + "\n");

        // Log off three users (one is not even logged in)
        countryCandy.logoff(mec);
        discoStew.logoff(mec);
        sleepingSam.logoff(mec);
        System.out.println("Status: " + mec);
        System.out.println("On-Line Users: " + mec.onlineUsers());
        System.out.println("Available Songs: " + mec.allAvailableSongs());
        System.out.println("Available Songs By Jaw: " +
                mec.availableSongsByArtist("Jaw") + "\n");

        // Log off the last two users
        peterPunk.logoff(mec);
        ronnieRocker.logoff(mec);
        System.out.println("Status: " + mec);
        System.out.println("On-Line Users: " + mec.onlineUsers());
        System.out.println("Available Songs: " + mec.allAvailableSongs());
        System.out.println("Available Songs By Jaw: " +
                mec.availableSongsByArtist("Jaw") + "\n");
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Song class:

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));

}

}

User class:

import java.util.ArrayList;

public class User {

private String userName;

private boolean online;

private ArrayList<Song> songList=new ArrayList<>();

  

public User() {

}

  

public User(String u,ArrayList<Song> songs) {

userName = u;

online = false;

songList=songs;

}

  

public String getUserName() { return userName; }

public boolean isOnline() { return online; }

  

public String toString() {

String s = "" + userName + ": "+this.songList.size() +"songs (";

if (!online) s += "not ";

return s + "online)";

}

  

public ArrayList<Song> getSongList() {

return songList;

}

public void setSongList(ArrayList<Song> songList) {

this.songList = songList;

}

// Various Users for test purposes

public static User DiscoStew() {

ArrayList<Song> sList=new ArrayList<>();

User discoStew = new User("Disco Stew",sList);

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() {

ArrayList<Song> sList=new ArrayList<>();

User sleepingSam = new User("Sleeping Sam",sList);

sleepingSam.addSong(new Song("Meadows", "Sleepfest", 7, 15));

sleepingSam.addSong(new Song("Calm is Good", "Waterfall", 6, 22));

return sleepingSam;

}

  

public static User RonnieRocker() {

ArrayList<Song> sList=new ArrayList<>();

User ronnieRocker = new User("Ronnie Rocker",sList);

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() {

ArrayList<Song> sList=new ArrayList<>();

User countryCandy = new User("Country Candy",sList);

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() {

ArrayList<Song> sList=new ArrayList<>();

User peterPunk = new User("Peter Punk",sList);

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;

}

public void addSong(Song s){

this.songList.add(s);

}

public int totalSongTime(){

int time=0;

for(Song s:songList){

time+=s.getDuration();

}

return time;

}

public void register(MusicExchangeCenter m){

User u=new User();

u.userName=this.userName;

u.songList=this.songList;

u.online=this.online;

m.registerUser(u);

}

public void logon(MusicExchangeCenter m){

User u=m.usersWithName(this.getUserName());

if(u!=null){

u.online=true;

}

}

public void logoff(MusicExchangeCenter m){

User u=m.usersWithName(this.getUserName());

if(u!=null && u.isOnline()){

u.online=false;

}

}

}

Musiccenter class:

import java.util.ArrayList;

public class MusicExchangeCenter {

private ArrayList<User> userList=new ArrayList<>();

public MusicExchangeCenter() {

// TODO Auto-generated constructor stub

}

public ArrayList<User> onlineUsers(){

ArrayList<User> onlineUsersList=new ArrayList<>();

for(User u:userList){

if(u.isOnline()){

onlineUsersList.add(u);

}

}

return onlineUsersList;

}

public ArrayList<Song> allAvailableSongs(){

ArrayList<Song> allSongList=new ArrayList<>();

for(User u:userList){

if(u.isOnline()){

allSongList.addAll(u.getSongList());

}

}

return allSongList;

}

public ArrayList<Song> availableSongsByArtist(String artist){

ArrayList<Song> allSongListArtist=new ArrayList<>();

for(User u:userList){

if(u.isOnline()){

for(Song s:u.getSongList()){

if(s.getArtist().equalsIgnoreCase(artist)){

allSongListArtist.add(s);

}

}

}

}

return allSongListArtist;

}

public String toString(){

return "Music Exchange Center("+onlineUsers().size()+" clients on line,"+allAvailableSongs().size()+" songs avaialble)";

}

public User usersWithName(String s){

User user=new User();

for(User u:userList){

if(u.getUserName().equalsIgnoreCase(s)){

user=u;

break;

}

}

return user;

}

public void registerUser(User x){

User u=usersWithName(x.getUserName());

if(u.getUserName()==null){

userList.add(x);

}

}

public ArrayList<User> getUserList() {

return userList;

}

public void setUserList(ArrayList<User> userList) {

this.userList = userList;

}

}

Tester class is same

Expected output:

Console es terminated> MusicExchangeTestProgram [Java Application]m FilesUavaljr80bin javaw.exe (Mar 22, 2018, 8:43:12 AM) ta

Add a comment
Know the answer?
Add Answer to:
public class Song { private String title; private String artist; private int duration; public Song() {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives: GUI Tasks: In Lab 4, you have completed a typical function of music player --...

    Objectives: GUI Tasks: In Lab 4, you have completed a typical function of music player -- sorting song lists. In this assignment, you are asked to design a graphic user interface (GUI) for this function. To start, create a Java project named CS235A4_YourName. Then, copy the class Singer and class Song from finished Lab 4 and paste into the created project (src folder). Define another class TestSongGUI to implement a GUI application of sorting songs. Your application must provide the...

  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i...

    class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i = i;} public void set(Upper n){ i = n.show();} public int show(){return i;} } class Middle extends Upper { private int j; private String name; public Middle(int i){ super(i+1); name = "Middle"; this.j = i;} public void set(Upper n){ j = n.show();} public int show(){return j;} } class Lower extends Middle { private int i; private String name; public Lower(int i){ super(i+1); name =...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact...

    Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact Disc.java (see Code Listing 7.2) and Classics.txt (see Code Listing 7.3) from the Student Files or as directed by your instructor. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by Compact Disc.java, the file you will be editing. 2. In Compact Disc.java, there are comments indicating where the missing code is to be placed. Declare...

  • You will be building a linked list. Make sure to keep track of both the head and tail nodes.

    Ch 8 Program: Playlist (Java)You will be building a linked list. Make sure to keep track of both the head and tail nodes.(1) Create two files to submit.SongEntry.java - Class declarationPlaylist.java - Contains main() methodBuild the SongEntry class per the following specifications. Note: Some methods can initially be method stubs (empty methods), to be completed in later steps.Private fieldsString uniqueID - Initialized to "none" in default constructorstring songName - Initialized to "none" in default constructorstring artistName - Initialized to "none"...

  • Consider the Automobile class: public class Automobile {    private String model;    private int rating;...

    Consider the Automobile class: public class Automobile {    private String model;    private int rating; // a number 1, 2, 3, 4, 5    private boolean isTruck;    public Automobile(String model, boolean isTruck) {       this.model = model;       this.rating = 0; // unrated       this.isTruck = isTruck;    }        public Automobile(String model, int rating, boolean isTruck) {       this.model = model;       this.rating = rating;       this.isTruck = isTruck;    }                public String getModel()...

  • Adapt your Rational class : public class Rational { private int num; private int denom; public...

    Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num...

  • public class File_In_36 {    private String filename;    public int[][] matrix()    {       //...

    public class File_In_36 {    private String filename;    public int[][] matrix()    {       // 1. matrix will call scan_File to determine whether or not the file       // name entered by the user actually exists       // 2. matrix will call size_matrix to determine the number of integers       // in the input file and set the instance variable matrix_size to that       // number       // 3. matrix will call check_square to determine whether or not matrix_size...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT