Computer Science A
Course Progress
0/0
OCS Build and Lesson
Code Runner - Java
Code Runner - Examples
Code Runner - JavaScript
FRQ - Methods and Control Structures
Challenge Submission Test
2021 FRQ 3
2023 FRQ 3
2024 FRQ 3
2024 FRQ 2
2024 FRQ 1
2024 FRQ 4
FRQ 2 - Sign Class
2023 FRQ 1
2021 FRQ 2
2019 FRQ 4
2019 FRQ 2
2019 FRQ 1
2016 FRQ 3
2018 FRQ Question 4
2018 FRQ Question 3
2018 FRQ Question 2
2018 FRQ Question 1
2017 FRQ 4
2017 FRQ 3
2017 FRQ Question 2
2017 FRQ 1
2016 FRQ 4
2016 FRQ 2
2016 FRQ Q1
FRQ - 2D Arrays
FRQ - ArrayLists
2025 FRQ 4
2025 FRQ 3
2025 FRQ 2
2025 FRQ 1
FRQ - Classes
FRQ - Array
2023 FRQ 4
2022 FRQ 4
2022 FRQ 3
2022 FRQ 2
2022 FRQ 1
2021 FRQ 4
2021 FRQ 1
2015 FRQ 4
2015 FRQ 2
2015 FRQ 1
2015 FRQ 3
2014 FRQ Q2 - Writing a Class
2019 FRQ 3
2014 FRQ 1
Sprint View
Week 19
2015 FRQ 2
2015 FRQ 2
3 min read
- FRQ Question 2
- Code Runner Challenge
FRQ Question 2
Consider a guessing game in which a player tries to guess a hidden word. The hidden word contains only capital letters and has a length known to the player. A guess contains only capital letters and has the same length as the hidden word.
After a guess is made, the player is given a hint that is based on a comparison between the hidden word and the guess. Each position in the hint contains a character that corresponds to the letter in the same position in the guess. The following rules determine the characters that appear in the hint.
| If the letter in the guess is … | the corresponding character in the hint is |
|---|---|
| also in the same position in the hidden word, | the matching letter |
| also in the hidden word, but in a different position | "+" |
| not in the hidden word, | "*" |
| Call to getHint | String returned |
|---|---|
| puzzle.getHint("AAAAA") | "+A+++" |
| puzzle.getHint("HELLO") | "H****" |
| puzzle.getHint("HEART") | "H*++*" |
| puzzle.getHint("HARMS") | "HAR*S" |
| puzzle.getHint("HARPS") | "HARPS" |
Code Runner Challenge
HiddenWord
View IPYNB Source
// CODE_RUNNER: HiddenWord
public class Main {
// ===============================
// 2015 FRQ #2 — HiddenWord class
// ===============================
public static class HiddenWord {
// STUDENT CODE STARTS HERE
private String word;
// STUDENT CODE ENDS HERE
/**
* Constructs a HiddenWord object.
* @param hWord the word to be guessed
* Precondition: hWord contains only uppercase letters.
*/
public HiddenWord(String hWord) {
// STUDENT CODE STARTS HERE
word = hWord;
// STUDENT CODE ENDS HERE
}
/**
* Returns a hint for guess.
* @param guess the word guessed
* Precondition: guess has the same length as the hidden word
* and contains only uppercase letters.
*/
public String getHint(String guess) {
// STUDENT CODE STARTS HERE
String hint = "";
for (int i = 0; i < guess.length(); i++) {
String guessLetter = guess.substring(i, i + 1);
if (guessLetter.equals(word.substring(i, i + 1))) {
hint += guessLetter;
} else if (word.indexOf(guessLetter) != -1) {
hint += "+";
} else {
hint += "*";
}
}
return hint;
// STUDENT CODE ENDS HERE
}
}
// ===============================
// Driver to prove it runs
// ===============================
public static void main(String[] args) {
HiddenWord puzzle = new HiddenWord("HARPS");
System.out.println(puzzle.getHint("AAAAA")); // +****
System.out.println(puzzle.getHint("HELLO")); // H****
System.out.println(puzzle.getHint("HEART")); // H*++*
System.out.println(puzzle.getHint("HARMS")); // HAR*S
}
}
// Required by runner
Main.main(null);
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
| Points | Criterion |
|---|---|
| +1 | Uses correct class, constructor, and method headers |
| +1 | Declares appropriate private instance variable |
| +1 | Initializes instance variable within constructor using parameter |
| +1 | Accesses all letters in both guess and hidden word in loop (n bounds errors in either) |
| +1 | Extracts and compares corresponding single letters from guess and hidden word |
| +1 | Tests whether guess letter occurs in same position in both guess and hidden word |
| +1 | Tests whether guess letter occurs in hidden word but not in same position as in guess |
| +1 | Adds correct character exactly once to the hint string based on the test result |
| +1 | Declares, initializes, and returns constructed hint string |