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

Aldrich Alviar
Aldrich Alviar
2,745 Points

scrabble game

the mHand field is used to represent all the tiles

how to fill out the addTile method so that it takes the char that passed and add to mHand? tnx

ScrabblePlayer.java
public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer(tile+"") {
        mHand += tile+"";
    }

    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;
    }
}

3 Answers

It looks like you were adding the code to your constructor instead of method addTile(). You might reset your code and put the new code inside the addTile method.

and make sure that when you add your parameters for the method that it looks like this: public void addTile(char tile){}.

The " +" " inside the parenthesis with your parameters will throw an error; it requires a variable type and a name that you provide for it only.

Talha Takleh Omar Takleh
Talha Takleh Omar Takleh
3,082 Points

public void addTile(char tile) { // Add the tile to the hand of the player mHand += tile; }

The concept is similar to string concatenation. Also your constructor seems to be wrong and that could produce an error. The code for your constructor should be:-

public ScrabblePlayer() { mHand = ""; //you initialise it to an empty string. }

Alistair Mackay
Alistair Mackay
7,812 Points

As Jeremy & Talha pointed out you were trying to add a tile to your constructor which doesn't really work in this case. This example is trying to get you to use some of the methods mentioned in the previous video like "indexOf" to get boolean variables (true or falsies) from methods.

And using conditional statements to change the outcome of the "hasTile" methods.

The below is what worked for me, it reuses the answer from Task 1 in task 2.

See Below if you're stuck:

public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
      mHand = "";
    }

    public String getHand() {
      return mHand;
    }

    public void addTile(char tile) {
/*Task 1: adds the tile character to the 
member variable.*/
      mHand += tile;
    }

    public boolean hasTile(char tile) {
/*Task 2: Changes isTile from true or false 
depending on the indexof "mHand" is greater 
than 0.*/
      boolean isTile = mHand.indexOf(tile) >= 0;
      if (isTile) {
        addTile(tile);
      }
      return isTile;
    }
}