Task 6.13
Task 6.13
LEARNING TARGETS:
- Practice working with 2D Object Arrays.
- Practice working with Traversals and Proximity Detection
- Working with Visual Java
Background
2D arrays are often used to build particle simulators where each cell of the 2D array is a different particle. We will be
building up to a classic version of this called "Falling Sand".
Instructions
You will need a Sand Class. It should have AT LEAST the following data members and behaviors:
➔ public int val;
➔ public boolean right;
➔ A Constructor that sets val to zero and right to either true or false
➔ An Assignment Constructor that takes in an int for value. Remember to set right to either true or false.
➔ A copy() method that returns a Sand object with the exact same attributes as the this-object. This method must
instantiate a new temporary Sand object, manually change all of its values to match the this-values, and then
return that Sand object — the purpose of this method is to be able to prevent aliasing.
➔ A toString() that prints a period for val == 0, @ for val == 1, and # for val == 10.
We will eventually be building a Visual for this, but for now the rest will be in the main:
● Instantiate two integer values: int row = 6; int col = 15;
● Instantiate a Sand Matrix: Sand[][] piles = new Sand[row][col];
● You will then need to go through and instantiate each cell of the array:
○ All of the cells in the top row should be set to zero.
○ Most cells should be set to zero.
○ Give it a 10% chance that non-top row cells could be set to a 1.
○ And finally, make all of the cells in the bottom row set to 10
PRINT your array to make sure it looks correct. (You might want to write a method in your main that will print this
array, you will be calling it a couple of times. Also, I wouldn't put any spaces between your cells). It should be
something like:
...............
..@.....@......
.......@.....@.
@.........@....
...@@.........@
###############
Run your code a few times to make sure it is doing what you expect and you have no errors.
Throughout the rest of this process be sure to NOT HARDCODE the values of 6 and 10. Use row and
col and/or use the dot-length attribute of the matrix. We will be changing these sizes!
Additionally, this is the minimum requirement. There are optional additions listed at the end of this document. If you know
that you will be finished before everyone else, it is worth checking these out.
In the MAIN:
● Create a for-loop that will run a small number of times (~10). In that loop, execute the following.
○ Every other turn, generate a random index somewhere in the top row, and change that random index cell
to be a 1. (this is adding new "sand" to your matrix)
○ Declare and Instantiate a new temporary Sand Matrix (it's important to do it here in the loop)
○ Fill each bucket in the temp matrix with the corresponding COPY (using the copy() method) from the
piles matrix.
○ Now run a set of nested for-loops to go through every bucket of the piles array and make updates in the
temporary array. ALL CHANGES should be done in the temp array!!!
○ Here are the updates you need to make – AND BE SURE THAT YOU ALWAYS CHECK TO MAKE
SURE YOUR NUMBERS ARE WITHIN BOUNDS
Meaning, lets say your current cell is [r][c], and you want to check to the right of it (c+1)... you need to
make sure that c+1 is still within the array's length:
if( c+1 < piles[0].length && temp[r][c+1] == 0)...
■ Check if the PILES array cell is a 1. If the cell BELOW it (in the TEMP array) is still in the array
and is open (0). SWAP the two cells (in the TEMP array):
■ Check if a cell is a 1. If the cell BELOW it (in the TEMP array) is full (not 0), then check to the
LOWER RIGHT and LOWER LEFT. The order you need to check them in is based on the
boolean right data member: if right == true, check right first and then left; if right == false, check
left first and then right. If the cell is open, swap the two cells.
○ Now set the piles array equal to the temp array, you can use aliasing here: piles = temp;
○ Repeat the loop.
STOP and make sure that your text elements are behaving correctly!
Go back and increase the Row and Columns of your matrix and increase the number iterations of your for-loop. And rerun
the program a few times to make sure it works correctly.