Question

Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

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;
}
}
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

public int getCount(int passed){
int count = 0;
for(int row = 0; row < matrix.length; row++){
for(int col = 0; col < matrix[0].length; col++){
{if(matrix[row][col] == passed)
count++;
}}
return count;}

HomeworkLib allows to answer only one question

Add a comment
Know the answer?
Add Answer to:
Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...

    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...

    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; /**...

    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...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    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...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    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...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • How do I separate the method into different class? I try separate the testing and method...

    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)...

    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{...

  • JAVA Write a method that returns the sum of all the elements in a specified column...

    JAVA Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public static double sumColumn(double [][] m, int columnIndex) Write another method that returns the sum of all the elements in a specified row in a matrix using the following header: public static double sumRow( double [][] m, int rowIndex) Write a test program that reads a 3-by-4 matrix and displays the sum of each column and sum...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT