Homework 4 (Hangman) FAQ

Q: How can I read a single character of text as a char?
A: You cannot read a single char. Instead, read an entire line as a String and then look at its first character.
Q: Do I have to handle the case where the user types a blank line (empty string)?
A: Yes; re-prompt the user if they do this.
Q: How can I return two values from one method?
A: You can't. Maybe you have chosen the wrong method structure; for example, maybe the method should pass those two values as parameters to another method, instead of returning them. Or maybe your method is too large and you should break it into smaller pieces.

Q: How do I compute the best game (most guesses remaining)?

A: You mostly have to figure this out for yourself, but here are some hints. In each game you should keep track of how many guesses are remaining. You should probably also keep track of the best game you have seen so far, and update that value accordingly after every game, if the game is better than the best one seen previously. You will have to carefully manage your returns and parameters to make sure that the right information reaches the right parts of your code, but it can be done.

Q: How do I round the winning percentage, so it would say for example 33.3% rather than 33.3333333%?

A: You don't have to do that. If you really wanted to, you could look up how to use the String.format method, but that is outside the scope of the assignment.
Q: Is it okay to have a method that calls itself? Or to have a Method A that calls Method B, which calls A, which calls B, ...?
A: No, you should not do that on this assignment. Having a method call itself is actually an advanced computing technique called recursion. Recursion is not an appropriate algorithmic technique for solving this problem. You can solve the problem correctly using while loops for repetition, instead of recursion.
Q: Since the program is so random, how can I possibly match the expected output? How can I use the Output Comparison Tool?
A: For starters, we suggest that you match the output from the early stages of the program where the secret word is fixed at "PROGRAMMER". If your program works perfectly for those cases, it is very likely that the output is correct for others. For later phases, you don't have to match the randomly chosen words shown in our output logs. But you should have the same general output format as our logs. If you *do* want to exactly match our output so that you can see the coveted "No differences found" in the Output Comparison Tool, it can be done. To do it, you can force your RandomGenerator object to return the same sequence of random numbers on every run of the program. This is called seeding the RandomGenerator object. Random numbers are generated from a mathematical function, and a seed is an integer you pass to the RandomGenerator object as it's being constructed. For example, to seed your RandomGenerator object with the value 42, you'd write:
RandomGenerator rg = new RandomGenerator();
rg.setSeed(42);

The Output Comparison Tool page shows the seeds we used to generate our logs of expected output. Please also note that in order for the above technique to work, you must create only a single RandomGenerator object throughout your program and pass it to each of your various methods. If you create several new RandomGenerator objects, such as one for every round of the guessing game played, your output might not match the output shown.

Q: What should I do if there's an error reading the lexicon file? What should be in the catch statement? I tried to use a println statement, but it said that the symbol "println" was not found.
A: It doesn't really matter. We won't test you on the file-not-found case, so how you handle the error is up to you. The reason println wasn't found is because only your "Program" class can use that command.
Q: How many fields is it okay to have? Can ______ be a field?
A: You are forbidden to use fields on this assignment, outside of the HangmanLexicon (optional).
Q: What is the difference between a "field" and an "instance variable"?
A: The terms "field" and "instance variable" are synonyms; a field is an instance variable. Many concepts in computer science have more than one name. Another example is the term "method," which is often called a "function" or "subroutine" in other languages.

Java's official term for a globally visible variable in a class is "field", while C++ and some other languages call this concept an "instance variable" or "member variable". Stanford CS culture seems to use the term "instance variable" more, even when writing Java code. This may be because some of the faculty or students learned C++ before Java and got used to that terminology. Sometimes the SLs shorten the phrase "instance variable" as "ivar".

Q: Is the following variable count a field? Can I use a variable like this in my program?
public class Hangman extends ConsoleProgram {
    
    int count = 0;
    
    public void run() {
        ...
        count = 5;
        ...
    }
    
    private void playHangmanGame() {
        if (count < 2) {
            ...
        }
    }
}
A: Whether a variable is a field or not is determined by where you declare it. If you declare it outside of your methods, it's a field/ivar. Yes, it still counts as a field if you declare it without the word 'private'. It may look like an ordinary variable because of the syntax above, but it's still a field/ivar because its scope is throughout the program. Fields should be minimized as much as possible. The Hangman assignment essentially forbids all fields in the Hangman class, other than one for the HangmanLexicon if you want it.
This document and its content are copyright © Marty Stepp, 2014. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.