2016 ap computer science free response code example

Example 1: 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; 
	}
}

Example 2: 2019 ap computer science free response answers

// Part A
public LightBoard(int numRows, int numCols) {
	lights = new boolean[numRows][numCols];
	for (int r=0; r<numRows; r++)
		for (int c=0; c<numCols; c++)
			lights[r][c] = Math.random() < 0.4; 
}

// Part B
public boolean evaluateLight(int row, int col) {
	int onInCol = 0;
	for (int r=0; r<lights.length; r++)
		if (lights[r][col])
			onInCol++;
	if (lights[row][col] && onInCol % 2 == 0)
		return false;
	else if (!lights[row][col] && onInCol % 3 == 0)
		return true;
	return lights[row][col];
}

Tags:

Misc Example