Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Delivering the MVP Validation

Kshatriiya .
Kshatriiya .
1,464 Points

Why do we need to use return type boolean for methods applyGuess() and promptForGuess()?

Hello the following codes:

public boolean applyGuess(char letter) {
    letter = validateGuess(letter);
    boolean isHit = mAnswer.indexOf(letter) >= 0;


    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
    return isHit;
  }

 public boolean promptForGuess() {
    Console console = System.console();
    boolean isHitt = false;
    boolean isValidGuess = false;

    while (! isValidGuess) {
    String guessAsString = console.readLine("Enter a letter: ");
    char guess = guessAsString.charAt(0);
      try {
        isHitt = mGame.applyGuess(guess);
        isValidGuess = true;
      } catch (IllegalArgumentException iae) {
        console.printf("%s. Please try again.\n", iae.getMessage());
    }
    }
    return isHitt;

  }

can be changed to type void without any return method like this

  public void applyGuess(char letter) {
    letter = validateGuess(letter);
    boolean isHit = mAnswer.indexOf(letter) >= 0;


    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
    //return isHit;
  }

 public void promptForGuess() {
    Console console = System.console();
    //boolean isHitt = false;
    boolean isValidGuess = false;

    while (! isValidGuess) {
    String guessAsString = console.readLine("Enter a letter: ");
    char guess = guessAsString.charAt(0);
      try {
        mGame.applyGuess(guess);
        isValidGuess = true;
      } catch (IllegalArgumentException iae) {
        console.printf("%s. Please try again.\n", iae.getMessage());
    }
    }
    //return isHitt;

  }

and the game still runs perfectly fine. Is there any reason to use a returnable variable type over void in instances like this?

Thank you.

2 Answers

Kshatriliya, yes. mGame.applyGuess() is what is known as a boolean expression -- assuming applyGuess() returns a boolean. As it will be either true or false, depending on what the method returns. So the statement(s) inside the if's code block will run only if the boolean expression is true.

You are correct that you can (indeed, must) change the return type to void if the function doesn't return anything. The question, though, is are there places in your code that expects a boolean back when they call the two functions. If so, your new code will get a runtime error when those calls are made.

Kshatriiya .
Kshatriiya .
1,464 Points

Thanks for the pointer, I'm just really experimenting with the code at this point but I suspect the game is still incomplete and there might be instances where the method will be called with the boolean type in the future.

Could you please verify something for me, if I called a method with an "if" statement like this:

if (mGame.applyGuess()) {} , //where applyGuess() returns a boolean

does that roughly translate to "if mGame.applyGuess() is true" ? and that If statement couple with a boolean method will only run if that method returns true?