Question

In: Computer Science

JAVA!!!! int[][] BingoCardArray = new int[5][5]; Use a nested for-loop, to populate the 2-D array representing...

JAVA!!!!

int[][] BingoCardArray = new int[5][5];

Use a nested for-loop, to populate the 2-D array representing 5 columns for B – I – N – G – O, where
row 1 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 – 75

row 2 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 - 75

row 3 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 – 75

row 4 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 - 75

row 5 =
        col 1 = random # 1- 15
        col 2 = random # 16 – 30
        col 3 = random # 31 – 45
        col 4 = random # 46 – 60
        col 5 = random # 61 – 75


The playGame()method in the BingoGame (Driver) class will loop to generate 50 random numbers in the range of 1 to 75.  This simulates drawing 50 numbers in a BINGO game.  The method will pass the number to a method, checkBingo()in the domain class, BingoCard, which will check if the number received as a parameter is in the B range (1 – 15), I range (16 – 30), N range (31 – 45), G range (46 -60) or O range (61 – 75).  According to the range it is in, the method will check each of the cells in designated column (col 0 = B; col 1 = I; col 2 = N; col 3 = G; col 4 = O).  Example: randomNum 27 was randomly generated, which belongs to the I-range, column 1, checking rows 0 to 4.

for (int row = 0; row <= 4; row++)
{  
     //hard-code column 1 since checking I-column:
    if (randomNum == BingoCardArray[row][1])

    {
             BingoCardArray[row][1] = 0;
    }
}

etc.    //the 0 being moved to the number simulates putting a chip on the Bingo card, marking that the
          // number was called.

The gotBingo()method in the Bingo (Domain) class will return TRUE if 5 numbers were set to 0 either horizontally, vertically, or diagonally.  Otherwise, the method will return FALSE.

Each horizontal checkis a set of if-else if statements

for (int row = 0; row < 5; row++)
{
     int rowTotal = 0;

     for (int col = 0; col < 5; col++)
    {

           //add up all the column values in that row, and see if they add up to 0.

           ….

     }

}

Each vertical checkis a set of if-else if statements

for (int col = 0; col  < 5; col++)
{
     int colTotal = 0;

     for (int row = 0; row < 5; row++)
    {

           //add up all the row values in that column, and see if they add up to 0.

           ….

     }

}

//for diagonals check….
for (int row = 0; row < 5; row++)
{
     int diagonal1Total = 0;

     int diagonal2Total = 0;

     for (int col = 0; col < 5; col++)
    {

           //figure out if it is a diagonal going one way

           //figure out if it is a diagonal going the other way

           ….

     }

}

When gotBingo()ends, it will have either returned a TRUE or a FALSE.

In the driver class, the totalGamesWon is a global variable for keeping track of how many games the user wins instead of the computer. Before the

The determineWinner()method in the Bingo (Driver) class will call the gotBIngo() method in the domain class, and if it returns true, it will add 1 to the totalGamesWon, otherwise it will leave the totalGamesWon alone.

Finally, the mainmethod will contain a do-while loop that will execute at least once, and do the following:

  1. Instantiate the BingoCard class into an object
  2. Call the playGame() method in the driver class
  3. The playGame() method will call the checkBingo(int aNum) in the domain class
  4. Call the determineWinner() method in the driver class, which calls the gotBingo() method in the domain class, and add 1 to totalGamesWom whenever gotBingo() returns true.
  5. Ask user if he/she wants to repeat and play BINGO again.

Solutions

Expert Solution

//Java code

import java.util.Random;

public class BingoCard {
    private int[][] BingoCardArray ;
    Random random= new Random();
    private static int totalGameWon=0;
    //constructor
    public BingoCard()
    {
        BingoCardArray  = new int[5][5];
        /**
         * generate 50 random numbers in the range of 1 to 75.
         */
        for (int i = 0; i <BingoCardArray.length ; i++) {
            /**
             *         col 1 = random # 1- 15
             *         col 2 = random # 16 – 30
             *         col 3 = random # 31 – 45
             *         col 4 = random # 46 – 60
             *         col 5 = random # 61 – 75
             */
            //B
            BingoCardArray[i][0]=random.nextInt(15)+1;
            //I
            BingoCardArray[i][1]=random.nextInt(15)+16;
            //N
            BingoCardArray[i][2]=random.nextInt(15)+31;
            //G
            BingoCardArray[i][3]=random.nextInt(15)+46;
            //O
            BingoCardArray[i][4]=random.nextInt(15)+61;

        }
    }

    public int[][] getBingoCardArray() {
        return BingoCardArray;
    }

    public static int getTotalGameWon() {
        return totalGameWon;
    }

    /**
     * checkBingo()in the domain class, BingoCard,
     * which will check if the number received as a parameter
     * is in the B range (1 – 15),
     * I range (16 – 30), N range (31 – 45), G range (46 -60) or O range (61 – 75)
     * @param aNum
     */
    public void checkBingo(int aNum)
    {
        for (int row = 0; row <= 4; row++)
        {


            //hard-code column 1 since checking B-column:
            if (aNum == BingoCardArray[row][0])

            {
                BingoCardArray[row][0] = 0;
            }
            //I column
            else if(aNum == BingoCardArray[row][1])
            {
                BingoCardArray[row][1]=0;
            }
            //N column
            else if(aNum == BingoCardArray[row][2])
            {
                BingoCardArray[row][2]=0;
            }
            //G column
            else if(aNum == BingoCardArray[row][3])
            {
                BingoCardArray[row][3]=0;
            }
            //O column
            else if(aNum == BingoCardArray[row][4])
            {
                BingoCardArray[row][4]=0;
            }
        }
    }
    public boolean determineWinner()
    {
        if(gotBingo())
        {
            totalGameWon++;
            return true;
        }
        else
        {
            return false;
        }
    }


