Skip to content

Instantly share code, notes, and snippets.

@YashTotale
Last active March 23, 2021 00:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YashTotale/e297db925273be7f704c906d1464112d to your computer and use it in GitHub Desktop.
Save YashTotale/e297db925273be7f704c906d1464112d to your computer and use it in GitHub Desktop.
TwoDimensionalArrays: AP Computer Science Assignment
import java.util.Arrays;
public class TwoDimensionalArrays {
public static void main(String[] args) throws Exception {
TwoDimensionalArrays arrMethods = new TwoDimensionalArrays();
System.out.println("-----");
System.out.println("2D Array Methods\n");
// countVals
int[][] numGrid = {{3, 2}, {4, 2}, {3, 3}};
int numSeek = 2;
int actualCount = arrMethods.countVals(numGrid, numSeek);
int expectedCount = 2;
if (actualCount != expectedCount) {
throw new Exception("countVals does not work");
}
System.out.println("countVals works!");
String[][] strGrid = {{"random", "seeker"}, {"21123", "seeker"}, {"testing", "seeker"}};
String strSeek = "seeker";
// findTopLeftLoc
int[] actualTopLeftLoc = arrMethods.findTopLeftLoc(strGrid, strSeek);
int[] expTopLeftLoc = {0, 1};
if (!Arrays.equals(actualTopLeftLoc, expTopLeftLoc)) {
throw new Exception("findTopLeftLoc does not work");
}
System.out.println("findTopLeftLoc works!");
// findBottomRightLoc
int[] actualBottomRightLoc = arrMethods.findBottomRightLoc(strGrid, strSeek);
int[] expBottomRightLoc = {2, 1};
if (!Arrays.equals(actualBottomRightLoc, expBottomRightLoc)) {
throw new Exception("findBottomRightLoc does not work");
}
System.out.println("findBottomRightLoc works!");
System.out.println("-----");
}
/**
* The method countVals returns the number of occurrences of 'seek' in 'grid'
*
* @param grid The 2D Array
* @param seek The int to find
* @return The number of occurrences of 'seek' in 'grid'
*/
public int countVals(int[][] grid, int seek) {
int sum = 0;
for (int r = 0; r < grid.length; r++) {
for (int c : grid[r]) {
if (c == seek) sum++;
}
}
return sum;
}
/**
* The method findTopLeftLoc returns the location of 'seek' in 'grid'.
* If 'seek' is found more than once, the location of 'seek' that is closest to the top left corner is returned.
*
* @param grid The 2D Array
* @param seek The String to find
* @return An int array of length 2, where the first index stores the row and the second index stores the col.
*/
public int[] findTopLeftLoc(String[][] grid, String seek) {
return findLocation(grid, seek, true);
}
/**
* The method findBottomRightLoc returns the location of 'seek' int the specified 2D array.
* If 'seek' is found more than once, the location of 'seek' that is closest to the bottom right corner is returned.
*
* @param grid The 2D Array
* @param seek The String to find
* @return An int array of length 2, where the first index stores the row and the second index stores the col.
*/
public int[] findBottomRightLoc(String[][] grid, String seek) {
return findLocation(grid, seek, false);
}
public int[] findLocation(String[][] grid, String seek, boolean topLeft) {
int[] location = new int[0];
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
String current = grid[r][c];
if (current.equals(seek)) {
if (location.length == 0) location = new int[]{r, c};
else {
int currentSum = r + c;
int locSum = location[0] + location[1];
if (topLeft && currentSum < locSum) location = new int[]{r, c};
else if (!topLeft && currentSum > locSum) location = new int[]{r, c};
}
}
}
}
return location;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment