Question 2: Classes
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.
import java.util.Scanner;
public class HiddenWord {
private String word;
public HiddenWord(String hWord) {
word = hWord;
}
public String getHint(String guess) {
String hint = "";
for (int i = 0; i < guess.length(); i++) {
if (guess.substring(i, i + 1).equals(word.substring(i, i + 1))) {
hint += guess.substring(i, i + 1);
} else if (word.indexOf(guess.substring(i, i + 1)) != -1) {
hint += "+";
} else {
hint += "*";
}
}
return hint;
}
public static void main(String[] args) {
HiddenWord puzzle = new HiddenWord("HARPS");
Scanner scanner = new Scanner(System.in);
System.out.println("LET'S PLAY WORDLE!");
int attempts = 0;
while (true) {
attempts++;
System.out.print("Attempt " + attempts + ": Enter your guess: ");
String guess = scanner.nextLine().toUpperCase();
String hint = puzzle.getHint(guess);
System.out.println("Hint: " + hint);
if (hint.equals(puzzle.word)) {
System.out.println("CONGRATS! You've guessed the word of the day: " + puzzle.word);
break;
} else if (attempts >= 6) {
System.out.println("Sorry, you've run out of attempts. The word was: " + puzzle.word);
break;
} else {
System.out.println("Try again! Attempts left: " + (6 - attempts));
}
}
scanner.close();
}
}
HiddenWord.main(null);
Output:
LET'S PLAY WORDLE!
Attempt 1: Enter your guess: HEART
Hint: H*++*
Try again! Attempts left: 5
Attempt 2: Enter your guess: HARPS
Hint: HARPS
CONGRATS! You've guessed the word of the day: HARPS
Explanation
I started designing this class by first initializing a private instance variable called ‘word’ to store the hidden word. The class has a constructor that takes the hidden word as an argument and initializes the word
variable with it. The most important method in this class is the getHint
method. This method takes a player’s guess as input and returns a hint based on the comparison between the guess and the hidden word. It iterates through each character in the guess and compares it with the corresponding character in the hidden word. If the characters match, it adds the character to the hint. If the character exists in the hidden word but in a different position, it adds a ‘+’ to the hint. If the character does not exist in the hidden word, it adds a ‘*’ to the hint. In order to test it in a creative manner, I made the HiddenWord guided user interface (GUI) similar to the popular word game Wordle. I did this because I was unable to make this similar to my project.
The code segment below shows an example of another class I created for my project for the Night at the Museum (NaTM)
package com.nighthawk.spring_portfolio.mvc.knn;
import java.util.List;
public class Worker {
private String name;
List<String> languagesKnown;
String preferredLocation;
private boolean internshipPreferred;
// Constructors
public Worker(String name, List<String> languagesKnown, String preferredLocation,
boolean internshipPreferred) {
this.name = name;
this.languagesKnown = languagesKnown;
this.preferredLocation = preferredLocation;
this.internshipPreferred = internshipPreferred;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getLanguagesKnown() {
return languagesKnown;
}
public void setLanguagesKnown(List<String> languagesKnown) {
this.languagesKnown = languagesKnown;
}
public String getPreferredLocation() {
return preferredLocation;
}
public void setPreferredLocation(String preferredLocation) {
this.preferredLocation = preferredLocation;
}
public boolean isInternshipPreferred() {
return internshipPreferred;
}
public void setInternshipPreferred(boolean internshipPreferred) {
this.internshipPreferred = internshipPreferred;
}
}
Some similarities with the class that I designed for this FRQ and this class is that they both have private variables: an example of such in the former would be private String word
, and an example of such in the latter would be private String name
. However, the methods in this class are primarily for “getting” and “setting” variables, which makes sense because this was mostly for the purposes of CRUD functions.
```