CIS 35A-- Java Programming
Programming Homework Assignment #3
Due Date: Wed., Feb. 15 (uploaded by 11:55 PM)
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: Modify your Prog.HW#2 (CORRECTED IF THERE WERE ERRORS), and it is
the STUDENT'S RESPONSIBILITY to FIX ANY ERRORS that are in HW#2 in HW#3 or
POINTS WILL BE DEDUCTED IN HW#2 AND HW#3 for the same errors, even if the
student didn't get the grading feedback yet for HW#2 (for example, if the student
submits HW#3 early before HW#2 was graded, OR if the student submits HW#2 14
days late, then submits HW#3 the next day without waiting for feedback, any of the
same errors that are in HW#2 and HW#3 will STILL BE DEDUCTED!)
The changes from Prog. HW#2 are: Write a class called MineGrid in which you
declare a 2-dim. array of boolean (I'm calling it grid) as a private instance variable.
Also declare PUBLIC static final variables for MIN_DIM (5) and MAX_DIM (15).
Include the following constructor or instance methods (NO static METHODS in the
MineGrid class), THE ONLY METHOD IN THIS CLASS TO DISPLAY ANYTHING WILL BE
METHOD #8:
1. constructor with an int for the first dimension size, and an int for the second
dimension size, and changing each parameter to MIN_DIM (if it's < MIN_DIM) or
to MAX_DIM (if it's > MAX_DIM), checking each one separately. Allocate
memory for a 2-dim. array of boolean, using the parameters and assigning to the
instance variable. BE SURE NOT TO RE-ALLOCATE MEMORY FOR THE 2-DIM.
ARRAY ANYWHERE ELSE IN THIS PROGRAM!
2. public instance method to return the first dimension size (how many "rows")
(NOT an instance variable in this class, but use the length of grid!)
3. public instance method to return the second dimension size (how many
"columns" in one row) (NOT an instance variable in this class, but use the 2nd
dim. length of grid!)
4. public instance method to set randomly selected spots in the instance variable
(int parameter for number of spots to set is the ONLY parameter) that first calls
initGrid (described below), then assigns the number of spots (randomly chosen)
to true if it's not already true. (this is similar to setRandomValues in #2, but
assign directly to the instance 2-dim. array variable)
5. public instance method to initialize/reset all the elements of the instance 2-dim.
array variable to false (NO PARAMETERS)
6. public instance method to set one spot in the instance 2-dim. array variable to
true, with 2 int parameters (one for the row index, one for the column index).
You MUST check if the row and column are within the bounds of the 2-dim. array
(don't do anything if the row or column are out of bounds)
7. public instance method to return one element of this' 2-dim. array instance
variable, passing row & column indices (make sure the row & column are in the
array bounds, return false if out of range)
8. public instance method to compare 2 MineGrid objects that has ONE PARAMETER
for a MineGrid object to compare to and ANOTHER PARAMETER for the result
Programming Assignment #3 - Page 1 of 3
CIS 35A-- Java Programming
MineGrid. Compare each corresponding element in this' 2-dim. array to the 1st
parameter's 2-dim. array, and assign true to the 2nd MineGrid's 2-dim. array
element if both elements are true, or false otherwise. Make sure you FIRST
check if the dimension sizes of the parameters are the same as this' instance 2dim. array's (if not, do nothing).
9. public instance method to display the instance 2-dim. array, (with one char
parameter, no other parameters) . Be sure to display the column number and
row numbers, and for each element display the char parameter wherever the
grid element is true (space, otherwise)
NOTE: The ONLY method that will have int parameters for the sizes is the
constructor.
Write another class called MineGame. In this class, declare the following PRIVATE
INSTANCE variables: an int for how many spots to set, and 3 MineGrid (NOT 2-dim.
array) one for the user, one for the game, and one for the results (I'm calling them
userGrid, gameGrid, and resultsGrid). Include the following public instance methods
(or constructor):
A. constructor with 3 int parameters (one for how many spots and 2 for the
dimension sizes) in which you assign the parameter for how many spots to its
corresponding instance variable, and assign instances of a MineGrid object for
each MineGrid instance variable, passing the 2 size parameters
B. public instance method to return the first dimension size (how many "rows")
(NOT an instance variable in this class) by returning the return value of one of
the MineGrid's first dimension method
C. public instance method to return the second dimension size (how many
"columns" in one row) (NOT an instance variable in this class) by returning the
return value of one of the MineGrid's first dimension method
D. public instance method (accessor) to return how many spots to set
E. public instance method to start a new game (call the resetting method 5 for each
MineGrid instance variable, then call the setRandomSpots method for the
gameGrid, passing the "how many spots" instance variable)
F. public instance method with 2 int parameters and an int return value to set a spot
in the userGrid, but return -1 if the row parameter OR column parameter is out of
bounds, return 0 if the spot at the userGrid was already true, otherwise, set the
spot in the userGrid (calling its method 6) and return 1
G. public instance method to assign to the resultsGrid by calling the userGrid's
compare method passing the gameGrid and resultsGrid.
H. public instance method to display all the MineGrids with labels (see the test runs)
the userGrid (calling its display method passing 'G'), the gameGrid (calling its
display method passing '*'), and the resultsGrid (calling its display method
passing 'M').
Write another class for main, call it Program3 that is in a separate file and class than
MineGrid and MineGame. Declare in main ONE MineGame object variable (I'm
calling game here), and 3 ints (one for the row size, one for the column size, and one
for how many spots to set), and CHANGE main from Prog. HW#2 to do the following
(IN THIS EXACT ORDER):
Programming Assignment #3 - Page 2 of 3
CIS 35A-- Java Programming
1. Get the first and second dimensions the same way as in Prog. HW#2* (using the
InputMethods static methods), passing the MineGrid final static variables
2. Read into a local variable for number of spots the same way you did in HW#2*
3. Assign an instance of a MineGame to the MineGame main variable, passing the 3
ints you just read from the user
4. **In a loop
Call the instance method for the game to start a new game
Call a static method similar to setInputValues in #2, but instead of passing a 2dim. array, pass the game variable (details in A. below)
Call the instance method for the game to get the results (method G)
Call the instance method for the game to display all (method H)
Call the SAME method you used in HW#2 (FIXED if you had errors) to prompt
the user and read (into a String) if the user wants to play another. If the
method returns true, repeat to **
A. The static method that sets the MineGame's parameter based on user input will
be logically similar to HW#2's setInputValues (FIXED if you had errors in it),
except you won't be accessing directly the userGrid's 2-dim. array and you
won't be resetting anything. In a loop (for setting the correct # of spots), you
MUST ONLY call the MineGame parameter's instance method for setting one
spot in the userGrid (method F), again displaying an error message if it was
already set. Again, use the InputMethod's getInt() method, except what you
pass will be based for the parameter's methods which return the # of row and
# of columns (methods B & C).
Turn in with SAME input specifications using InputMethods given in the HW2_CodeFile
for Prog. HW#2 (*FIXED if you had errors). The output should be the same format as
in HW#2 (but *FIXED if you had errors).
Programming Assignment #3 - Page 3 of 3
MineGrid
main
Heap
mGame
grid
MineGame
userGrid
gameGrid
0
1
2
3
4
5
(similar MineGrid)
resultGrid
(similar MineGrid)
false
false
false
false
false
false
false
false
false
false
(etc)
false
false
false
false
false
Note: the 7 would be the number of spots that will be set (not shown here), and
6 is the number of rows, 5 if the number of columns (not to be hard-coded in your
programs, but just shown here for example)
HINTS FOR PROG. HW#3, method 8:
the parameters are 2 MineGrid (NOT 2-dim. arrays) and they are the
ONLY parameters
DON'T CHANGE ANYTHING in "this" MineGrid nor the parameter's MineGrid!
because you're in the same class as the parameter in method 8 (like in
the add method below), you may access the parameter's private instance
variables DIRECTLY!
/* EXAMPLE OF A CLASS THAT HAS A METHOD SIMILAR IN STRUCTURE TO PROG. HW#3,
METHOD 7:*/
public class Example{
private int [] intArray;
public Example( int [] inums ){
if( inums != null ){
intArray = new int[inums.length];
for( int i=0; i < intArray.length; ++i )
intArray[i] = inums[i];
}
else
intArray = new int[]{10, 20};
}
public void add( Example exParam, Example resParam ){
if( exParam != null && resParam != null &&
exParam.intArray.length == resParam.intArray.length &&
exParam.intArray.length == this.intArray.length)
{
int [] resArray = resParam.intArray;
for( int row = 0; row < this.intArray.length; ++row )
resArray[row] =
this.intArray[row]+exParam.intArray[row];
} // end if
} // end add method
// there may be other methods in here
} // end class Example
/* Example of calling the merge method, assume inside a method in another
class that has the following:
{
Example ex1, ex2, ex3;
ex1 = new Example(new int[]{1, 2, 3});
ex2 = new Example(new int[]{4, 5, 6});
ex3 = new Example(new int[]{0,0,0});
ex1.add(ex2, ex3);
// etc.
}
*/
Purchase answer to see full
attachment