2.1 Starter Code You are given the file BingoCard.java. It implements a class th
ID: 3858298 • Letter: 2
Question
2.1 Starter Code You are given the file BingoCard.java. It implements a class that holds the 5x5 grid of numbers. The class also has a 5x5 grid of tokens, which let us know if the number has been marked already. The constructor initializes the card with distinct random numbers, and marks the free space at the center of the card with a token. The toString() method builds multiple line, depicting the state of the card. The state shows which numbers have been marked. 2.2 Your Task Your task is to implement the add(int number) method. This method should mark the position of the number given as parameter with a token if it is on the card. Also, the method returns true if we detect a Bingo now that this new position was marked. A Bingo happens when 5 tokens form a line (horitontal, vertical or diagonal). 2.3 Marking Submit your .java file through BlackBoard. The marking is as follow: • find if the number is on our card (3 points) • place a token at the number’s position (2 points) • check if we have bingo in the: – current row (1 point) – current column (1 point) – diagonal (1 point) – other diagonal (1 point) • return true on bingo and false otherwise (1 point) • check for bingo in current row, current column and diagonals using a single loop (2 bonus points) • The program does not compile. (-10 points) • The program crashes. (-4 points) STARTER CODE: public class BingoCard { // Attributes private int[][] numbers; private boolean[][] tokens; // Constructor public BingoCard() { // allocate arrays numbers = new int[5][5]; tokens = new boolean[5][5]; // initialize the card with distinct numbers // note: it doesn't mater that the free space gets a number int[] numbers = generateShuffledNumbers(); int i = 0; for (int r = 0; r < 5; r++) { for (int c = 0; c < 5; c++) { this.numbers[r][c] = numbers[i++]; } } // mark the free space with a token tokens[2][2] = true; } // convert the card to a string showing where we have tokens public String toString() { StringBuilder builder = new StringBuilder(); // for each row for (int r = 0; r < 5; r++) { // for each column for (int c = 0; c < 5; c++) { // get the number, and if it's marked with a token int number = numbers[r][c]; boolean token = tokens[r][c]; // append the number (or FS for the free space) if (r == 2 && c == 2) { builder.append("FS"); // free space } else { builder.append(number); } // append a '*' if the number is marked builder.append(token ? '*' : ' '); builder.append(' '); } // new line between each row builder.append(' '); } return builder.toString(); } // Marks with a token the number if it is on the card. // Also, the method returns true if we have Bingo, false otherwise. public boolean add(int number) { // TODO find the number on our card (3 points) // TODO place a token at its position (2 points) // TODO check if we have bingo in the: // - current row (1 pts) // - current column (1 pts) // - diagonal (1 pts) // - other diagonal (1 pts) // BONUS: check for bingo in current row, current column and diagonals using a single loop (2 pts) // TODO return true on bingo and false otherwise (1 pts) return false; } // Main public static void main(String[] args) { // generate a bingo card BingoCard card = new BingoCard(); int[] numbers = generateShuffledNumbers(); boolean bingo = false; // increase this number if you want to win more often (max: 50) final int K = 25; // listen for the numbers for (int i = 0; i < K && !bingo; i++) { // shout number int number = numbers[i]; System.out.println("Number " + number); // update our card bingo = card.add(number); System.out.println(card); } // shout result if (bingo) System.out.println("Bingo!"); else System.out.println("Bummer."); } // Utility: generates a shuffled array containing all the numbers from 10 to 59. // Note: You do NOT need to understand how to use this function to solve the lab. private static int[] generateShuffledNumbers() { final int base = 10; final int n = 50; int[] numbers = new int[n]; // Fisher–Yates shuffle (inside-out version) for (int i = 0; i < numbers.length; i++) { int j = (int) (Math.random() * (i + 1)); if (i != j) { numbers[i] = numbers[j]; } numbers[j] = base + i; } return numbers; } }
Explanation / Answer
Given below is the completed code. If the answer helped, please rate it. Thank you.
public class BingoCard {
// Attributes
private int[][] numbers;
private boolean[][] tokens;
// Constructor
public BingoCard() {
// allocate arrays
numbers = new int[5][5];
tokens = new boolean[5][5];
// initialize the card with distinct numbers
// note: it doesn't mater that the free space gets a number
int[] numbers = generateShuffledNumbers();
int i = 0;
for (int r = 0; r < 5; r++) {
for (int c = 0; c < 5; c++) {
this.numbers[r][c] = numbers[i++];
}
}
// mark the free space with a token
tokens[2][2] = true;
}
// convert the card to a string showing where we have tokens
public String toString() {
StringBuilder builder = new StringBuilder();
// for each row
for (int r = 0; r < 5; r++) {
// for each column
for (int c = 0; c < 5; c++) {
// get the number, and if it's marked with a token
int number = numbers[r][c];
boolean token = tokens[r][c];
// append the number (or FS for the free space)
if (r == 2 && c == 2) {
builder.append("FS"); // free space
} else {
builder.append(number);
}
// append a '*' if the number is marked
builder.append(token ? '*' : ' ');
builder.append(' ');
}
// new line between each row
builder.append(' ');
}
return builder.toString();
}
// Marks with a token the number if it is on the card.
// Also, the method returns true if we have Bingo, false otherwise.
public boolean add(int number) {
int row = 0, col = 0;
boolean found = false;
// find the number on our card (3 points)
for(row = 0; row < 5; row++)
{
for(col = 0; col < 5; col++)
if(numbers[row][col] == number)
{
found = true;
break;
}
if(found)
break;
}
if(found)
{
// place a token at its position (2 points)
tokens[row][col] = true;
boolean bingo = true; //assume its bingo and check
// check if we have bingo in the:
// - current row (1 pts)
for(int k = 0; bingo && k < 5; k++)
{
if(tokens[row][k] == false)
bingo = false;
}
if(bingo)
return true;
// - current column (1 pts)
bingo = true;
for(int k = 0; bingo && k < 5; k++)
{
if(tokens[k][col] == false)
bingo = false;
}
if(bingo)
return true;
// - diagonal (1 pts)
bingo = true;
for(int k = 0; bingo && k < 5; k++)
{
if(tokens[k][k] == false)
bingo = false;
}
if(bingo)
return true;
// - other diagonal (1 pts)
bingo = true;
for(int k = 0; bingo && k < 5; k++)
{
if(tokens[k][4-k] == false)
bingo = false;
}
if(bingo)
return true;
// BONUS: check for bingo in current row, current column and diagonals using a single loop (2 pts)
}
return false;
}
// Main
public static void main(String[] args) {
// generate a bingo card
BingoCard card = new BingoCard();
int[] numbers = generateShuffledNumbers();
boolean bingo = false;
// increase this number if you want to win more often (max: 50)
final int K = 25;
// listen for the numbers
for (int i = 0; i < K && !bingo; i++) {
// shout number
int number = numbers[i];
System.out.println("Number " + number);
// update our card
bingo = card.add(number);
System.out.println(card);
}
// shout result
if (bingo) System.out.println("Bingo!");
else System.out.println("Bummer.");
}
// Utility: generates a shuffled array containing all the numbers from 10 to 59.
// Note: You do NOT need to understand how to use this function to solve the lab.
private static int[] generateShuffledNumbers() {
final int base = 10;
final int n = 50;
int[] numbers = new int[n];
// Fisher–Yates shuffle (inside-out version)
for (int i = 0; i < numbers.length; i++) {
int j = (int) (Math.random() * (i + 1));
if (i != j) {
numbers[i] = numbers[j];
}
numbers[j] = base + i;
}
return numbers;
}
}
output
sample run 1
=============
Number 36
56 39 32 36* 12
33 10 53 50 29
42 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 20
56 39 32 36* 12
33 10 53 50 29
42 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 11
56 39 32 36* 12
33 10 53 50 29
42 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 59
56 39 32 36* 12
33 10 53 50 29
42 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 42
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 30
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 19
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 14
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 43
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 18
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 21
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 47
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 28
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 25
56 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 56
56* 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49 38
46 37 48 40 41
23 57 52 31 13
Number 49
56* 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57 52 31 13
Number 58
56* 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57 52 31 13
Number 57
56* 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57* 52 31 13
Number 22
56* 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57* 52 31 13
Number 27
56* 39 32 36* 12
33 10 53 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57* 52 31 13
Number 53
56* 39 32 36* 12
33 10 53* 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57* 52 31 13
Number 12
56* 39 32 36* 12*
33 10 53* 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57* 52 31 13
Number 35
56* 39 32 36* 12*
33 10 53* 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57* 52 31 13
Number 16
56* 39 32 36* 12*
33 10 53* 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57* 52 31 13
Number 10
56* 39 32 36* 12*
33 10* 53* 50 29
42* 51 FS* 49* 38
46 37 48 40 41
23 57* 52 31 13
Bummer.
=============
sample run 2
Number 46
49 59 40 34 32
55 57 37 45 27
13 24 FS* 16 17
50 31 41 11 22
23 38 42 36 58
Number 58
49 59 40 34 32
55 57 37 45 27
13 24 FS* 16 17
50 31 41 11 22
23 38 42 36 58*
Number 57
49 59 40 34 32
55 57* 37 45 27
13 24 FS* 16 17
50 31 41 11 22
23 38 42 36 58*
Number 26
49 59 40 34 32
55 57* 37 45 27
13 24 FS* 16 17
50 31 41 11 22
23 38 42 36 58*
Number 11
49 59 40 34 32
55 57* 37 45 27
13 24 FS* 16 17
50 31 41 11* 22
23 38 42 36 58*
Number 40
49 59 40* 34 32
55 57* 37 45 27
13 24 FS* 16 17
50 31 41 11* 22
23 38 42 36 58*
Number 50
49 59 40* 34 32
55 57* 37 45 27
13 24 FS* 16 17
50* 31 41 11* 22
23 38 42 36 58*
Number 47
49 59 40* 34 32
55 57* 37 45 27
13 24 FS* 16 17
50* 31 41 11* 22
23 38 42 36 58*
Number 39
49 59 40* 34 32
55 57* 37 45 27
13 24 FS* 16 17
50* 31 41 11* 22
23 38 42 36 58*
Number 49
49* 59 40* 34 32
55 57* 37 45 27
13 24 FS* 16 17
50* 31 41 11* 22
23 38 42 36 58*
Bingo!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.