2.1 Starter Code You are given the file BingoCard.java. It implements a class th
ID: 3858303 • 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). 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
I already answered this question before ..
Below is the required code , added code is in bold.
package com.chegg;
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();
}
public boolean add(int number) {
//finding number
int row = 0, column = 0;
for (int r = 0; r < 5; r++) {
// for each column
for (int c = 0; c < 5; c++) {
if (number == numbers[r][c]) {
tokens[r][c] = true;
row = r;
column = c;
}
}
}
for (int i = 0; i < 5; i++) {
if (tokens[row][i] == true) {
return true;
}
if (tokens[i][column] == true) {
return true;
}
}
for (int i = 0, j = 0; i < 5 && j < 5; i++, j++) {
if (tokens[i][j] == true) {
return true;
}
}
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.