College Board FRQs
2014 FRQ #1 Part A (ArrayList FRQ)
Write the method scrambleword, which takes a given word and returns a string that contains a scrambled version of the word according to the following rules.
- The scrambling process begins at the first letter of the word and continues from left to right.
- If two consecutive letters consist of an "A" followed by a letter that is not an "A", then the two letters are swapped in the resulting string.
- Once the letters in two adjacent positions have been swapped, neither of those two positions can be involved in a future swap.
public static String scrambleWord(String word) {
String newWord = "";
for (int i = 0; i<word.length()-1; i++) {
if (word.charAt(i) == 'A' && word.charAt(i+1) != 'A') {
newWord += word.charAt(i+1);
newWord += word.charAt(i);
i++;
} else {
newWord += word.charAt(i);
}
}
return newWord;
}
// Test
scrambleWord("ABRACADABRA");
2014 FRQ#1 Part B (ArrayList FRQ)
Write the method scrarnbleOrRemove, which replaces each word in the parameter wordList with its scrambled version and removes any words that are unchanged after scrambling. The relative ordering of the entries in wordList remains the same as before the call to scrambleOrRemove.
public static void scrambleOrRemove(List<String> wordList) {
for (int i = 0; i<wordList.size(); i++) {
String scrambled = scrambleWord(wordList.get(i));
if (scrambled.equals(wordList.get(i))) {
wordList.remove(i);
i--;
}
else {
wordList.set(i, scrambled);
}
}
}
ArrayList<String> l = new ArrayList<String>();
l.add("TAN");
l.add("ABRACADABRA");
l.add("WHOA");
l.add("APPLE");
l.add("EGGS");
scrambleOrRemove(l);
System.out.println(l);
2017 FRQ#2 (Class FRQ)
The MultPractice class is a StudyPractice that produces multiplication practice problems. A MultPractice object is constructed with two integer values: first integer and initial second integer. The first integer is a value that remains constant and is used as the first integer in every practice problem. The initial second integer is used as the starting value for the second integer in the practice problems. This second value is incremented for each additional practice problem that is produced by the class.
// Base interface (made by college board)
public interface StudyPractice {
String getProblem();
void nextProblem();
}
public class MultPractice implements StudyPractice {
private int firstNum;
private int secondNum;
public MultPractice(int first, int second) {
firstNum = first;
secondNum = second;
}
public String getProblem() {
return firstNum + " TIMES " + secondNum;
}
public void nextProblem() {
secondNum++;
}
}
// Tester Code
StudyPractice sp = new MultPractice(7, 3);
System.out.println(sp.getProblem());
sp.nextProblem();
sp.nextProblem();
System.out.println(sp.getProblem());
public class Phrase {
private String currentPhrase;
public Phrase(String a) {
this.currentPhrase = a;
}
public String getPhrase() {
return currentPhrase;
}
public int findNthOccurrence(String str, int n) {
int ret = -1;
String curStr = currentPhrase;
int offset = 0;
for (int i = 0; i<n; i++) {
ret = curStr.indexOf(str) + offset;
if (ret - offset == -1) return -1;
curStr = curStr.substring(ret - offset + 1, curStr.length());
offset += (ret - offset) + 1;
}
return ret;
}
public void replaceNthOccurence(String str, int n, String repl) {
// FRQ Answer
int occur = findNthOccurrence(str, n);
if (occur == -1) return;
currentPhrase = currentPhrase.substring(0, occur) + repl + currentPhrase.substring(occur + str.length(), currentPhrase.length());
}
}
Phrase p = new Phrase("aaaa");
p.replaceNthOccurence("aa", 2, "bbb");
System.out.println(p.getPhrase());
2017 FRQ#3b (Methods/Control Structures)
Write the Phrase method f indLastOccurrence. This method finds and returns the index of the last occurrence of a given string in current Phrase. If the given string is not found, -1 is returned. The following tables show several examples of the behavior of the method findLastOccurrence.
public class Phrase {
private String currentPhrase;
public Phrase(String a) {
this.currentPhrase = a;
}
public String getPhrase() {
return currentPhrase;
}
public int findNthOccurrence(String str, int n) {
int ret = -1;
String curStr = currentPhrase;
int offset = 0;
for (int i = 0; i<n; i++) {
ret = curStr.indexOf(str) + offset;
if (ret - offset == -1) return -1;
curStr = curStr.substring(ret - offset + 1, curStr.length());
offset += (ret - offset) + 1;
}
return ret;
}
public void replaceNthOccurence(String str, int n, String repl) {
int occur = findNthOccurrence(str, n);
if (occur == -1) return;
currentPhrase = currentPhrase.substring(0, occur) + repl + currentPhrase.substring(occur + str.length(), currentPhrase.length());
}
public int findLastOccurrence(String str) {
// FRQ Answer
int og_occur = findNthOccurrence(str, 1);
int prev_occur = og_occur;
int cnt = 2;
while (og_occur != -1) {
prev_occur = og_occur;
og_occur = findNthOccurrence(str, cnt);
cnt++;
}
return prev_occur;
}
}
// Tester code
Phrase p = new Phrase("A cat ate late.");
p.replaceNthOccurence("aa", 2, "bbb");
System.out.println(p.findLastOccurrence("ate"));
System.out.println(p.findLastOccurrence("bat"));
public static boolean isNonZeroRow(int[][] array2D, int r) {
for (int i = 0; i<array2D[r].length; i++) {
if (array2D[r][i] == 0) return false;
}
return true;
}
// Tester code
int[][] arr = {{2,1,0},{1,3,2},{0,0,0},{4,5,6}};
// expected: false, true, false, true
for (int i = 0; i<arr.length; i++) {
System.out.println(isNonZeroRow(arr, i));
}
// method provided by college board (not part of FRQ)
public static int numNonZeroRows(int[][] array2D) {
int count = 0;
for (int i = 0; i<array2D.length; i++) count += isNonZeroRow(array2D, i) ? 1 : 0;
return count;
}
public static int[][] resize(int[][] array2D) {
int row = 0;
int[][] ret = new int[numNonZeroRows(array2D)][array2D[0].length];
for (int i = 0; i<array2D.length; i++) {
if (isNonZeroRow(array2D, i)) {
ret[row] = array2D[i];
row++;
}
}
return ret;
}
// Tester code
int[][] arr = {{2,1,0},{1,3,2},{0,0,0},{4,5,6}};
int[][] arrNew = resize(arr);
for (int i = 0; i<arrNew.length; i++) {
for (int j = 0; j<arrNew[i].length; j++) {
System.out.print(arrNew[i][j] + " ");
}
System.out.println();
}