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

Ezekiel dela Peña
Ezekiel dela Peña
6,231 Points

About the Game Object?

Does this Game Object came from Game.java? Just to clarify things-out. Thanks in advance!

2 Answers

Conrad Spotts
Conrad Spotts
11,768 Points

Yes, it does. It seems a little confusing because near the beginning of the video the word "game" is used 6 different times in Prompter.java, but the reason it makes sense is that the file Game.java is in the same directory (you can see that in the side panel on workspaces).

Later in the video around the 3:30 minute mark, he says that we need to import the java.io.Console package so that we can use the Console class. That line that he adds at the top allows for the console class to be used in our code here in Prompter.java much like Game.java already is allowed.

For further assurance, (and hopefully I'm making sense, I'm right there with you learning), the following code was written during the video:

public boolean promptForGuess() {
    Console console = System.console();
    String guessAsString = console.readLine("Enter a letter:  ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);
  }

That last line on there returns the result of putting the prompter guess we just defined into the method written in our Game.java file which is applyGuess(char letter). If you change the name of that in Prompter.java, it won't work because it's looking for the name of that method in the Game.java file.

When information like this doesn't make sense to me, I'll talk out the part of the code out loud to myself and if something seems to be redundant or doesn't make sense in the example given in the video, I'll change it to see what it breaks when I run it. Hope this helps!

Csaba Koles
Csaba Koles
4,644 Points

Hi!

Yes, it is. In Java you can reach each file as long as they are in the same folder. That is why he can use the Game object declared with the Game class.