Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
CompSciRocks / PartA.java
Created November 27, 2018 13:42
Working solution for the 2018 AP Computer Science ArrayTester Free Response question https://compsci.rocks
public static int[] getColumn(int[][] arr2D, int c) {
int[] out = new int[arr2D.length];
for (int r=0; r<arr2D.length; r++)
out[r] = arr2D[r][c];
return out;
}
@CompSciRocks
CompSciRocks / runSimulation.java
Created November 27, 2018 15:25
Solution for the Frog Simulation free response on the 2018 AP computer science exam - https://compsci.rocks
public double runSimulation(int num) {
int good = 0;
for (int i=0; i<num; i++)
if (simulate())
good++;
return (double)good / num;
}
@CompSciRocks
CompSciRocks / CodeWordChecker.java
Created November 27, 2018 16:14
Solution for Code Word Checker free response on the 2018 AP Computer Science Exam. https://compsci.rocks/codeword-checker-solution/
public class CodeWordChecker implements StringChecker {
private int min;
private int max;
private String no;
public CodeWordChecker(int mi, int ma, String n) {
min = mi;
max = ma;
no = n;
}
@CompSciRocks
CompSciRocks / GrayImage.java
Last active November 27, 2018 18:00
Solution for GrayImage free response question on 2012 AP Computer Science exam - https://compsci.rocks
public class GrayImage {
public static final int BLACK = 0;
public static final int WHITE = 255;
private int[][] pixelValues;
public int countWhitePixels() {
// Part A
}
public void processImage() {
// Part B
}
public class Robot {
private int[] hall;
private int pos;
private boolean facingRight;
public boolean forwardMoveBlocked() { // part A }
private void move() { // part B }
public int clearHall() { // part C }
private boolean hallIsClear() {
// implementation not shown
@CompSciRocks
CompSciRocks / Cat.java
Last active November 27, 2018 20:21
Solution for Pet free response question on 2004 AP Computer Science Exam - https://compsci.rocks/pet-solution/
public class Cat extends Pet {
public Cat(String name) {
super(name);
}
public String speak() {
return "meow";
}
}
public int numWordsOfLength( int len ) {
int cnt = 0;
for (Object w: myList) {
String s = (String)w;
if (s.length() == len) {
cnt++;
}
}
return cnt;
public Digits( int num ) {
digitList = new ArrayList<>();
int n = num;
do {
digitList.add(0, n % 10);
n /= 10;
} while (n > 0);
}
public class MultPractice implements StudyPractice {
private int numOne;
private int numTwo;
public MultPractice(int a, int b) {
numOne = a;
numTwo = b;
}
String[] wordArray = {"wheels", "on", "the", "bus"};
RandomStringChooser sChooser = new RandomStringChooser(wordArray);
for (int k = 0; k < 6; k++) {
System.out.print(sChooser.getNext() + " ");
}
// Possible output
// bus the wheels on NONE NONE