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 Strings and Chars

Stuck

The prompt is.... :
Below is an object that I am using to represent a player in a Scrabble-like game I'm building. The mHand field is used to represent all the tiles the user currently has in their hand.

Can you please fill out the addTile method so that it takes the char that is passed and adds it to the mHand member field? Thanks!

ScrabblePlayer.java
public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand = "";
    }

    public String getHand() {
       return mHand;
    }

    public void addTile(char tile) {
        // Adds the tile to the hand of the player

    }

    public boolean hasTile(char tile) {
       return false;
    }
}

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi there,

look here:

    public void addTile(char tile) {
        // this method takes a tile as argument
       mHand += tile;
      // using += sign you can add the tile to mHand

    }

    public boolean hasTile(char tile) {
      boolean hasTile = mHand.indexOf(tile) != -1;
      // create a boolean
      // the method indexOf returns -1 if there is no tile in mHand
      // so if not (!) -1 ... boolean value is true 
      // if there is no tile in mHand indexOf would return -1
      // hasTile would turn into false
      return hasTile;
      // return the value of hasTile (false or true)
      // in dependance of the indexOf result
    }

Makes sense?

GRigorij