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) Creating the MVP Prompting for Guesses

Brent Mayberry
Brent Mayberry
1,029 Points

Reason for using "return mGame.applyGuess(guess)"?

Starting at 4:13 in the video, Craig explains he's returning the boolean value for the applyGuess() method located the Game class, but he's calling that method from the Prompter class.

My question is why is the mGame object necessary? Does the mGame object in the Prompter class allow methods in the Game class to be called? Is it like a link between the two classes? Is this why the constructor was used to make the member field mGame an object of type Game?

public boolean promptForGuess() {
  Console.console = System.console();
  String guessAsString =  console.readLine("Enter a letter:  ");
  char guess = guessAsString.charAt(0);
  return mGame.applyGuess(guess);
}

Thanks for helping me understand!

1 Answer

Brian Fallon
Brian Fallon
4,733 Points

The answer to your questions would be yes. the mGame object is an instance of the game. If you did not have a game object created then you would not be able to use the methods in the Game class because there would not actually be a running instance of a game to run operations on.

Brent Mayberry
Brent Mayberry
1,029 Points

Thanks! This is one of "those questions" with an obvious answer to everyone but me. :-)

Brian Fallon
Brian Fallon
4,733 Points

Not a problem at all! Everyone has those moments and is a reason why the forums are here!