Question 2
import java.util.Scanner;
public class BasketballWordGuess {
private String hiddenWord;
public BasketballWordGuess(String word) {
hiddenWord = word;
}
public String getHint(String guess) {
String hint = "";
for (int i = 0; i < guess.length(); i++) {
if (guess.charAt(i) == hiddenWord.charAt(i)) {
hint += guess.charAt(i);
} else if (hiddenWord.indexOf(guess.charAt(i)) >= 0) {
hint += "+";
} else {
hint += "*";
}
}
return hint;
}
public static void main(String[] args) {
BasketballWordGuess game = new BasketballWordGuess("DUNKS");
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Fantasy Basketball Word Guess Game!");
System.out.println("Try to guess the basketball-related word. (Hint: it's 5 letters and all caps)");
boolean hasGuessedCorrectly = false;
while (!hasGuessedCorrectly) {
System.out.print("Take a shot and guess the word: ");
String userGuess = scanner.nextLine().toUpperCase();
String hint = game.getHint(userGuess);
System.out.println("Hint: " + hint);
if (hint.equals(game.hiddenWord)) {
System.out.println("Swish! That's correct! You've guessed the word!");
hasGuessedCorrectly = true;
} else {
System.out.println("Not quite, try again!");
}
}
scanner.close();
}
}
BasketballWordGuess.main(null);
Welcome to the Fantasy Basketball Word Guess Game!
Try to guess the basketball-related word. (Hint: it's 5 letters and all caps)
Take a shot and guess the word: Hint: D*++S
Not quite, try again!
Take a shot and guess the word: Hint: DUN*S
Not quite, try again!
Take a shot and guess the word: Hint: DUNKS
Swish! That's correct! You've guessed the word!
The FRQ required the crafting of a Java class, which was an engaging exercise in applying the fundamentals of Java programming. The task was to encapsulate the essential elements of a class, including instance variables, constructors, and the pivotal getHint method within the HiddenWord class but in this case making it into a basketball theme. The most intellectually stimulating part was devising the logic for the getHint method. This involved constructing conditional statements to compare the player’s guess with the actual hidden word and return a string with asterisks, letters, and plus signs accordingly.