HERE IS THE ORIGINAL CODE:
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;
}
}
}
}
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();
}
}
ANSWER:-
CODE:-
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;
}
}
}
public int getColTotal(int col){
int total = 0;
for (int row = 0; row <
matrix.length; row++)
{
total +=
matrix[row][col];
}
return total;
}
public int getLargest(){
int largest = matrix[0][0];
for (int row = 0; row <
matrix.length; row++)
{
for (int col =
0; col < matrix[0].length;col++)
{
if(matrix[row][col] > largest){
largest =
matrix[row][col];
}
}
}
return largest;
}
public int getCount(int value){
int count = 0;
for (int row = 0; row <
matrix.length; row++)
{
for (int col =
0; col < matrix[0].length;col++)
{
if(matrix[row][col] == value){
count++;
}
}
}
return count;
}
}
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();
}
}
NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.
THANK YOU!!!
OUTPUT:-

Write a getCount method in the IntArrayWorker class that returns the count of the number of...
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...
Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()" public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...
This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...
cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...
JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...
How do I modify this to let the user input the size of the matrix? The lowest size is 4 and the largest size is 16. import java.util.*; public class Test1 { public static void main(String[] args) { int[][] matrix = new int[4][4]; // create 4 by 4 matrix (need user input???) // generate 1's and 0's for each each rows and columns // and track largest row index with the most ones int largestRI...
How do I separate the method into different class? I try
separate the testing and method class into 2 different class. And
when I test it, it said that the method is undefined. And it ask me
to create the method in the main class. I don't know what is the
problem.
This is the access to the code
https://repl.it/@Teptaikorn/test
Thank you very much
MazeSolver.java MazeTerster.... TwoDim AutoA... testfile.txt Main.java x > 0 N Binary TreeN... X LinkedBinary... Maze.java 1...
Need Help ASAP!! Below is my code and i am getting error in
(public interface stack) and in StackImplementation class. Please
help me fix it. Please provide a solution so i can fix the
error.
thank you....
package mazeGame;
import java.io.*;
import java.util.*;
public class mazeGame {
static String[][]maze;
public static void main(String[] args)
{
maze=new String[30][30];
maze=fillArray("mazefile.txt");
}
public static String[][]fillArray(String file)
{
maze = new String[30][30];
try{...
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...
Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...