Skip to content

Instantly share code, notes, and snippets.

@gschaffner
Created April 14, 2016 05:51
Show Gist options
  • Save gschaffner/749daba11d6a2c1a94ebd1a8bb9afec4 to your computer and use it in GitHub Desktop.
Save gschaffner/749daba11d6a2c1a94ebd1a8bb9afec4 to your computer and use it in GitHub Desktop.
APCS Written Test Practice 2013
// 1a
public DownloadInfo getDownloadInfo(String title) {
for(DownloadInfo elem : downloadList) {
if(elem.getTitle().equals(title)) {
return elem;
}
}
return null;
}
// 1b
public void updateDownloads(List<String> titles) {
for(String title : titles) {
if(getDownloadInfo(title) != null) {
getDownloadInfo(title).incrementTimesDownloaded();
} else {
downloadList.add(new DownloadInfo(title));
}
}
}
// 2a
public TokenPass(int playerCount) {
board = new int[playerCount];
for(int i = 0; i < board.length; i++) {
board[i] = (int)((Math.random() * 10) + 1));
}
currentPlayer = (int)(Math.random() * playerCount);
}
// 2b
public void distributeCurrentPlayerTokens() {
int tokens = board[currentPlayer];
board[currentPlayer] = 0;
for(int i = 1; i <= tokens; i++) {
board[(currentPlayer + i) % board.length] += 1;
}
}
// 3a
// skipped because we didn't do GridWorld
// 3b
// skipped because we didn't do GridWorld
// 4a
public SkyView(int numRows, int numCols, double[] scanned) {
view = new double[numRows][numCols];
int curIndex = 0; // current index in scanned
for(int curRow = 0; curRow < numRows; curRow++) {
if(curRow % 2 == 0) { // even row, going up
for(int curCol = 0; curCol < numCols; curCol++) {
view[curRow][curCol] = scanned[curIndex];
curIndex++;
}
} else { // odd row, going down
for(int curCol = numCols - 1; curCol >= 0; curCol--) {
view[curRow][curCol] = scanned[curIndex];
curIndex++;
}
}
}
}
// 4b
public double getAverage(int startRow, int endRow, int startCol, int endCol) {
double sum = 0;
int numValues = 0; // the number of values added to sum, used to calculate average
for(int curRow = startRow; curRow <= endRow; curRow++) {
for(int curCol = startCol; curCol <= endCol; curCol++) {
sum += view[curRow][curCol];
numValues++;
}
}
return (sum / numValues);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment