Download Info Solution

For the Download Info free response on the 2013 AP Computer Science exam you were expected to implement a solution working with an ArrayList of an arbitrary class called DownloadInfo.

Working with lists of classes is a common topic for the free response questions and it’s likely that you will see a similar FRQ on your AP exam.

DownloadInfo

To start, you’re given the shell of a class called DownloadInfo that stores the title and number of times that a particular title has been downloaded.

For the full class with comments, head over to the College Board website. But here are the basics.

public class DownloadInfo {
	public DownloadInfo(String title) {
		// implementation not shown
	}
	public String getTitle() {
		// implementation not shown
	}
	public void incrementTimesDownloaded() {
		// implementation not shown 
	}
}

Notice that there is a constructor and two methods, all shown as implementation not shown. We’ll need those later.

MusicDownloads

You’re also given the following MusicDownloads class which contains the two methods you’re going to write. Like above you’re going to want to view the entire PDF on the College Board website. But here’s a stripped down version so we have something to talk about.

public class MusicDownloads {
	private List<DownloadInfo> downloadList;
	public MusicDownloads() {
		downloadList = new ArrayList<DownloadInfo>();
	}
	public DownloadInfo getDownloadInfo(String title) {
		// part A
	}
	public void updateDownloads(List<String> titles) {
		// part B
	}
}

We’ve got a downloadList instance variable that’s a List of DownloadInfo references. The constructor instantiates downloadList to contain an ArrayList. We implement the other two methods in parts A & B.

getDownloadInfo

In part A your code should go through downloadList and find the DownloadInfo reference that has the same title as the title parameter. For this we need to know that a DownloadInfo has a getTitle method that returns the title as a String.

My solution looks like this.

public DownloadInfo getDownloadInfo( String title ) {
    for(DownloadInfo dl: downloadList) {
        if (dl.getTitle().equals(title)) {
            return dl; 
        }
    }
    return null; 
}

Using a for each loop the code goes through all DownloadInfo references in downloadList and pulls out the title using getTitle. That title is compared to title and if a match if found it returns that reference.

If, after the loop no matching title is found the the method returns null, which is what the problem description states should happen.

updateDownloads

On part B we’re asked to take a List of titles and increment the count of times the matching DownloadInfo has been downloaded. Remember that the DownloadInfo class has a method called incrementTimesDownloaded that will do this for us. We just have to call it correctly.

public void updateDownloads( List<String> titles ) {
    for (String title: titles) {
        DownloadInfo find = getDownloadInfo(title);
        if (find == null) {
            downloadList.add(new DownloadInfo(title));
        }
        else {
            find.incrementTimesDownloaded(); 
        }
    }
}

Again we’re looping, but this time it’s through the list of strings titles.

Inside the loop the code tries to find a matching DownloadInfo using the method we wrote for part A. If that method returns null it means that there isn’t a match and we need to add a new DownloadInfo to downloadList. If a match if found, we call incrementTimesDownloaded on it.

Leave a Reply

Your email address will not be published. Required fields are marked *