For problems 22 and 23, write the Java code that is requested. Write a public an
ID: 3808592 • Letter: F
Question
For problems 22 and 23, write the Java code that is requested. Write a public and static Java method named count that takes an integer and an integer array as parameters, in that order, and returns an integer. The method counts the total number of occurrences of the specified integer in the array and returns the total. Write Java code to call the method defined above with the array called myArray, which is declared below, and the integer parameter set to the literal 14, and print the return value. After calling the method, print the array using Arrays.tostring(). You can assume that the required import statements have already been made.//Java coding int[] myArray = {7, 14, 35, 14, 123, 99, 14, 2, 7, 14, 55, 14};Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static int count(int [] array, int size, int value)
{
int cnt = 0;
for(int i = 0; i < size; i++)
{
if(value == array[i])
cnt++;
}
return cnt;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int [] myArray = { 7,14,35,14,123,99,14,2,7,14,55,14} ;
System.out.printf("Number 14 has occured %d times in the array ", count(myArray,12,14));
}
}
-------------------------------------------------------------------------------------------------------------------------------------
//output
Number 14 has occured 5 times in the array
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.