Question

In: Computer Science

FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...

FOR JAVA

Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array.

For example, if the array (as created by the program below) is

10 45 3 8

2 42

3 21 44

And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array)

public class HomeworkA {

public static void main(String args[]){

int arr[][] = {{10, 45, 3, 8}, {2, 42}, {3, 21, 44}};

System.out.println(“The number time 3 appears is “+findNums(arr,3)); }

public static int findNum (int [][] myArray) {

..........

Solutions

Expert Solution

import java.util.*;
import java.io.*;
public class Occurrence
   {
    public static void main(String[] args)
        {
int[][] arr={{10, 45, 3,8 },{ 2,42 },{3,21,44}};
//2D array arr, initialized with the above elements
System.out.println("The number of times 3 appearing is: " +findNums(arr,3));
//a print statement, which calls the findNums()
//it passes arr and counting number 3
//It prints the value returned by the method
     }//end of main()
       public static int findNums(int [][] myArray,int num)
    {
      int count=0;
      //count=0, to count the occurrence of the number
        for(int i = 0; i < myArray.length; i++)
          {
      //outer loop to set to length of the 2D array limit
      //That is the rows     
            for (int j = 0; j < myArray[i].length; j++)
            {
              //inner loop is set to columns limit
                if(myArray[i][j]==num)
               //checking each position element is match with the num value
               //here, the num value holds value 3
               //if it matches, just increment the count variable
                count++;
            }//end of loop
        }//end of outer loop
   
    return count;
    //return count value to main
}//enof method
}//end of class

Screenshot


Related Solutions

in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {   ...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user inputs) as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values {7, 2, 8, 9, 4, 15, 7, 1, 9, 10}, then the call of shrink(list) should return a new array containing {9, 17, 19, 8, 19}. The first pair from...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
Write a method that takes an array of integers as input. The method should find and...
Write a method that takes an array of integers as input. The method should find and display two indices m and n such that if you sort the elements from index m to index n the entire array would be sorted. The method should minimize m − n, that is it should find the smallest subsection for the array that needs to be sorted. For example, int[] a = {2, 4, 6, 7, 9, 8, 12, 15, 5, 13, 18,...
Using Java, write a program that takes in two integers from the keyboard called m and...
Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Java Write a valid Java method called printReverse that takes a String as a parameter and...
Java Write a valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String. Your method should not return anything. Make sure you use the appropriate return type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT