Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!