This question involves a StreamingService class that tracks a user's viewing habits. The number of episodes a user watches each day is stored in an ArrayList of integers. You will write two methods for the StreamingService class.
import java.util.ArrayList;
public class StreamingService {
/** The number of episodes watched each day; initialized in the constructor. */
private ArrayList<Integer> dailyWatchCount;
/**
* Calculates the average number of episodes watched per day over a 7-day period
* starting from `startDay`.
* @param startDay the starting day index (0-based) for the 7-day period
* @return the average number of episodes watched
* Precondition: 0 <= startDay <= dailyWatchCount.size() - 7
*/
public double calculateWeeklyAverage(int startDay) {
/* to be implemented in part (a) */
}
/**
* Finds the length of the longest consecutive period of 'binge-watching'.
* A 'binge day' is defined as a day where 5 or more episodes were watched.
* @return the length of the longest binge streak (e.g., 3 for 3 consecutive binge days).
* Returns 0 if there are no binge days.
*/
public int findLongestBinge() {
/* to be implemented in part (b) */
}
// There may be instance variables, constructors, or methods that are not shown.
}
(a) Write the calculateWeeklyAverage method.
This method calculates the average number of episodes watched per day over a 7-day period. The period begins at the index startDay and includes the next 6 days for a total of 7 days. The method should return the average as a double.
For example, if dailyWatchCount is [2, 3, 8, 1, 0, 6, 7, 4, 5, 5, 5, 1]:
- A call to
calculateWeeklyAverage(0) would sum the first 7 elements (2+3+8+1+0+6+7 = 27) and return 27.0 / 7, which is approximately 3.857.
- A call to
calculateWeeklyAverage(2) would sum the elements from index 2 to 8 (8+1+0+6+7+4+5 = 31) and return 31.0 / 7, which is approximately 4.428.
(b) Write the findLongestBinge method.
This method examines the entire dailyWatchCount list to find the longest streak of consecutive 'binge days'. A day is considered a 'binge day' if 5 or more episodes were watched. The method should return the number of days in the longest consecutive streak. If there are no binge days, it should return 0.
For example, if dailyWatchCount is [2, 3, 8, 1, 0, 6, 7, 4, 5, 5, 5, 1]:
- The days with 5 or more episodes are at indices 2 (8), 5 (6), 6 (7), 8 (5), 9 (5), and 10 (5).
- There is a binge streak of length 1 at index 2.
- There is a binge streak of length 2 at indices 5 and 6.
- There is a binge streak of length 3 at indices 8, 9, and 10.
- The longest streak is 3. The method should return
3.
If dailyWatchCount is [1, 2, 3, 4], there are no binge days, so the method should return 0.