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

Questions about this video

hi, i had some questions about why we this lines of code:

class Prompter 
{
  private Game game;

  public Prompter(Game game)//why we create this contsructor
  {
    this.game = game; 
  }

this answer: https://teamtreehouse.com/community/game-game-this-is-so-confusing cleared a few of my questions but i still have some more questions. Livia is saying :''So with 'private Game game', we create a private variable of type 'Game' called 'game' inside the Prompter class. Then we add a constructor with a parameter of type Game, so we can set the value of the private 'game' variable when a prompter object is created"

i have a few questions:

  1. why we create a constructor in the first place for that purpose?
  2. why we need to set the value of the private game variable? and what does it mean "when the object is created?>> it is referring to when we run the program?

Those are the main things that i do not understand yet.

Another question is regarding this line of code :

char guess = guessInput.charAt(0);

why we put 0 in the charAt? the charAt method for my understandings return the letter in the particular index,what is the logic behind putting 0? what happen if we use 1 or 2 regrading to our example?

is it because we need to extract only the first letter per guess?, if someone types jokol and our answer is 'joke' , this line of code only takes the first letter?

Steven Parker Steve Hunter Livia Galeazzi Eric McKibbin i will appreciate ur help.

2 Answers

Steven Parker
Steven Parker
229,744 Points

:bell: Hi, I was alerted by your tag.

The constructor gives the ability to establish the value of "game" at the same time the object is created. Otherwise you'd need to assign it in a separate step, and need an additional "setter" method.

You need the private "game" variable because the method "promptForGuess" must have it to call the "applyGuess" method of the "game".

An example of when the object is created can be seen in the "main" method of the "Hangman" class, on this line:

    Prompter prompter = new Prompter(game);

The "new" operator used there creates a "Prompter" object, which is then assigned to the variable "prompter". You can also see "game" being passed to the constructor.

The argument given to "charAt" is the index of the character being selected, and 0 is the index of the first character. If you wanted to look at the second one instead, you would pass it 1. And 2 would get the third character. In this case, the first character is used because that is what the "applyGuess" method will need to determine the choice the player has made.

And you're right, one advantage to using only the first letter is that it makes spelling errors less likely to cause a problem.

Hi, thanks for ur time to answer my question, but i still dont understand few things.. i didnt understand what u mean by this sentence : ** The constructor gives the ability to establish the value of "game" at the same time the object is created**

also i still dont understand why we need to set this.game = game?

for my understandings we use the this keyword in order to avoid naming collison and to seperate the field variable from the parameter but i still dont understand why we do this to the game variable..

1 other thing is in this code :

    Prompter prompter = new Prompter(game);

i understand tht we create an instance of the Prompter object but why we pass in "game", from where "game" come? >> is it from the :

   Game game = new Game("treehouse");

we first create an instance of the game object to initalize the game and in order to let the user to prompt we create the prompter instance?

Steven Parker
Steven Parker
229,744 Points

When I say, "The constructor gives the ability to establish the value of "game" at the same time the object is created" I mean it lets you use one line instead of two:

    Prompter prompter = new Prompter(game);  // when constructor takes "game" 1 line is enough
// vs.
    Prompter prompter = new Prompter();      // if it didn't, then ...
    prompter.setgame(game);                  // you would need another line and a "setter"

You need to set this.game = game because just "game" is a paramter that is only available to the constructor, but "this.game" is the field variable that is part of the object and it remains avaiable later.

You pass in "game" when you create the new Prompter object because that's how it is given to the constructor.

And you're exactly right about where "game" comes from, and why it is created first.