Single Dimension Arrays and Double Dimension Arrays Java. Question: CHANGE YOUR
ID: 3685358 • Letter: S
Question
Single Dimension Arrays and Double Dimension Arrays Java.
Question: CHANGE YOUR CODE TO GET THE FOLLOWING IMAGE:
public class GameBoard
{
private int[][] board;
private String title;
private String[] images = {"_", "D", "H", "C", "S"};
private final int BLANK = 0;
private final int DIAMOND = 1;
private final int HEART = 2;
private final int CLUB = 3;
private final int SPADE = 4;
public GameBoard(String inTitle, int height, int width)
{
title = inTitle;
board = new int[height][width];
}
public void initBoard()
{
//this method will set every element in the array to "_"
for( int i =0; i <board.length; i++)
{
for (int j= 0; j<board[i].length; j++)
{
board[i][j]= BLANK;
}
}
}
public void createBoard()
{
//this method will set up the board to match the pictures
// Diamonds should be the character "D"
// Spaces should be the character "S"
// Hearts should be the charater "H"
// Clubs should be the character "C"
for( int i =0; i <board.length; i++)
{
for (int j= 0; j<board[i].length; j++)
{
board[i][j]= DIAMOND;
}
}
}
public void printBoard()
{
String s;
int index;
// this method should print out the title and then the entire board.
for( int i =0; i <board.length; i++)
{
for (int j= 0; j<board[i].length; j++)
{
index=board[i][j];
s= images[index];
System.out.print(s+ " ");
}
System.out.println();
}
} public void printBoard()
{
String s;
int index;
// this method should print out the title and then the entire board.
for( int i =0; i {
for (int j= 0; j {
index=board[i][j];
s= images[index];
System.out.print(s+ " ");
}
System.out.println();
}
}
public String toString()
{
return "[" + title + "]";
}
}
Explanation / Answer
import java.io.*;
public class Democ
{
public static void main(String args[])
{
GameBoard obj = new GameBoard("" ,4,4);
obj.initBoard();
obj.createBoard();
obj.printBoard();
}
}
class GameBoard
{
private int[][] board;
private String title;
private String[] images = {"_", "D", "H", "C", "S"};
private final int BLANK = 0;
private final int DIAMOND = 1;
private final int HEART = 2;
private final int CLUB = 3;
private final int SPADE = 4;
public GameBoard(String inTitle, int height, int width)
{
title = inTitle;
board = new int[height][width];
}
public void initBoard()
{
//this method will set every element in the array to "_"
for( int i =0; i <board.length; i++)
{
for (int j= 0; j<board[i].length; j++)
{
board[i][j]= BLANK;
}
}
}
public void createBoard()
{
//this method will set up the board to match the pictures
// Diamonds should be the character "D"
// Spaces should be the character "S"
// Hearts should be the charater "H"
// Clubs should be the character "C"
for( int i =0; i <board.length; i++)
{
for (int j= 0; j<board[i].length; j++)
{
board[i][j]= DIAMOND;
}
}
}
public void printBoard()
{
String s;
int index;
// this method should print out the title and then the entire board.
System.out.println (title);
for( int i =0; i <board.length; i++)
{
for (int j= 0; j<board[i].length; j++)
{
index=board[i][j];
s= images[index];
if(i==j)
{
System.out.print(s);
}
System.out.print(" ");
}
System.out.println();
}
}
public String toString()
{
return "[" + title + "]";
}
}