CIS 35A-- Java Programming
Programming Assignment #2 - Page 1 of 3
Programming Homework Assignment #2
Due Date: Mon., May 7 (uploaded by 11:59 PM under Week 5 in Catalyst)
Upload to Catalyst the source files (.java files) and screen output (copied and pasted
into the end of the main file, commented out), all zipped in one file (use input given
on Catalyst)
Problem: Write a Java application program to play Bingo. However, the 2-dim.
arrays to be used in this program will be "sideways", so row 0 is the "B column", row 1
is the "I column", row 2 is the "N column", row 3 is the "G column", and row 4 is the
"O column". Even though there are several ways to write a program for Bingo, below
are more requirements. (If any of you find a good internet page about Bingo, please
post it in the HW2 Forum!)
Declare the following arrays in main— bingoNums1 (2-dim. array of int), bingoNums2
(2-dim. array of int), bingoMatches1 (2-dim. array of boolean), bingoMatches2 (2-
dim. array of boolean). In main, allocate memory for all the 2-dim. arrays so they're
5 X 5 (could do in the declarations). DO NOT RE-ALLOCATE THE 2-DIM. ARRAYS IN
THIS PROGRAM (i.e., always re-use the same 2-dim. arrays). Then main must call the
following methods (logic for main is below):
1. **Call a static method (also in this main class, I'm calling it fillBingoNums, see
details in A. below) to fill the 2-dim. array of int parameter passing bingoNums1
2. Call fillBingoNums again, this time passing bingoNums2.
3. Call displayBingoNums (see B. below) passing "Your Bingo Card: " and
bingoNums1
4. Call displayBingoNums passing "Computer's Bingo Card: " and bingoNums2
5. Call a static method (I'm calling it initBingoMatches, details in C. below), passing
bingoMatches1 to set all the elements in the 2-dim. array to false
6. Call initBingoMatches), passing bingoMatches2
7. Call a static method (I'm calling it playBingo-- see details in D. below) that plays a
Bingo game using bingoNums1, bingoNums2, bingoMatches1, and bingoMatches2
8. Prompt the user and read (into a String) if the user wants to play another. If the
beginning char of the answer string is 'y' or 'Y', then repeat from **
More on the above-mentioned methods:
A. In the fillBingoNums method (has a 2-dim. array of int parameter), fill the 2-dim.
array so that EACH "row" of the 2-dim. array represents a "column" on the Bingo
card in the following way (more explanation in Online Meeting #1):
row 0 has the range 1-15, row 1 has the range 16-30, row 2 has the range
31-45, row 3 has the range 46-60, row 4 has the range 61-75 (POINTS OFF IF
YOU ASSIGN TRANPOSED INSTEAD!)
pseudo-randomly (see last page for how, but DON'T USE A Random object
unless it's private static) assign numbers in the appropriate to each element
(use nested for loops), BUT DO NOT ALLOW DUPLICATES*
row 2, element 2 will be a "free" space, so put -1 (negative 1) in that element
CIS 35A-- Java Programming
Programming Assignment #2 - Page 2 of 3
B. In the displayBingoNums method, display each row of the parameter (2-dim.
array of ints) in columns (sideways), so it displays the String parameter first, then
on the next row "B I N G O", then the next row all the element 0's of each row
of the 2-dim. array, then below that, all the element 1's of each row, etc. (display
FREE if element value is -1) (see Online Meeting video for clarifications)
C. In the initBingoMatches method,
use nested for loops to set each element of the 2-dim. array of boolean
parameter to false
set element [2][2] to true ("free" spot)
D. In the playBingo method, you will use bingoNums1, bingoNums2, bingoMatches1,
and bingoMatches2 passed from main (parameters), and in this method,
bingoNums1 and bingoMatches1 are for the USER, and the other 2 are for the
computer. Declare a LOCAL one-dim. array of boolean (I'm calling it bingoArray )
and allocate 76 elements for it. This method will play a Bingo game (see E. below
for algorithm), using the following methods:
getBingoNumber (GIVEN IN THE HW2 CODE FILE) that will return a
randomly-generated number not already used in the game.
displayBingoNum (GIVEN IN THE HW2 CODE FILE) that will display to
System.out ONE number with the appropriate letter "column"
findMatch (see details in F. below) that checks a bingoNums 2-dim. array if
the number is in it, AND updates the corresponding bingoMatches 2-dim.
array (returns true if it is, false, if not)
checkWin (see details in G. below, SOME of this is in the HW2 Code File) that
checks if a bingoMatches array (parameter) has a row, column or either
diagonal all matched, and returns (in a RETURN STATEMENT) the winning
numbers (from the parameter bingoNums) in a new array of ints (SHOULD
NOT CALL THIS METHOD UNLESS A MATCH WAS FOUND!!!)
E. When "playing" Bingo, do the following
Display some instructions (see test run)
***Get and display the next Bingo number
Check the "user" first, THEN the "computer": (in the following, the "user" is
the player, then the "computer" is the player)
Check if the number is in the bingoNums (2-dim. array) using findMatch
(which will also update the corresponding bingoMatches array)
Display if the player had a match
If the player had a match, check if the player won (call checkWin) and
display if the player won or not, and IF the player won, display the
winning numbers (array that was returned from the checkWin method)
(NOTE: if the USER won, don't check if the computer won)
If no one won, prompt the user if he/she wants to continue, read the answer
in a String
CIS 35A-- Java Programming
Programming Assignment #2 - Page 3 of 3
If the beginning char of the answer isn't 'n' nor 'N', return to main, otherwise,
repeat to ***
F. In the findMatch method,
Check the appropriate row of the bingoNums parameter if the number
(parameter) is in that row * (DON'T CHECK THE WHOLE 2-DIM. ARRAY OR
POINTS WILL BE DEDUCTED FOR INEFFICIENCY, hint: see displayBingoNum)
If it is, set the corresponding element in the bingoMatches parameter to true,
AND (to make it easier to debug, so this is really for the programmer), display
the row and column it was found
Return true (boolean) if it's found, false otherwise
G. In the checkWin method, (SOME OF THIS METHOD IS IN THE HW2 CODE FILE)
Check the bingoMatches parameter for the first row, column or diagonal (be
sure to check right and left diagonals if needed) in which each element is true
and save the numbers in a winning row/column/diagonal in a new one-dim.
array of ints (INCLUDE CODE TO CHECK EACH ROW, COLUMN, DIAGONAL)
Return (in a return statement) the one-dim. array (if win found), null
otherwise
*Suggestion: write a method that does a sequential/linear search for ONE row, and
returns the position in that row where found, or -1 if not found (use this method
where you see *)
DO NOT USE ANY CLASS-SCOPE VARIABLES unless they're static AND final! ALL
METHODS ARE static. (This assignment is NOT object-oriented, but HW#3 will be).
ALWAYS check the array bounds correctly (both dimensions)! Try to make efficient
(including not wasting memory) or points may be deducted! DON'T CHANGE ANY
ALGORITHMS OR SPECIFICATIONS WITHOUT PERMISSION OR POINTS OFF!
TURN IN with output that includes the user winning, the computer winning, someone
winning a "row", someone winning a "column", someone winning a diagonal, and the
user quitting before anyone wins. You may have play several times to get all those
examples. Check your answers carefully (make sure it's a win when it should be)!
HOW TO USE get pseudo-randomly generated integers between MIN and MAX
(inclusive): (int)(Math.random() * (MAX-MIN+1) ) + MIN (OR you may use a
Random object for this but ONLY IF IT'S private static at CLASS SCOPE)
SUGGESTION: To test this, make sure you check if ALL the numbers on a bingoCard
are in the correct range for the "column", that no duplicates in the bingoCard OR bingo
game numbers are duplicates, if it's a match, that your program indicates that it's a
match when it should be, and it finds a win when it should. This isn't easy, but not
impossible. When testing your method that checks if it's a win, display each row and
column that you're testing (and make your output clear so you know you're checking
the correct row and column). When testing, copy and paste your output to a Word
document so you could find matches and wins easier. CHECK YOUR ANSWERS (that
the winning numbers were actually "chosen", that they were in the bingoNums array
AND they were all in a row)!!! If you don't know how to play Bingo, look it up on the
internet!
Extra Credit: See the CodeLab exercises for HW#2 (listed in Catalyst and CodeLab). See CodeLab for the due dates.