Respuesta :
Answer:
Check the explanation
Explanation:
Below is the code for the add method to be added in the BillBoard Class.
public void add(Musician musician)
{
if(musician.getIsPlatinum())
{
//Checks if the top10 list has less than 10 musicians
if(top10.size()<10)
{
top10.add(musician);
}
else
{
// Declare variable that stores Weeks in top 40 of first musician
int min = (top10.get(0)).getWeeksInTop40();
// Declare variable that stores the index 0 initially and will finally store index of musician with minimum weeks in top40.
int i_min = 0;
/*This loops finds the index of musician with least number of weeks in top40 and also number of weeks he/she had in top40 */
for(int i=1; i<10; i++)
{
if(min>(top10.get(i)).getWeeksInTop40())
{
min = (top10.get(i)).getWeeksInTop40();
i_min = i;
}
}
// Replaces the old musician with new musician by checking if the new one have more weeks in top40 list.
if(min<musician.getWeeksInTop40())
{
top10.remove(i_min);
top10.add(musician);
}
else
{
System.out.println("Musician could not be added to the top10 list since they don't have enough top40 weeks.");
}
}
}
else
{
System.out.println("Musician could not be added to the top10 list.");
}
}
Below is the complete modified code,
import java.util.ArrayList;
public class BillboardTester
{
public static void main(String[] args)
{
Billboard top10 = new Billboard();
top10.add(new Musician("Beyonce", 316, 100000000));
top10.add(new Musician("The Beatles", 365, 600000000));
top10.add(new Musician("Drake", 425, 150000000));
top10.add(new Musician("Pink Floyd", 34, 250000000));
top10.add(new Musician("Mariah Carey", 287, 200000000));
top10.add(new Musician("Rihanna", 688, 250000000));
top10.add(new Musician("Queen", 327, 170000000));
top10.add(new Musician("Ed Sheeran", 536, 150000000));
top10.add(new Musician("Katy Perry", 317, 143000000));
top10.add(new Musician("Justin Bieber", 398, 140000000));
//This musician should not be added to the top10 because they don't have enough records sold
top10.add(new Musician("Karel the Dog", 332, 60));
//This musician should replace the artist with the least Weeks on the top 40 charts.
top10.add(new Musician("Tracy the Turtle", 332, 150000000));
//This musician should not replace an artist, but is a Platinum artist
top10.add(new Musician("Alex Eacker", 100, 23400000));
top10.printTop10();
}
}
public class Billboard
{
private ArrayList<Musician> top10 = new ArrayList<Musician>();
//Don't make alterations to this method!
public void printTop10()
{
System.out.println(top10);
}
public void add(Musician musician)
{
if(musician.getIsPlatinum())
{
//Checks if the top10 list has less than 10 musicians
if(top10.size()<10)
{
top10.add(musician);
}
else
{
// Declare variable that stores Weeks in top 40 of first musician
int min = (top10.get(0)).getWeeksInTop40();
// Declare variable that stores the index 0 initially and will finally store index of musician with minimum weeks in top40.
int i_min = 0;
// This loops finds the index of musician with least number of weeks in top40 and also number of weeks he/she had in top40
for(int i=1; i<10; i++)
{
if(min>(top10.get(i)).getWeeksInTop40())
{
min = (top10.get(i)).getWeeksInTop40();
i_min = i;
}
}
// Replaces the old musician with new musician by checking if the new one have more weeks in top40 list.
if(min<musician.getWeeksInTop40())
{
top10.remove(i_min);
top10.add(musician);
}
else
{
System.out.println("Musician could not be added to the top10 list since they don't have enough top40 weeks.");
}
}
}
else
{
System.out.println("Musician could not be added to the top10 list.");
}
}
}
public class Musician
{
private String name;
private int weeksInTop40;
private int albumsSold;
private boolean isPlatinum;;
public Musician(String name, int weeksInTop40, int albumsSold)
{
this.name = name;
this.weeksInTop40 = weeksInTop40;
this.albumsSold = albumsSold;
setPlatinum(albumsSold);
}
public void setPlatinum(int albumsSold)
{
if(albumsSold >= 1000000)
{
isPlatinum = true;
}
else
{
isPlatinum = false;
}
}
public int getWeeksInTop40()
{
return this.weeksInTop40;
}
public String getName()
{
return this.name;
}
public boolean getIsPlatinum()
{
return isPlatinum;
}
public String toString()
{
return this.name;
}
}
Below I have attached image of output of code,
