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

Few questions about the variables used in this video!!

Hello, can someone please tell me why did we left the mHits & mMisses variable BLANK!! Also I didn't get the why we put char letter in the parameters of applyGuess variable!! what is contains in "letter" in this code?

Please break it down :) jcorum Steve Hunter

2 Answers

Right,

The three member variables are left uninitialized when declared simply because they get initialized in the constructor. So, once an instance of the Game class is created, the member variables have something assigned to them - both mHits and mMisses are assigned a blank string; mAnswer receives the value of the parameter passed to the constructor. Until an instance is created, there's no need for the variables to contain anything as they don't strictly exist!

The applyGuess method takes a parameter. This parameter is of type char. So that the method knows what is going to be passed to it, we let it know that the incoming parameter is of type char, we also let the method know that the char can be referenced by using the variable name letter. So, inside the method, we can access that character by using the variable name, letter.

The method applyGuess takes a letter (the guessed letter) and sees if that letter is contained within the word we're trying to guess. So, each guess is passed into the method, the method returns a true or false depending whether letter is contained within mAnswer.

Make sense?

Steve.

Hi Isam,

Can you post some code to help me answer the points you've raised?

Thanks,

Steve.

Hello my friend, long time no see :)

here is the code:

public class Game {
  private String mAnswer; 
  private String mHits;
  private String mMisses;

  public Game(String answer) {
    mAnswer = answer;
    mHits = "";
    mMisses = "";
    public boolean applyGuess(char letter){
      boolean isHit = mAnswer.indexOf(letter) >= 0;
      if (isHit) {
        mHits += letter;
      } else {
        mMisses += letter;
      }
      return isHit;
    }

  }
}

I don't know why the code all stacked together!! It's hard to understand

Hiya - sorry; I've been busy at work!

The code is formatted badly because you used inverted commas, not backticks. I've amended your code.

Now, I'll see if I can work out what the problem is!!

Steve.

Thank you, also when you have free minute of your time please tell me how to rotate my profile picture :D it gets rotated when I uploaded for some reason!!

Not sure about rotating your avatar; it's odd that the site does that. Perhaps crop the image to be taller than it is wide and see if that works?

It makes more sense than it used to now, but I need to repeat the video few more times.

OK - what doesn't quite make sense yet? I can try and explain further.

Steve.

Well Steve after repeating Craig video and reading your explanation few times things started to make more sense to me.

Thanks my friend.

No problem. :+1: :smile:

Thanks for the follow on Twitter. :+1: