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 Delivering the MVP Determining If the Game is Won

Wan Nor Adzahari Wan Tajuddin
Wan Nor Adzahari Wan Tajuddin
2,438 Points

What is the purpose of the Game(String answer) method>

I know this maybe a little too late into the track to ask this question, but what does this block of code does?

public Game(String answer){ this.answer = answer.toLowerCase(); hits = ""; misses = ""; }

And in the Hangman.java object, what does this line of code do? Game game = new Game("treehouse");

1 Answer

Fahad Mutair
Fahad Mutair
10,359 Points

this line of code is constructor and it will initialize the answer ,that you will pass it whenever you call it in your case "treehouse", to the private answer in Game class and it will set hits to null and set misses to null also

public Game(String answer){ 
this.answer = answer.toLowerCase(); 
hits = ""; 
misses = ""; 
}

and the line of code will create an object of Game class to be used in Hangman.java it will call that constructor and it will pass "treehouse" to (String answer) and it will assign it inside that constructor as you can see in this line this.answer = answer.toLowerCase();

Game game = new Game("treehouse");