java - Creating a new instance for class with a boolean array -
i'm writing code changes colour of array cell either black or white depending on original colour is. if cell white, changes black , vice versa. i'm working below code:
public class grid { boolean[][] grid; private int height; private int width; public white; public black; public grid(int height, int width) { height = height; width = width; } public int getheight() { return height; } public int getwidth() { return width; } public boolean iswhite(int i, int j) { boolean [][] grid = new boolean [height][width]; ( = 0; i<height ; i++);{ (j = 0; j<width ; j++){ if (grid[i][j] ?? //iswhite) { ??//setblack } else (??)//setwhite } } }} protected void setwhite(int i, int j) { ?? } protected void setblack(int i, int j) { ?? } }
i believe i'm supposed create instance grid[i][j] depending on boolean value i'm not sure how relate 'public boolean iswhite' 'setwhite' , 'setblack'. how should go it?
you have defined boolean grid[][]
class variable. you'll need set in constructor along lines of grid = new boolean[height][width];
recommend initializing values, sanity's sake. (i.e. set them true or false or predefined pattern.) once you've done that, can check grid[i][j]
value, either true or false. (whichever of wish represent black , white.)
for instance, iswhite function simple (assuming white == true)
public boolean iswhite(int i, int j) { return grid[i][j]; }
similarly, set functions relatively minimal:
void setwhite(int i, int j) { grid[i][j] = true; } void setblack(int i, int j) { grid[i][j] = false; }
additional: want create code appropriate thing when flipping colors rather placing inside iswhite function. (it should testing color, not changing values, right?)
void changecoloratyx(int y, int x){ if(iswhite(y, x)){ setblack(y, x); } else { setwhite(y,x); } }
Comments
Post a Comment