Reputation: 211
I have to create a program that creates a scrabble tile. I have no problem creating the letter and value, but I need to have an id number for each tile and that's where the problem is. For my class objects I have:
char tile;
int value;
private static int ID = 0;
//Default Constructor
public STile() {
this.tile = '?';
this.value = 0;
STile.ID++;
}
//Constructor with parameters.
public STile(char tile, int value) {
this.tile = tile;
this.value = value;
STile.ID++;
}
And my getter for this is:
public static int getID() {
return STile.ID;
}
The tiles are created fine but the ID of every tile is 26(the for loop to create them runs 26 times). So every letter should be a different number 1-26. I tried setting a variable equal to STile.ID and that didn't seem to work either, So I'm back to what I have up top.
EDIT:
So I retried setting it to a variable and it worked.
public LTile(){
this.tile = '?';
this.value = 0;
count = LTile.ID;
LTile.ID += 1;
}
public LTile(char tile, int value){
this.tile = tile;
this.value = value;
count = LTile.ID;
LTile.ID += 1;
Upvotes: 1
Views: 909
Reputation: 113
Maybe have two ID values, one static and one not, then after incrementing that static ID, set the objects ID equal to the static ID.
Upvotes: 5