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

Arcee Palabrica
Arcee Palabrica
8,100 Points

game as Argument

Hey guys got a little confused. When we instantiate prompter we put game as an argument in this code:

Prompter prompter = new Prompter(game);

we usually put values here right? like a String or an int. Which game are we referring to?

Thanks :)

1 Answer

Fahad Mutair
Fahad Mutair
10,359 Points

hi ARCEE PALABRICA ,

Hangman.java
Prompter prompter = new Prompter(game);

this line of code is to make object of Prompter.java , basically you called the constructor. if you take a look at that constructor you will see it takes Game game as parameter

Prompter.java
public Prompter(Game game) {
   this.game = game; 
  }

so here we have to pass game as argument, so we have to make an object to Game.java class that's why we create this line of code

Hangman.java
Game game = new Game(args[0]);

and this line of code it needs another argument to be passed , lets take a look at the game constructor

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

you can see game constructor is has (String answer) as parameter so again we need to pass an argument to game constructor that's why we typed (args[0]) that comes from main method (String[] args)

Prompter prompter = new Prompter(game);

we usually put values here right? like a String or an int. Which game are we referring to?

so basically game it refer to this line Game game = new Game(args[0]);

Arcee Palabrica
Arcee Palabrica
8,100 Points

Hey Fahad Mutair ... Thanks so much for the help. :)

Maybe I just got confused by all the variable names being the same. So if we created an object with this line of code:

Game g = new Game (args[0]);

then the line of code for creating the prompter object is:

Prompter prompter = new Prompter(g);

am I correct?