2013 Download Info
<< 2003 TreasureMap | APQuestionsTrailIndex | 2013 TokenPass >>
Click here to see the questions from 2013
MusicDownloadsTester.java
import java.util.ArrayList;
import java.util.List;
public class MusicDownloadsTester {
public static void main(String[] args) {
MusicDownloads webMusicA = new MusicDownloads();
webMusicA.add(new DownloadInfo("Hey Jude",5));
webMusicA.add(new DownloadInfo("Soul Sister",3));
webMusicA.add(new DownloadInfo("Aqualung",10));
System.out.println("Part A tests:");
System.out.println("webMusicA has:\n"+webMusicA);
System.out.println( getDownloadInfoTest(webMusicA, "Aqualung") +" [expected found at 2]");
System.out.println( getDownloadInfoTest(webMusicA, "Happy Birthday") +" [expected null]");
String found = getDownloadInfoTest(webMusicA, "Aqualung");
found+=getDownloadInfoTest(webMusicA, "Happy Birthday");
String expected="'Aqualung' found at 2'Happy Birthday' returned a null";
if (found.equals(expected))
System.out.print("No ");
System.out.println("problems found in part (a)\n");
System.out.println("Part B tests:");
MusicDownloads webMusicB = new MusicDownloads();
webMusicB.add(new DownloadInfo("Hey Jude",5));
webMusicB.add(new DownloadInfo("Soul Sister",3));
webMusicB.add(new DownloadInfo("Aqualung",10));
System.out.println("Before the call, webMusicB has:\n"+webMusicB);
List<String> songTitles = new ArrayList<String>();
songTitles.add("Lights");
songTitles.add("Aqualung");
songTitles.add("Soul Sister");
songTitles.add("Go Now");
songTitles.add("Lights");
songTitles.add("Soul Sister");
webMusicB.updateDownloads(songTitles);
System.out.println("After the call, webMusicB has:\n"+webMusicB);
found = webMusicB.toString();
expected = "0) 'Hey Jude' 5 downloads\n1) 'Soul Sister' 5 downloads\n2) 'Aqualung' 11 downloads\n3) 'Lights' 2 downloads\n4) 'Go Now' 1 downloads\n";
if (found.equals(expected))
System.out.print("No ");
System.out.println("problems found in part (b)");
}
/**
* this tester method has spoilers,
* so don't look too carefully if you're still
* working on the part (a) solution
*/
public static String getDownloadInfoTest(MusicDownloads md, String title){
DownloadInfo di = md.getDownloadInfo(title);
if (di == null)
return "'"+title+"'"+" returned a null";
List<DownloadInfo> list = md.getList();
int i=0;
boolean found=false;
while (i<list.size() && !found){
String s = list.get(i).getTitle();
if (s.equals(title))
found=true;
else
i++;
}
if (!found)
return "'"+title+"'"+" not found but NOT null";
return "'"+title+"'"+" found at "+i;
}
}
DownloadInfo.java
public class DownloadInfo
{
private String title;
private int count;
/** Creates a new instance with the given unique title and sets the
* number of times downloaded to 1.
* @param title the unique title of the downloaded song
*/
public DownloadInfo(String title)
{
//this implementation was not shown
this.title=title;
count=1;
}
//this implementation was not shown
public DownloadInfo(String title, int count) {
this(title);
this.count=count;
}
/** @return the title */
public String getTitle()
{
return title;
}
/** Increment the number times downloaded by 1
*
*/ public void incrementTimesDownloaded()
{
count++;
}
// There may be instance variables, constructors, and methods that are not shown.
public String toString(){
return "'"+getTitle()+"' "+count+" downloads";
}
}
MusicDownloads.java
import java.util.ArrayList;
import java.util.List;
public class MusicDownloads
{
/** The list of downloaded information.
* Guaranteed not to be null and not to contain duplicate titles.
*/
private List<DownloadInfo> downloadList;
/** Creates the list of downloaded information. */
public MusicDownloads()
{
downloadList = new ArrayList<DownloadInfo>();
}
/** Returns a reference to the DownloadInfo object with the requested title if it exists.
* @param title the requested title
* @return a reference to the DownloadInfo object with the
* title that matches the parameter title if it exists in the list;
* null otherwise.
*
* Postcondition
* - no changes were made to downloadList
*/
public DownloadInfo getDownloadInfo(String title)
{
// part (a)
return null; // replace this line with your code
}
/** Updates downloadList with information from titles.
* @param titles a list of song titles
* Postcondition:
* - there are no duplicate titles in downloadList.
* - no entries were removed from downloadList.
* - all songs in titles are represented in downloadList.
* - for each existing entry in downloadList, the download count is increased by
* the number of times its title appeared in titles.
* - the order of the existing entries in downloadList is not changed.
* - the first time an object with a title from titles is added to downloadList, it
* is added to the end of the list.
* - new entries in downloadList appear in the same order
* in which they first appear in titles.
* - for each new entry in downloadList, the download count is equal to
* the number of times its title appeared in titles
*/
public void updateDownloads(List<String> titles)
{
// part (b)
}
// other methods to get the examples to work
public void add(DownloadInfo d){
downloadList.add(d);
}
public List<DownloadInfo> getList(){
return downloadList;
}
public String toString(){
String result ="";
for (int i=0; i< downloadList.size(); i++)
result+= i+") "+downloadList.get(i).toString()+"\n";
return result;
}
}
