This lab contains one file - TwoArray.java is due at 11:59pm on Thursday, October 5 (Borish's Dad's Birthday!)
As usual, when you submit this lab, the only java file in your folder should be TwoArray.java. Please delete all other files from your turn-in folder on google drive except for any revisions that I have not yet graded.
Here is a video version of this lesson if you are interested - https://www.youtube.com/watch?v=qRuKSylIYow
1. Create a class called TwoArray. This class should have the following methods, although you may write more methods to help you if you like in addition to using main to help you test your code.
a. zeroNegs - this method should input a 2d array of ints (int[][]) and return the same 2d int array just with all of the negative numbers replaced with zeroes. For instance, if you send it {{3,4,-6},{5,-7,8}}, your method should return {{3,4,0},{5,0,8}}.
b. sumAll - this method should input a 2d array of ints (int[][]) and return an int that represents the sum of all of the integers in all arrays you send it. So if you call sumAll( {{2,3,5},{4,6,7}} ), it will return 2+3+5+4+6+7 = 27. Think about how you did something similar last week...
c. randomize(int r, int c) - this method inputs two ints and returns a 2d array of doubles (double[][]). The array will have r rows and c columns, and each entry will consist of a random double between 0 and 5 (meaning on the interval [0,5) ). So if I call randomize(2,3), one possible output would be:
{ { 3.451, 0.552, 4.992},
{2.221,2.335,1.225}}
d. biggerThanTwo(int[][] a) - this method inputs a 2d array of ints, and returns a 2d array of booleans. You will need to create a new 2d array of booleans of the same size as a. This array will have the same size as the original array, with each spot being true if the original number in the same spot is greater than 2, and false if the original number in the same spot is not greater than 2. For example if you sent this method: {{2,3,5},{4,6,7},{0,1,2}} it would return the array: {{false,true,true},{true,true,true},{false,false,false}}
e. convertToArray(double[][] bigboy) - this method inputs a 2d array of doubles and returns a 1d arrays of doubles (double[]) containing the same elements from bigboy in row major order (the usual row by row ordering). So if you sent this method {{2,3,5},{4,6,7},{0,1,2}}, it should return the one dimensional array {2,3,5,4,6,7,0,1,2}. (hint: you will want to set up a separate variable to track your location in the array you are going to return)
f. getRow(int[][] nums, int row) - this method inputs a 2d array of ints and an int row. It should return a 1d array of ints representing that row of the array. If you have a 2d array a = {{2,3,5},{4,6,7},{0,1,2},{8,1,3}}, and you call getRow(a,1), it should return the array {4,6,7}. You do not need to worry about that case that row would be out of bounds.
g. getCol(int[][] nums, int col) - this method inputs a 2d array of ints and an int col. It should return a 1d array of ints representing column col of the array. If you have a 2d array a = {{2,3,5},{4,6,7},{0,1,2},{8,1,3}}, and you call getCol(a,2), it should return the array {5,7,2,3} (these are the entries in column 2 of each row). You do not need to worry about that case that col would be out of bounds. Be careful setting up the length of the array you will return!
h. emptyBorders(String[][] words) - this method should input a 2d array of Strings and return a 2d array of Strings. The array it returns should be the same array EXCEPT for the following changes:
-All entries in the top and bottom rows should be changed to an empty String (new String("") )
-All entries in the left most and right most columns should be changed to an empty String as well
-All other entries should have their first and last letters removed
So if the input array is:
{{"hello", "jack", "jim", "joe"},
{"hello", "jack", "jim", "joe"},
{"hello", "jack", "jill", "joe"},
{"hello", "jack", "jill", "joe"}}
Your method should return:
{{"", "", "", ""},
{"", "ac", "i", ""},
{"", "ac", "il", ""},
{"", "", "", ""}}
i. sumAdjacent(int[][] nums, int row, int col) - this method should input a 2d array of ints and two ints (representing a location) and return an int. The int it returns should be the sum of the four adjacent elements (above, below, right and left) to that spot. If any of those adjacent elements would be out of bounds, it will not include them in the sum. HINT: the easiest way to do this is to individually check each of the four directions one at a time to make sure the location in that direction is part of the array. If that location is out of bounds, do not try to add it!
For example, if you have the following array A:
{{2, 5, 7},
{3, 1, 12},
{4, 4, 9},}
if you call sumAdjacent(A,1,1), it should return 24 (because it adds 5+3+12+4).
If you call sumAdjacent(A,0,1), it should return 10 (it adds 2 + 7 + 1 and there is no element above 0,1). It should not cause a runtime error.
If you call sumAdjacent(A,2,2), it should return 16 (it adds 4 + 12, there are no elements below or to the right) and not cause a runtime error.
Make sure your method does not cause an out of bounds error along any of the 4 sides, and test it out thoroughly!
Make sure all your method names, inputs and outputs are correct, then submit TwoArray.java. Once you are done, you may start the next assignment!