Name (print) _________ Ron Cytron ______________   Lab Section you attend___ All ______

CSE131 Quiz 5 Solution

Suppose you are given three classes: Song, Artist, and MP3Player. Songs have a length and are sometimes CountryWestern. Every Artist has a top hit Song and a count of the number of Songs recorded by the Artist. A Song can be added to an MP3Player, and a (random) Song can be removed from an MP3Player only if the MP3Player is not empty. An MP3Player can tell you the last Song it played.

Song Artist MP3Player
  • int getLength()
  • boolean isCountryWestern()
  • Song getTopHit()
  • int getNumSongs()
  • boolean isEmpty()
  • Song removeOneSong()
  • void addSong(Song s)
  • Song lastPlayed()

  1. (50 pts) Based on the above methods, provide the type (and make up a suitable name) for one instance variable of any one of the above classes.
    (Yes, I only need one such thing here, not one for each class. Tell me which class would have the instance variable.)
    [  -15 if name not suitable.  -25 for unmotivated instance var ]
      Song:      int length      boolean countryWestern  String genre ...
      Artist:    Song topHit     int numSongs                         ...
      MP3Player: boolean empty   int numSongs  Song lastPlayed        ...
  2. Using the above methods, write a Java code fragment (below or on the back of this page) to perform each of the following tasks (Choose any two, 25 points each):
    • Example

      Given an Artist a, how long is the Artist's top hit?

              int length = a.getTopHit().getLength();
    Choose any two of the following:
    1. Given two Artists, a1 and a2, determine how long it would take to play both of their top hit Songs.
        int time = a1.getTopHit().getLength() + a2.getTopHit().getLength();
    2. Given an Artist a, determine if a is a one-hit wonder (ask me if this is unclear).
        boolean oneHitWonder = a.getNumSongs() == 0;
    3. Determine if an MP3Player m has never played any Songs.
        boolean neverPlayed  = m.lastPlayed() == null;
    4. Given two MP3Players mine and yours, remove every Song from yours and add it to mine.
       while (!yours.isEmpty()) {
    5. Given an MP3Player m, keep removing Songs from m until m is empty or a Country Western Song has been removed.
       boolean cwRemoved = false;
       while (! (m.isEmpty() || cwRemoved)) {
          cwRemoved =  m.removeOneSong().isCountryWestern();