Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help fix my MusicExchangeCenter code import javafx.util.Pair; public class MusicExchangeTestProgram3 { public static void main(String args[]) { // Create a new music exchange center MusicExchangeCenter

Help fix my MusicExchangeCenter code

image text in transcribed

import javafx.util.Pair; public class MusicExchangeTestProgram3 { 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 discoStew.register(mec); ronnieRocker.register(mec); sleepingSam.register(mec); countryCandy.register(mec); peterPunk.register(mec); // Log on all users discoStew.logon(mec); sleepingSam.logon(mec); ronnieRocker.logon(mec); countryCandy.logon(mec); peterPunk.logon(mec); System.out.println("Status: " + mec); // Simulate users downloading various songs discoStew.downloadSong(mec, "Bite My Arms Off", "Peter Punk"); discoStew.downloadSong(mec, "Meadows", "Sleeping Sam"); discoStew.downloadSong(mec, "If I Had a Hammer", "Country Candy"); discoStew.downloadSong(mec, "Is that My Toenail ?", "Peter Punk"); sleepingSam.downloadSong(mec, "Anvil Headache", "Peter Punk"); sleepingSam.downloadSong(mec, "Is that My Toenail ?", "Disco Stew"); sleepingSam.downloadSong(mec, "If I Had a Hammer", "Country Candy"); countryCandy.downloadSong(mec, "Anvil Headache", "Peter Punk"); countryCandy.downloadSong(mec, "Meadows", "Sleeping Sam"); countryCandy.downloadSong(mec, "If I Had a Hammer", "Peter Punk"); countryCandy.downloadSong(mec, "Only You Can Rock Me", "Ronnie Rocker"); countryCandy.downloadSong(mec, "Is that My Toenail ?", "Disco Stew"); peterPunk.downloadSong(mec, "Is that My Toenail ?", "Country Candy"); peterPunk.downloadSong(mec, "Rock is Cool", "Ronnie Rocker"); peterPunk.downloadSong(mec, "What?", "Ronnie Rocker"); peterPunk.downloadSong(mec, "Meadows", "Sleeping Sam"); // Display the downloaded songs System.out.println(" Here are the downloaded songs: "); for (Song s: mec.getDownloadedSongs()) System.out.println(s); // Display the downloaded songs alphabetically System.out.println(" Here are the unique downloaded songs alphabetically: "); for (Song s: mec.uniqueDownloads()) System.out.println(s); // Display the downloaded songs in order of popularity System.out.println(" Here are the downloaded songs by populariry: "); for (Pair p: mec.songsByPopularity()) System.out.println("(" + p.getKey() + " downloads) " + p.getValue()); // Display the royalties System.out.println(" Here are the royalties: "); mec.displayRoyalties(); } }

image text in transcribedimage text in transcribed

image text in transcribed

import javafx.util.Pair;

import java.util.*;

public class MusicExchangeCenter { Map royalties = new HashMap();

ArrayList users; private ArrayList downloadedSongs = new ArrayList();

public ArrayList getDownloadedSongs() { return downloadedSongs; }

public MusicExchangeCenter() { users = new ArrayList(); }

public static ArrayList removeDuplicate(ArrayList list) { ArrayList newlist = list; try { for (int i = 0; i

}

public TreeSet uniqueDownloads() { TreeSet aTreeSet = new TreeSet(new Comparator() { @Override public int compare(Song o1, Song o2) { return o1.getTitle().compareTo(o2.getTitle()); } });

for (Song song : downloadedSongs) { aTreeSet.add(song); } return aTreeSet; }

public ArrayList> songsByPopularity() { ArrayList> anArray = new ArrayList(); int downloadCount; for (Song song : uniqueDownloads()) { downloadCount = 0; for (Song songinlist : downloadedSongs) { if (song.getTitle().equals(songinlist.getTitle())) { downloadCount++; } } anArray.add(new Pair(downloadCount, song)); } anArray.sort(new Comparator>() { public int compare(Pair p1, Pair p2) { if (p1.getKey()

public void setRoyalties(Song s) { if (royalties.get(s.getArtist()) != null) { royalties.put(s.getArtist(), royalties.get(s.getArtist()) + .25); } else { royalties.put(s.getArtist(), 0.25); } }

public void displayRoyalties() { print(String.format("%-8s%-10s%n------------------", "Amount", "Artist")); for (String artist : royalties.keySet()) { print(String.format("%n$%-7.2f%-10s", royalties.get(artist), (artist))); } }

public void print(String s) { System.out.printf(s); }

public ArrayList onlineUsers() { ArrayList onlineUsers = new ArrayList(); for (User user : users) { if (user.isOnline()) { onlineUsers.add(user); } } return onlineUsers; }

public ArrayList allAvailableSongs() { ArrayList availableSongs = new ArrayList(); for (User user : users) { if (user.isOnline()) { for (Song s : user.getSongList()) { availableSongs.add(s); } } } return (ArrayList) removeDuplicate(availableSongs); }

@Override public String toString() { return String.format("Music Exchange Center (%s users on-line, %s songs available)", (onlineUsers().isEmpty()) ? 0 : onlineUsers().size(), (allAvailableSongs().isEmpty()) ? 0 : allAvailableSongs().size()); }

public User userWithName(String s) { for (User user : users) { if (user.getUserName().equals(s)) { return user; } } return null; }

public void registerUser(User x) { if (userWithName(x.getUserName()) == null) { users.add(x); } }

public Song getSong(String title, String ownerName) { for (User u : users) { if (u.getUserName().equals(ownerName)) { if (u.isOnline()) { for (Song song : u.getSongList()) { if (title.equals(song.getTitle())) { return song; } } } } } return null; }

public ArrayList availableSongsByArtist(String artist) { ArrayList songByArtist = new ArrayList(); for (Song s : allAvailableSongs()) { if (s.getArtist().equals(artist)) { songByArtist.add(s); } } return songByArtist;

} }

public class Song { public User owner = null; 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)); } }

import java.util.ArrayList;

public class User { private String userName; private boolean online; private ArrayList songList;

public User() { this(""); }

public User(String u) { userName = u; online = false; songList = new ArrayList(); }

// 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 ArrayList getSongList() { return songList; }

public void register(MusicExchangeCenter m) { m.registerUser(this); }

public void logon(MusicExchangeCenter m) { if (m.userWithName(userName) != null) { online = true; } }

public void logoff(MusicExchangeCenter m) { if (m.userWithName(userName) != null) { online = false; } }

public Song getSongByTitle(String s) { for (Song song : songList) { if (song.getTitle().equals(s)) { return song; } } return null; }

public boolean containsDuplicates() { for (int i = 0; i

if (songList.get(i).getTitle().equals(songList.get(j).getTitle())) {

return true; } } } return false; }

public void downloadSong(MusicExchangeCenter m, String title, String ownerName) { Song songToDownload = m.getSong(title, ownerName); if (songToDownload != null) { Song newSong = new Song(songToDownload.getTitle(), songToDownload.getArtist(), songToDownload.getMinutes(), songToDownload.getSeconds()); newSong.owner = this; songList.add(newSong); m.setRoyalties(newSong); m.getDownloadedSongs().add(newSong); } }

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 String getUserName() { return userName; }

public void addSong(Song s) { songList.add(s); s.owner = this; }

public void print(String s) { System.out.println(s); }

public ArrayList requestCompleteSonglist(MusicExchangeCenter m) { ArrayList stringList = new ArrayList(); stringList.add(String.format("%3s %-25s %-15s %-6s %-10s", "", "TITLE", "ARTIST", "TIME", "OWNER")); for (Song s : m.allAvailableSongs()) { stringList.add(String.format("%2s. %-25s %-15s %-1s:%-4s %-10s", stringList.size(), s.getTitle(), s.getArtist(), s.getMinutes(), s.getSeconds(), s.owner.getUserName())); } for (String s : stringList) { //print(s); } return stringList; }

public ArrayList requestSonglistByArtist(MusicExchangeCenter m, String artist) { ArrayList stringList = new ArrayList(); stringList.add(String.format("%3s %-25s %-15s %-6s %-10s%n", "", "TITLE", "ARTIST", "TIME", "OWNER")); for (Song s : m.availableSongsByArtist(artist)) { stringList.add(String.format("%2s. %-25s %-15s %-1s:%-4s %-10s", stringList.size(), s.getTitle(), s.getArtist(), s.getMinutes(), s.getSeconds(), s.owner.getUserName())); } return stringList;

}

public int totalSongTime() { int total = 0; for (Song song : songList) { total += song.getSeconds(); } return total; }

public boolean isOnline() { return online; }

public String toString() { String s = "" + userName + ": " + songList.size() + " songs ("; if (!online) s += "not "; return s + "online)"; } }

In the getSong) method in the MusicExchangeCenter, adjust the code so that if the song is found (i.e, able to be downloaded), then it is added to the downloadedSongs list. Write the get method for the downloadedSongs attribute. Write a method in the MusicExchangeCenter class called uniqueDownloads() that returns (i.e., not displays) a TreeSet of all downloaded Song objects such that the set is sorted alphabetically by song title. There should be no duplicates songs in this set. Write a method in the MusicExchangeCenter class called songsByPopularity) that returns (i.e., not displays) an ArrayList of Pair objects where the key of the pair is an Integer and the value is the Song object. The integer key should represent the number of times that that song was downloaded. The list returned should be sorted in decreasing order according to the number of times the song was downloaded. To sort a list of Pair objects, you can use the following code: Collections.sort (popular, new Comparator> public int compare (Pair pl, Pair p2) PUT YOUR CODE IN HER 1) i Just insert the missing code so that it returns an appropriate integer indicating whether pair p1 comes before or after pair p2 in the sort order (see notes page 281). Here is a test program. make sure it works with your code: In the getSong) method in the MusicExchangeCenter, adjust the code so that if the song is found (i.e, able to be downloaded), then it is added to the downloadedSongs list. Write the get method for the downloadedSongs attribute. Write a method in the MusicExchangeCenter class called uniqueDownloads() that returns (i.e., not displays) a TreeSet of all downloaded Song objects such that the set is sorted alphabetically by song title. There should be no duplicates songs in this set. Write a method in the MusicExchangeCenter class called songsByPopularity) that returns (i.e., not displays) an ArrayList of Pair objects where the key of the pair is an Integer and the value is the Song object. The integer key should represent the number of times that that song was downloaded. The list returned should be sorted in decreasing order according to the number of times the song was downloaded. To sort a list of Pair objects, you can use the following code: Collections.sort (popular, new Comparator> public int compare (Pair pl, Pair p2) PUT YOUR CODE IN HER 1) i Just insert the missing code so that it returns an appropriate integer indicating whether pair p1 comes before or after pair p2 in the sort order (see notes page 281). Here is a test program. make sure it works with your code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Will the individual understand it?

Answered: 1 week ago

Question

Q.1. Health issues caused by adulteration data ?

Answered: 1 week ago

Question

1. Traditional and modern methods of preserving food Articles ?

Answered: 1 week ago

Question

What is sociology and its nature ?

Answered: 1 week ago

Question

What is liquidation ?

Answered: 1 week ago