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 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");