Question
Question: Check for duplicates in constructor of Bingo Card Bingo.java package bingo; import java.util.Scanner; import java.util.Random; public class Bingo { public static BingoCard gameCard; public
Question: Check for duplicates in constructor of Bingo Card
Bingo.java
package bingo;
import java.util.Scanner; import java.util.Random; public class Bingo {
public static BingoCard gameCard; public static int totalGamesWon = 0; public static void main(String[] args) { //Use do-while loop to do the following: //1. instantiate a gameCard //2. call the method playGame() //3. call the method determineWinner() //4. Ask user if they wish to play again? (1 = yes; 2 = no) //5. Loop while user replies 1 //Display value in totalGamesWon Scanner sc = new Scanner(System.in); int choice; do{ gameCard = new BingoCard(); playGame(); determineWinner(); System.out.print("Do you want to play again?(1 = Yes|2 = No)"); choice = sc.nextInt(); } while(choice==1); } public static void playGame() { Random rand = new Random(); for(int i=1;i<=25;i++){ int value = rand.nextInt(75)+1; if(1<=value && value<=15){ if(gameCard.getbNum1()==value) gameCard.setbNum1(0); else if(gameCard.getbNum2()==value) gameCard.setbNum2(0); else if(gameCard.getbNum3()==value) gameCard.setbNum3(0); else if(gameCard.getbNum4()==value) gameCard.setbNum4(0); else if(gameCard.getbNum5()==value) gameCard.setbNum5(0); } else if(16<=value && value<=30){ if(gameCard.getiNum1()==value) gameCard.setiNum1(0); else if(gameCard.getiNum2()==value) gameCard.setiNum2(0); else if(gameCard.getiNum3()==value) gameCard.setiNum3(0); else if(gameCard.getiNum4()==value) gameCard.setiNum4(0); else if(gameCard.getiNum5()==value) gameCard.setiNum5(0); } else if(31<=value && value<=45){ if(gameCard.getnNum1()==value) gameCard.setnNum1(0); else if(gameCard.getnNum2()==value) gameCard.setnNum2(0); else if(gameCard.getnNum3()==value) gameCard.setnNum3(0); else if(gameCard.getnNum4()==value) gameCard.setnNum4(0); else if(gameCard.getnNum5()==value) gameCard.setnNum5(0); } else if(46<=value && value<=60){ if(gameCard.getgNum1()==value) gameCard.setgNum1(0); else if(gameCard.getgNum2()==value) gameCard.setgNum2(0); else if(gameCard.getgNum3()==value) gameCard.setgNum3(0); else if(gameCard.getgNum4()==value) gameCard.setgNum4(0); else if(gameCard.getgNum5()==value) gameCard.setgNum5(0); } else if(61<=value && value<=75){ if(gameCard.getoNum1()==value) gameCard.setoNum1(0); else if(gameCard.getoNum2()==value) gameCard.setoNum2(0); else if(gameCard.getoNum3()==value) gameCard.setoNum3(0); else if(gameCard.getoNum4()==value) gameCard.setoNum4(0); else if(gameCard.getoNum5()==value) gameCard.setoNum5(0); } } //Loop 25 times, simulating the 25 balls in a BINGO game //Inside loop: //1. Generate a random number between 1 and 75 //2. if the number generated is between 115, // check bNum1, bNum2, bNum3, bNum4, and bNum5 to see if there is a match; // if so, move a 0 to the matching instance variable in the gameCard //3. if the number generated is between 1630, // check iNum1, iNum2, iNum3, iNum4, and iNum5 to see if there is a match; // if so, move a 0 to the matching instance variable in the gameCard //4. if the number generated is between 3145, // check nNum1, nNum2, nNum3, nNum4, and nNum5 to see if there is a match; // if so, move a 0 to the matching instance variable in the gameCard //5. if the number generated is between 4660, // check gNum1, gNum2, gNum3, gNum4, and gNum5 to see if there is a match; // if so, move a 0 to the matching instance variable in the gameCard //6. if the number generated is between 6175, // check oNum1, oNum2, oNum3, oNum4, and oNum5 to see if there is a match; // if so, move a 0 to the matching instance variable in the gameCard } public static void determineWinner() { //1. Print the content of the Bingo Card, ensuring the display is formatted as follows: // bNum1 iNum1 nNum1 gNum1 oNum1 // bNum2 iNum2 nNum2 gNum2 oNum2 // bNum3 iNum3 nNum3 gNum3 oNum3 // bNum4 iNum4 nNum4 gNum4 oNum4 // bNum5 iNum5 nNum5 gNum5 oNum5 System.out.println(gameCard.getbNum1()+" "+gameCard.getiNum1()+" "+gameCard.getnNum1()+" "+gameCard.getgNum1()+" "+gameCard.getoNum1()); System.out.println(gameCard.getbNum2()+" "+gameCard.getiNum2()+" "+gameCard.getnNum2()+" "+gameCard.getgNum2()+" "+gameCard.getoNum2()); System.out.println(gameCard.getbNum3()+" "+gameCard.getiNum3()+" "+gameCard.getnNum3()+" "+gameCard.getgNum3()+" "+gameCard.getoNum3()); System.out.println(gameCard.getbNum4()+" "+gameCard.getiNum4()+" "+gameCard.getnNum4()+" "+gameCard.getgNum4()+" "+gameCard.getoNum4()); System.out.println(gameCard.getbNum5()+" "+gameCard.getiNum5()+" "+gameCard.getnNum5()+" "+gameCard.getgNum5()+" "+gameCard.getoNum5()); //2. Call the gotBingo() method for the gameCard if(gameCard.gotBingo()){ totalGamesWon++; System.out.println("BINGO!"); } else System.out.println("No BINGO"); //3. if gotBingo() returns true, add 1 to totalGamesWon // AND display "BINGO!" //4. if gotBingo() returns false, display "No BINGO" } }
BingoCard.java
package bingo;
import java.util.Random;
public class BingoCard
{
//These ints are initialized to random numbers between
private int bNum1;
private int bNum2;
private int bNum3;
private int bNum4;
private int bNum5;
private int iNum1;
private int iNum2;
private int iNum3;
private int iNum4;
private int iNum5;
private int nNum1;
private int nNum2;
private int nNum3;
private int nNum4;
private int nNum5;
private int gNum1;
private int gNum2;
private int gNum3;
private int gNum4;
private int gNum5;
private int oNum1;
private int oNum2;
private int oNum3;
private int oNum4;
private int oNum5;
public BingoCard()
{ //Constructor for a BingoCard
//Generate a random number for each instance variable in the Bingo Card, using
//the following range of numbers:
// "B" (numbers 115), "I" (numbers 1630),
// "N" (numbers 3145), "G" (numbers 4660),
// and "O" (numbers 6175).
Random myRan = new Random();
bNum1 = myRan.nextInt(15) + 1;
bNum2 = myRan.nextInt(15) + 1;
bNum3 = myRan.nextInt(15) + 1;
bNum4 = myRan.nextInt(15) + 1;
bNum5 = myRan.nextInt(15) + 1;
iNum1 = myRan.nextInt(15) + 16;
iNum2 = myRan.nextInt(15) + 16;
iNum3 = myRan.nextInt(15) + 16;
iNum4 = myRan.nextInt(15) + 16;
iNum5 = myRan.nextInt(15) + 16;
nNum1 = myRan.nextInt(15) + 31;
nNum2 = myRan.nextInt(15) + 31;
nNum3 = myRan.nextInt(15) + 31;
nNum4 = myRan.nextInt(15) + 31;
nNum5 = myRan.nextInt(15) + 31;
gNum1 = myRan.nextInt(15) + 46;
gNum2 = myRan.nextInt(15) + 46;
gNum3 = myRan.nextInt(15) + 46;
gNum4 = myRan.nextInt(15) + 46;
gNum5 = myRan.nextInt(15) + 46;
oNum1 = myRan.nextInt(15) + 61;
oNum2 = myRan.nextInt(15) + 61;
oNum3 = myRan.nextInt(15) + 61;
oNum4 = myRan.nextInt(15) + 61;
oNum5 = myRan.nextInt(15) + 61;
}
public int getbNum1() {
return bNum1;
}
public int getbNum2() {
return bNum2;
}
public int getbNum3() {
return bNum3;
}
public int getbNum4() {
return bNum4;
}
public int getbNum5() {
return bNum5;
}
public int getiNum1() {
return iNum1;
}
public int getiNum2() {
return iNum2;
}
public int getiNum3() {
return iNum3;
}
public int getiNum4() {
return iNum4;
}
public int getiNum5() {
return iNum5;
}
public int getnNum1() {
return nNum1;
}
public int getnNum2() {
return nNum2;
}
public int getnNum3() {
return nNum3;
}
public int getnNum4() {
return nNum4;
}
public int getnNum5() {
return nNum5;
}
public int getgNum1() {
return gNum1;
}
public int getgNum2() {
return gNum2;
}
public int getgNum3() {
return gNum3;
}
public int getgNum4() {
return gNum4;
}
public int getgNum5() {
return gNum5;
}
public int getoNum1() {
return oNum1;
}
public int getoNum2() {
return oNum2;
}
public int getoNum3() {
return oNum3;
}
public int getoNum4() {
return oNum4;
}
public int getoNum5() {
return oNum5;
}
public void setbNum1(int bNum1) {
this.bNum1 = bNum1;
}
public void setbNum2(int bNum2) {
this.bNum2 = bNum2;
}
public void setbNum3(int bNum3) {
this.bNum3 = bNum3;
}
public void setbNum4(int bNum4) {
this.bNum4 = bNum4;
}
public void setbNum5(int bNum5) {
this.bNum5 = bNum5;
}
public void setiNum1(int iNum1) {
this.iNum1 = iNum1;
}
public void setiNum2(int iNum2) {
this.iNum2 = iNum2;
}
public void setiNum3(int iNum3) {
this.iNum3 = iNum3;
}
public void setiNum4(int iNum4) {
this.iNum4 = iNum4;
}
public void setiNum5(int iNum5) {
this.iNum5 = iNum5;
}
public void setnNum1(int nNum1) {
this.nNum1 = nNum1;
}
public void setnNum2(int nNum2) {
this.nNum2 = nNum2;
}
public void setnNum3(int nNum3) {
this.nNum3 = nNum3;
}
public void setnNum4(int nNum4) {
this.nNum4 = nNum4;
}
public void setnNum5(int nNum5) {
this.nNum5 = nNum5;
}
public void setgNum1(int gNum1) {
this.gNum1 = gNum1;
}
public void setgNum2(int gNum2) {
this.gNum2 = gNum2;
}
public void setgNum3(int gNum3) {
this.gNum3 = gNum3;
}
public void setgNum4(int gNum4) {
this.gNum4 = gNum4;
}
public void setgNum5(int gNum5) {
this.gNum5 = gNum5;
}
public void setoNum1(int oNum1) {
this.oNum1 = oNum1;
}
public void setoNum2(int oNum2) {
this.oNum2 = oNum2;
}
public void setoNum3(int oNum3) {
this.oNum3 = oNum3;
}
public void setoNum4(int oNum4) {
this.oNum4 = oNum4;
}
public void setoNum5(int oNum5) {
this.oNum5 = oNum5;
}
public boolean gotBingo()
{
//when a game is being played, if the letter and number match
//the letter and number called out, its value will be zeroed out
//to simulate placing a button on that location. if(bNum1+bNum2+bNum3+bNum4+bNum5==0) return true; else if(iNum1+iNum2+iNum3+iNum4+iNum5==0) return true; else if(nNum1+nNum2+nNum3+nNum4+nNum5==0) return true; else if(gNum1+gNum2+gNum3+gNum4+gNum5==0) return true; else if(oNum1+oNum2+oNum3+oNum4+oNum5==0) return true; else if(bNum1+iNum1+nNum1+gNum1+oNum1==0) return true; else if(bNum2+iNum2+nNum2+gNum2+oNum2==0) return true; else if(bNum3+iNum3+nNum3+gNum3+oNum3==0) return true; else if(bNum4+iNum4+nNum4+gNum4+oNum4==0) return true; else if(bNum5+iNum5+nNum5+gNum5+oNum5==0) return true; else if(bNum1+iNum2+nNum3+gNum4+oNum5==0) return true; else if(bNum5+iNum4+nNum3+gNum2+oNum1==0) return true;
return false;
}
public boolean Vertical()
{
return false;
}
//to determine if the card has BINGO, one of the following conditions must be true:
//1. Vertical: bNum1, bNum2, bNum3, bNum4, and bNum5 must all be 0's OR
// iNum1, iNum2, iNum3, iNum4, and iNum5 must all be 0's OR
// nNum1, nNum2, nNum3, nNum4, and nNum5 must all be 0's OR
// gNum1, gNum2, gNum3, gNum4, and gNum5 must all be 0's OR
// oNum1, oNum2, oNum3, oNum4, and oNum5 must all be 0's
//2. Horizontal: bNum1, iNum1, nNum1, gNum1, and oNum1 must all be 0's OR
// bNum2, iNum2, nNum2, gNum2, and oNum2 must all be 0's OR
// bNum3, iNum3, nNum3, gNum3, and oNum3 must all be 0's OR
// bNum4, iNum4, nNum4, gNum4, and oNum4 must all be 0's OR
// bNum5, iNum5, nNum5, gNum5, and oNum5 must all be 0's
//3. Diagonal: bNum1, iNum2, nNum3, gNum4, and oNum5 must all be 0's OR
// bNum5, iNum4, nNum3, gNum2, and oNum1 must all be 0's
//When any of the above 12 conditions are true, return true. Otherwise, return false;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started