11 min read

part a

Write the complete HiddenWord class, including any necessary instance variables, its constructor, and the method, getHint, described above. You may assume that the length of the guess is the same as the length of the hidden word.

public class HiddenWord {
    private String word;

    public HiddenWord(String word) {
        this.word = word;
    }

    public String getHint(String guess) {
        String hint = "";

        for(int i = 0; i < word.length(); i++) {
            String guessLetter = guess.substring(i, i + 1);

            if(word.substring(i, i + 1).equals(guessLetter))
                hint += guessLetter;
            else if(word.indexOf(guessLetter) != -1)
                hint += "+";
            else
                hint += "*";
        }
        return hint;
    }
}

public class partA {
    public static void main(String[] args) {
        HiddenWord puzzle = new HiddenWord("HARPS");

        System.out.println(puzzle.getHint("AAAAA"));
        System.out.println(puzzle.getHint("HELLO"));
        System.out.println(puzzle.getHint("HEART"));
        System.out.println(puzzle.getHint("HARMS"));
        System.out.println(puzzle.getHint("HARPS"));
    }
}
partA.main(null)
+A+++
H****
H*++*
HAR*S
HARPS

Summary

This question was very hard and I needed some help, it was definitely not as easy as the first part. I think that this question is about classes and learning how to use classes and manipulate strings. I think the key algorithm during this part was focused on choosing the string ‘HARPS’ and then checking a series of hints in order to see how it works exactly. I also really liked this FRQ, and I was just thinking wouldn’t it be cool if you could play in the terminal, and it just kind of happened with a lot of help, because I didn’t know how to use Scanner all to well.

Stats: Time: 42 minutes

Date Completed: February 24th, 2023

CollegeBoard Scoring: 8/9

Personal Ranking: 2

Looking back on the FRQs and how they relate to our project, I’ve realized where I need improvement and where I’m more confident.

The FRQs about 2D arrays and programming structures were tough. I couldn’t solve them alone and had to use outside help to finish them. My struggle wasn’t with writing the basic parts but putting everything together into a working program. I need to practice working faster, understanding 2D arrays better, creating complex structures, and knowing when to use string to array conversions.

It’s clear that the FRQ concepts are important for our project too. In the next trimester, I’ll keep comparing our project work to practice FRQs. This way, I can understand the concepts better and see their relevance. It’ll prepare me for the AP exam and future programming challenges.

import java.util.Scanner;

public class Test {
    private String word;

    public Test(String word) {
        this.word = word;
    }

    public String getHint(String guess) {
        String hint = "";

        for(int i = 0; i < word.length(); i++) {
            String guessLetter = guess.substring(i, i + 1);

            if(word.substring(i, i + 1).equals(guessLetter))
                hint += guessLetter;
            else if(word.indexOf(guessLetter) != -1)
                hint += "+";
            else
                hint += "*";
        }
        return hint;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Test puzzle = new Test("EDWIN");

        String guess;
        do {
            System.out.print("Enter your guess: ");
            guess = scanner.nextLine().toUpperCase();

            if (guess.length() != 5) {
                System.out.println("Your guess must be 5 letters long. Try again.");
            } else {
                String hint = puzzle.getHint(guess);
                System.out.println("Hint: " + hint);
                if (hint.equals("EDWIN")) {
                    System.out.println("Congratulations! You've guessed the word!");
                    break;
                }
            }
        } while (true);

        scanner.close();
    }
}
HiddenWord.main(null)
Enter your guess: 

Hint: EDWIN
Congratulations! You've guessed the word!

Scoring

This question was worth 9 points

part a:

  • +1 for uses correct class, constructor, and method headersinterface NumberGroup
  • +1 for Declares appropriate private instance variable
  • +1 for Initializes instance variable within constructor using parameter
  • +6 for getHint
    • +1 Accesses all letters in both guess and hidden word in loop
    • +4 Process letters within loop
      • +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

question specific penalties:

  • -0 Uses get to access letters from strings
  • -0 Consistently uses incorrect name instead of instance variable name for hidden word