    public boolean gotBingo()
    {
        boolean isBingo = false;
        //check horizontal
        int[] rowTotal = new int[BingoCardArray.length];
        for (int i = 0; i <BingoCardArray.length ; i++) {
            int sum=0;
            for (int j = 0; j <BingoCardArray[0].length ; j++) {
                sum+=BingoCardArray[i][j];
            }
            rowTotal[i]=sum;

        }
        //check vertical
        int[] colTotal=new int[BingoCardArray[0].length];
        for (int i = 0; i <BingoCardArray.length ; i++) {
            int sum=0;
            for (int j = 0; j <BingoCardArray[0].length ; j++) {

                sum+=BingoCardArray[j][i];
            }
            colTotal[i]= sum;

        }
        if(rowTotal[0]==0 || rowTotal[1]==0|| rowTotal[2]==0 || rowTotal[3]==0 || rowTotal[4]==0)
            isBingo= true;
        else if(colTotal[0]==0 || colTotal[1]==0||colTotal[2]==0||colTotal[3]==0||colTotal[4]==0)
            isBingo= true;
        //check diagonal
        if(  (BingoCardArray[0][0]==0 && BingoCardArray[1][1]==0 && BingoCardArray[2][2]==0 && BingoCardArray[3][3]==0 && BingoCardArray[4][4]==0)
                ||      (BingoCardArray[4][4]==0 && BingoCardArray[3][3]==0 && BingoCardArray[2][2]==0 && BingoCardArray[1][1]==0 && BingoCardArray[0][0]==0) )


            isBingo= true;

        return isBingo;


    }

    @Override
    public String toString() {
        String result="";
        for (int i = 0; i <BingoCardArray.length ; i++) {
            for (int j = 0; j <BingoCardArray[0].length ; j++) {
                result+=String.format("%-5.30s",BingoCardArray[i][j]);
            }
            result+="\n";
        }
        return result;
    }
}

//===========================================

import java.util.Random;
import java.util.Scanner;

public class BingoGame {

    public static void main(String[] args) {
        BingoCard bingoCard;
        Scanner input = new Scanner(System.in);
        while (true) {
            bingoCard=new BingoCard();
            while (true) {

                playGame(bingoCard);
                System.out.println(bingoCard);
                if (bingoCard.determineWinner()) {
                    System.out.println("You got Bingo..");
                    break;
                } else {
                    System.out.println("Ohhh.. You not got Bingo this time.");
                    System.out.println("Retry (y/n): ");
                    String choice = input.nextLine();
                    if (choice.charAt(0) == 'N' || choice.charAt(0) == 'n')
                        break;
                }

            }
            System.out.println("Do you really wanna exit (y/n): ");
            String choice = input.nextLine();
            if (choice.charAt(0) == 'Y' || choice.charAt(0) == 'y')
                break;
        }
        System.out.println("Total matches win by you: "+BingoCard.getTotalGameWon());

    }
    public  static void playGame(BingoCard bingoCard)
    {
        Random random = new Random();
        for (int row = 0; row <= 4; row++)
        {
            int randomNum = random.nextInt(75)+1;

           bingoCard.checkBingo(randomNum);
        }
    }

}

//Output

//If you need any help regarding this solution.......... please leave a comment ........ thanks


Related Solutions

Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
IN JAVA Create a program that uses a nested for loop to display a rectangle of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below). Get the number of rows and columns from the user, and display the result. Examples: If the user provided rows=4, and columns=7, the program should display a pattern as follows: # # # # # # # # #...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and produces as output the sum of integers in an array, the largest and the smallest element in an array. Your program should contain the following methods: public static int sum(int[]) public static int findLargest(int[]) public static int findSmallest(int[])
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested...
Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested loop Given that for any polynomial of degree n, p(x) = anxn + an-1 xn-1 + …a1x + a0, p(x) = O(xn).  Show that: an expression such as the following                                                               4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.       Which of the following statements are true?                                              The running time of a loop is, at most, the running time of...
In Java: Write a nested loop that allows the user to continuously enter phrases and output...
In Java: Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case). Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested...
Be able to determine the runtime complexity of: A Java assignment statement A single loop Nested loop Given that for any polynomial of degree n, p(x) = anxn + an-1 xn-1 + …a1x + a0, p(x) = O(xn).              Show that:            an expression such as the following                                                               4x3 + 7x2 + 12 is O(x3), by finding the witnesses, C and k.      Which of the following statements are true?                                              The running time of a loop is, at most, the running time of...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int x, int y) { x = 1; y = 3; } For each of the following parameter-passing methods, saw what the final values in the array A would be, after the call to f. (There may be more than one correct answer.) a. By value. b. By reference. c. By value-result.
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT