AP® Computer Science A Data Collections Practice Questions
Work through AP Computer Science A practice questions on Data Collections, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Question 1162
Question 1163
A cinema management system models each scheduled showing as a Movie object and keeps every showing currently on the schedule inside a Theater object. The two classes are shown below; only the method averageRatingAtLeast in Theater is left for you to complete. Everything else is already written.
public class Movie
{
private String title;
private int lengthMinutes;
private double rating;
public Movie(String theTitle, int theLength, double theRating)
{
title = theTitle;
lengthMinutes = theLength;
rating = theRating;
}
public String getTitle() { return title; }
public int getLengthMinutes() { return lengthMinutes; }
public double getRating() { return rating; }
}
public class Theater
{
/** All movies currently scheduled at this theater. Never null. */
private ArrayList<Movie> showings;
public Theater(ArrayList<Movie> startingShowings)
{
showings = startingShowings;
}
/** Returns the average rating of every movie in showings whose length is at least
* minLength minutes. If no movie meets the length requirement, returns 0.0.
* @param minLength the inclusive lower bound on length, in minutes; assumed non-negative
* @return the mean rating of qualifying movies, or 0.0 when no movie qualifies
*/
public double averageRatingAtLeast(int minLength) { /* to implement */ }
}
Write the method averageRatingAtLeast. The method must iterate over every Movie in showings, accumulate the rating of each movie whose length in minutes is at least minLength, and return the mean of those ratings as a double. If no movie meets the length requirement, the method returns 0.0.
Suppose showings holds four movies:
{title:"Alpha", lengthMinutes:90, rating:4.0}, {title:"Bravo", lengthMinutes:135, rating:4.5}, {title:"Charlie", lengthMinutes:75, rating:3.0}, {title:"Delta", lengthMinutes:120, rating:3.5}.
A call to averageRatingAtLeast(120) selects the movies whose length is at least 120 minutes — Bravo and Delta — and returns the mean of their ratings: (4.5 + 3.5) / 2 = 4.0.
A call to averageRatingAtLeast(200) finds no qualifying movie and therefore returns 0.0.
public double averageRatingAtLeast(int minLength)Main Solution
Sample Solution
public double averageRatingAtLeast(int minLength)
{
double sum = 0.0;
int count = 0;
for (int i = 0; i < showings.size(); i++)
{
Movie m = showings.get(i);
if (m.getLengthMinutes() >= minLength)
{
sum = sum + m.getRating();
count = count + 1;
}
}
if (count == 0)
{
return 0.0;
}
return sum / count;
}
Scoring notes. Points are earned for: iterating over every index in showings (an enhanced-for loop also works); fetching each Movie through get(i) and using its accessor methods rather than touching the private fields; comparing getLengthMinutes() to minLength with >= (not >); accumulating both a running rating sum and a running qualifying count; guarding the final division with a count == 0 check that returns 0.0; and returning sum / count as a double in the normal case (legal because sum is already a double). Common errors include using >, dividing by showings.size() instead of count, and accessing showings.get(i).rating directly.
Answer images
Question 1164
Question 1165
Question 1166
Question 1167
Question 1168
Question 1169
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

