ap computer science 2020 free-response answers code example
Example 1: 2019 ap computer science free response answers
// Part A
public static int numberOfLeapYears(int year1, int year2) {
int cnt = 0;
for (int y=year1; y<=year2; y++) {
if (isLeapYear(y))
cnt++;
}
return cnt;
}
// Part B
public static int dayOfWeek(int month, int day, int year) {
int firstDay = firstDayOfYear(year);
int doy = dayOfYear(month, day, year);
return (firstDay + dayOfYear - 1) % 7;
}
Example 2: 2019 ap computer science free response answers
public class StepTracker {
private int days;
private int activeDays;
private int totalSteps;
private int minActive;
public StepTracker(int m) {
minActive = m;
days = 0;
activeDays = 0;
totalSteps = 0;
}
public void addDailySteps(int steps) {
days++;
totalSteps += steps;
if (steps >= minActive) {
activeDays++;
}
}
public int activeDays() {
return activeDays;
}
public double averageSteps() {
if (days == 0) {
return 0.0;
}
return (double)totalSteps / days;
}
}