ap computer science a 2021 free response answers code example
Example 1: 2019 ap computer science free response answers
// Part A
public ArrayList<String> getDelimitersList(String[] tokens) {
ArrayList<String> out = new ArrayList<>();
for (String t: tokens) {
if (t.equals(openDel) || t.equals(closeDel))
out.add(t);
}
return out;
}
// Part B
public boolean isBalanced(ArrayList<String> delimiters) {
int open = 0;
int close = 0;
for (String d: delimiters) {
if (d.equals(openDel))
open++;
else if (d.equals(closeDel))
close++;
if (close > open)
return false;
}
return open == close;
}
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;
}
}