FRQ Practice
2017 FRQ
1. Digits
This question involves the implementation of a class named Digits
. The Digits
class represents a positive integer as a sequence of digits. The digits are stored in a list of integers, where each integer in the list is between 0 and 9, inclusive. The digits appear in the list in the same order in which they appear in the original integer. For example, the integer 1234 is represented as the list [1, 2, 3, 4]
.
a. constructor
Write the constructor for the Digits
class. The constructor initializes and fills digitList
with the digits from the non-negative integer num
. The elements in digitList
must be Integer
objects representing single digits, and appear in the same order as the digits in num
. Each of the following examples shows the declaration of a Digits
object and the contents o digitList
as initialized by the constructor.
public class Digits {
private ArrayList<Integer> digitList;
public Digits(int num) {
digitList = new ArrayList<Integer>();
if (num == 0) digitList.add(0);
while (num != 0) {
digitList.add(0, num % 10);
num /= 10;
}
}
public ArrayList<Integer> getDigitList() {
return this.digitList;
}
public static void main(String[] args) {
Digits cool = new Digits(12345);
System.out.println(cool.getDigitList());
}
}
Digits.main(null);
public class Digits {
private ArrayList<Integer> digitList;
public Digits(int num) {
digitList = new ArrayList<Integer>();
if (num == 0) digitList.add(0);
while (num != 0) {
digitList.add(0, num % 10);
num /= 10;
}
}
public ArrayList<Integer> getDigitList() {
return this.digitList;
}
public boolean isStrictlyIncreasing() {
if (digitList.size() == 1) {
return true;
}
for (int i = 1; i < digitList.size(); i++) {
if (digitList.get(i) <= digitList.get(i-1)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Digits cool = new Digits(1457);
System.out.println(cool.getDigitList());
System.out.println(cool.isStrictlyIncreasing());
}
}
Digits.main(null);
public interface StudyPractice {
String getProblem();
void nextProblem();
}
The MultPractice class is a StudyPractice with multiplication practice. It has 2 integer values: first integer and initial second integer. first integer remains constant, and second is starting value for the second integer. This second value is incremented for each additional practice problem produced
in MultPractice, the getProblem method returns string in format "first integer TIMES second integer". the nextProblem updates state of object to represent the next practice problem
public class MultPractice implements StudyPractice {
private int firstInt;
private int secondInt;
public MultPractice(int firstInt, int secondInt) {
this.firstInt = firstInt;
this.secondInt = secondInt;
}
@Override
public String getProblem() {
return firstInt + " TIMES " + secondInt;
}
@Override
public void nextProblem() {
secondInt++;
}
public static void main(String[] args) {
StudyPractice p1 = new MultPractice (7, 3);
System.out.println(p1.getProblem());
p1.nextProblem();
System.out.println(p1.getProblem());
}
}
MultPractice.main(null);
public class Phrase {
private String currentPhrase;
public Phrase(String p) {
currentPhrase = p;
}
public int findNthOccurence(String str, int n) {
int nth = 0;
for (int i = 0; i < currentPhrase.length()-str.length(); i++) {
if (currentPhrase.substring(i, i+str.length()).equals(str)) {
nth++;
if (nth == n) return i;
}
}
return -1;
}
public void replaceNthOccurrence(String str, int n, String repl) {
int index = findNthOccurence(str, n);
if (index != -1) {
currentPhrase = currentPhrase.substring(0, findNthOccurence(str, n)) + repl + currentPhrase.substring(findNthOccurence(str, n)+str.length());
}
}
public String toString() {
return currentPhrase;
}
public static void main(String[] args) {
Phrase phrase1 = new Phrase("A cat ate late.");
phrase1.replaceNthOccurrence("at", 3, "rane");
System.out.println(phrase1);
}
}
Phrase.main(null);
public class Phrase {
private String currentPhrase;
public Phrase(String p) {
currentPhrase = p;
}
public int findNthOccurence(String str, int n) {
int nth = 0;
for (int i = 0; i < currentPhrase.length()-str.length(); i++) {
if (currentPhrase.substring(i, i+str.length()).equals(str)) {
nth++;
if (nth == n) return i;
}
}
return -1;
}
public void replaceNthOccurrence(String str, int n, String repl) {
int index = findNthOccurence(str, n);
if (index != -1) {
currentPhrase = currentPhrase.substring(0, findNthOccurence(str, n)) + repl + currentPhrase.substring(findNthOccurence(str, n)+str.length());
}
}
public int findLastOccurrence(String str) {
int last = -1;
for (int i = 1; findNthOccurence(str, i) != -1; i++) {
if (findNthOccurence(str, i) > last) {
last = findNthOccurence(str, i);
}
}
return last;
}
public String toString() {
return currentPhrase;
}
public static void main(String[] args) {
Phrase phrase1 = new Phrase("A cat ate late.");
phrase1.replaceNthOccurrence("at", 3, "rane");
System.out.println(phrase1);
System.out.println(phrase1.findLastOccurrence("at"));
}
}
Phrase.main(null);
public class Position {
private int r;
private int c;
public Position (int r, int c) {
this.r = r;
this.c = c;
}
public String toString() {
return "(" + r + ", " + c + ")";
}
}
public class Successors {
public static Position findPosition(int num, int[][] intArr) {
for (int i = 0; i < intArr.length; i++) {
for (int j = 0; j < intArr[i].length; j++) {
if (intArr[i][j] == num) {
return new Position(i, j);
}
}
}
return null;
}
public static void main(String[] args) {
int[][] intArr = {{1, 2, 3}, {4, 5, 6}};
System.out.println(findPosition(5, intArr));
}
}
Successors.main(null);
public class Successors {
public static Position findPosition(int num, int[][] intArr) {
for (int i = 0; i < intArr.length; i++) {
for (int j = 0; j < intArr[i].length; j++) {
if (intArr[i][j] == num) {
return new Position(i, j);
}
}
}
return null;
}
public static Position[][] getSuccessorArray(int[][] intArr) {
Position[][] posArr = new Position[intArr.length][intArr[0].length];
for (int i = 0; i < intArr.length; i++) {
for (int j = 0; j < intArr[i].length; j++) {
posArr[i][j] = findPosition(intArr[i][j]+1, intArr);
}
}
return posArr;
}
public static void main(String[] args) {
int[][] intArr = {{1, 2, 3}, {4, 5, 6}};
System.out.println(Arrays.deepToString(getSuccessorArray(intArr)));
}
}
Successors.main(null);
public class WordPair {
private String first;
private String second;
public WordPair(String first, String second) {
this.first = first;
this.second = second;
}
public String getFirst() {
return first;
}
public String getSecond() {
return second;
}
public String toString() {
return "(" + first + ", " + second + ")";
}
}
public class WordPairList {
private ArrayList<WordPair> allPairs;
public WordPairList(String[] words) {
allPairs = new ArrayList<WordPair>();
for (int i = 0; i < words.length; i++) {
for (int j = i+1; j < words.length; j++) {
allPairs.add(new WordPair(words[i], words[j]));
}
}
}
public ArrayList<WordPair> getAllPairs() {
return allPairs;
}
public static void main(String[] args) {
String[] phrase = {"the", "more", "the", "merrier"};
WordPairList example = new WordPairList(phrase);
System.out.println(example.getAllPairs());
}
}
WordPairList.main(null);
public class WordPairList {
private ArrayList<WordPair> allPairs;
public WordPairList(String[] words) {
allPairs = new ArrayList<WordPair>();
for (int i = 0; i < words.length; i++) {
for (int j = i+1; j < words.length; j++) {
allPairs.add(new WordPair(words[i], words[j]));
}
}
}
public int numMatches() {
int matches = 0;
for (WordPair pair : allPairs) {
if (pair.getFirst().equals(pair.getSecond())) matches++;
}
return matches;
}
public ArrayList<WordPair> getAllPairs() {
return allPairs;
}
public static void main(String[] args) {
String[] phrase = {"the", "more", "the", "merrier"};
WordPairList example = new WordPairList(phrase);
System.out.println(example.getAllPairs());
System.out.println(example.numMatches());
}
}
WordPairList.main(null);
public interface StringChecker {
boolean isValid(String str);
}
CodeWordChecker is a StringChecker. 3 parameters: 2 ints and a string. first two are min and max word lengths, and third is a string that cannot occur in the code word. It can also be single parameter with only string that doesn't occur, min and max is 6 and 20 respectively.
public class CodeWordChecker implements StringChecker {
private int minLen;
private int maxLen;
private String badWord;
public CodeWordChecker(int min, int max, String bad) {
minLen = min;
maxLen = max;
badWord = bad;
}
public CodeWordChecker(String bad) {
minLen = 6;
maxLen = 20;
badWord = bad;
}
@Override
public boolean isValid(String str) {
if (str.length() >= minLen && str.length() <= maxLen) {
for (int i = 0; i <= str.length()-badWord.length(); i++) {
if (str.substring(i, i+badWord.length()).equals(badWord)) return false;
}
return true;
}
return false;
}
public static void main(String[] args) {
StringChecker sc1 = new CodeWordChecker(5, 8, "$");
System.out.println(sc1.isValid("happ"));
System.out.println(sc1.isValid("happy"));
System.out.println(sc1.isValid("happy$"));
}
}
CodeWordChecker.main(null);