Question: Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found

Java

1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetLargest() and the call to it in the main method of IntArrayWorkerTester. 3. Write a getColTotal method in the IntArrayWorker class that returns the total of all integers in a specified column. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetColTotal() and the call to it in the main method of IntArrayWorkerTester.

IntArrayWorkerTester

public class IntArrayWorkerTester { /** method to test setMatrix */ public static void testSetMatrix() { IntArrayWorker worker = new IntArrayWorker(); int[][] nums = {{1, 1, 1} ,{2,2,2}}; worker.setMatrix(nums); System.out.println("This should have all 1's in first row and all 2's in second"); worker.print(); } /** Method to test fillPattern1 */ public static void testFillPattern1() { IntArrayWorker worker = new IntArrayWorker(); int[][] nums = new int[3][4]; worker.setMatrix(nums); worker.fillPattern1(); System.out.println("fills with 2's on diagonal, 3's to left, and 1's to right"); worker.print(); } /** Method to test getCount*/ public static void testGetCount() { IntArrayWorker worker = new IntArrayWorker(); int[][] nums = new int[3][4]; worker.setMatrix(nums); worker.fillPattern1(); int count = worker.getCount(1); System.out.println("Count should be 6 and count is " + count); } /** Method to test getTotal */ public static void testGetTotal() { IntArrayWorker worker = new IntArrayWorker(); int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; worker.setMatrix(nums2); int total = worker.getTotal(); System.out.println("Total should be 21 and is " + total); } /** Method to test getTotalNested */ public static void testGetTotalNested() { IntArrayWorker worker = new IntArrayWorker(); int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; worker.setMatrix(nums2); int total = worker.getTotalNested(); System.out.println("Total should be 21 and is " + total); } /** Method to test getLargest */ // public static void testGetLargest() // { // test when largest is last // IntArrayWorker worker = new IntArrayWorker(); // int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; // worker.setMatrix(nums2); // int largest = worker.getLargest(); // System.out.println("Largest should be 6 and is " + largest); // // test when largest is first // int[][] nums3 = {{6, 2, 3}, {4, 5, 1}}; // worker.setMatrix(nums3); // largest = worker.getLargest(); // System.out.println("Largest should be 6 and is " + largest); // // test when largest is in the middle // int[][] nums4 = {{1, 2, 3}, {6, 5, 1}}; // worker.setMatrix(nums4); // largest = worker.getLargest(); // System.out.println("Largest should be 6 and is " + largest); // // test when duplicate largest // int[][] nums5 = {{6, 2, 6}, {4, 5, 1}}; // worker.setMatrix(nums5); // largest = worker.getLargest(); // System.out.println("Largest should be 6 and is " + largest); // } /** Method to test getColTotal */ // public static void testGetColTotal() // { // IntArrayWorker worker = new IntArrayWorker(); // int [][] nums2 = {{1, 2, 3}, {4, 5, 6}}; // worker.setMatrix(nums2); // int total = worker.getColTotal(0); // System.out.println("Total for column 0 should be 5 and is " + total); // total = worker.getColTotal(1); // System.out.println("Total for column 1 should be 7 and is " + total); // total = worker.getColTotal(2); // System.out.println("Total for column 2 should be 9 and is " + total); // } public static void main(String[] args) { testSetMatrix(); testFillPattern1(); testGetCount(); testGetTotal(); testGetTotalNested(); //testGetLargest(); //testGetColTotal(); } }

IntArrayWorker

public class IntArrayWorker { /** two dimensional matrix */ private int[][] matrix = null; /** set the matrix to the passed one * @param theMatrix the one to use */ public void setMatrix(int[][] theMatrix) { matrix = theMatrix; } /** * Method to return the total * @return the total of the values in the array */ public int getTotal() { int total = 0; for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[0].length; col++) { total = total + matrix[row][col]; } } return total; } /** * Method to return the total using a nested for-each loop * @return the total of the values in the array */ public int getTotalNested() { int total = 0; for (int[] rowArray : matrix) { for (int item : rowArray) { total = total + item; } } return total; } /** * Method to fill with an increasing count */ public void fillCount() { int numCols = matrix[0].length; int count = 1; for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < numCols; col++) { matrix[row][col] = count; count++; } } } /** * print the values in the array in rows and columns */ public void print() { for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[0].length; col++) { System.out.print( matrix[row][col] + " " ); } System.out.println(); } System.out.println(); } /** * fill the array with a pattern */ public void fillPattern1() { for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[0].length; col++) { if (row < col) matrix[row][col] = 1; else if (row == col) matrix[row][col] = 2; else matrix[row][col] = 3; } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!