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

James Barrett
James Barrett
13,253 Points

Prompter.java - Calling objects is confusing me!

public class Prompter {
      private Game mGame;
    }

      public Prompter(Game game) {
        mGame = game;
      }

I am really struggling to understand what these pieces of code are doing! Is the main objective of this code to allow the Game class to understand this logic?

Furthermore:

    boolean isHit = prompter.promptForGuess();

Struggling to understand what this code is achieving too. Is this a completely different variable to the one created in the Game class? I believe I understand the code following that. The promptForGuess method is called which is then calling the applyGuess method to return a single character.

Very slow learner of Java so please correct me if I am using teh wrong terminology too!

Thanks, James.

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey James,

I looked back at my code when I did this to rejog my memory and I remember how this was an awful struggle for me.

So right now we have a Game.java, Prompter.java, and a Hangman.java. The Game.java is contains the game class which has a answer, details, (im not sure how far youve got as I only have the completely finished code but basically that game class should have a bunch of methods and a bunch of other stuff).

When it says this:

public class Prompter {
      private Game mGame;
    }

      public Prompter(Game game) { //here we have to pass in a game object
        mGame = game;
      }

when we create a Prompter object, we have to pass in a game object as a paramenter. The capitalized Game is the data type and the lowercase game is just a variable that can be anything but this must be a game object This game object will basically have all the details of the game and the answer and the stuff i mentioned in the top. So we are basically putting that game object inside of your prompter object and storing it in a Game type object in what we called as mGame. now we can easily use that game object from within the Prompter object. I hope that made sense but it is a pretty difficult concept to grasp at first.

in this part:

boolean isHit = prompter.promptForGuess();

it basically is basically telling the prompter object to run that promptForGuess() method and return a true or false and store it as isHit. i believe you created that prompter object in your main hangman.java file

since I only have the completed file and its completely different that what you have, im not sure what you have in your code at the moment and the specifics of your question. i hope this helped but you probably are still confused. if you have any lingering questions, please post the rest of your code so we can dive deeper and clear up anything. im happy to help!

Cheers!

James Barrett
James Barrett
13,253 Points

This was an excellent answer, this has cleared up everything for me! I can move on with confidence. Thank you so much!

Kevin Faust
Kevin Faust
15,353 Points

No problem! Glad to hear that you now understand the concept

Happy coding,

Kevin