Name (print) ___________________________________   Lab Section you attend______________

CSE131 Quiz 5
Modular Design
3 Oct 2008


Solution

The optional extension can be found here.

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.)
    
    
    
  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): 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.
    2. Given an Artist a, determine if a is a one-hit wonder (ask me if this is unclear).
    3. Determine if an MP3Player m has never played any Songs.
    4. Given two MP3Players mine and yours, remove every Song from yours and add it to mine.
    5. Given an MP3Player m, keep removing Songs from m until m is empty or a Country Western Song has been removed.