Skip to content

Instantly share code, notes, and snippets.

public int numWordsOfLength( int len ) {
int cnt = 0;
for (Object w: myList) {
String s = (String)w;
if (s.length() == len) {
cnt++;
}
}
return cnt;
@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 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 / 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
}
@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 / WordPairList.java
Created November 27, 2018 16:01
WordPair solution for the 2018 AP Computer Science Exam - https://compsci.rocks/wordpair-solution/
public WordPairList(String[] words) {
allPairs = new ArrayList<>();
for (int i=0; i<words.length; i++)
for (x=i+1; x<words.length; x++)
allPairs.add(new WordPair(words[i], words[x]);
}
@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 / 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;
}