In: Computer Science
Checker |
+DisplayCount(int [], int):void |
The code below is a partial implementation of the class diagram
above. Note the similarities between
the class diagram and its implementation.
class Checker
{
/*
* Method DisplayCount
* This method should display the numbers of
times
* a given number (x) is found in a given array
(numbers)
*
* Provide the implementation (method body) for
DisplayCount
*/
public void DisplayCount(int[] numbers, int x)
{
}
}
I need to complete the implementation of method DisplayCount as described in the comments above, and I need to write a test class that uses class Checker and its DisplayCount method.
Assuming an array like this:
7 | 5 | 9 | 0 | 1 | 2 | 3 | 2 | 7 | 9 |
If the value of x is 10, the output should be:
10 is in the array 0 times
If the value of x is 7, the output should be:
7 is in the array 2 times
//Java code
class Checker { public void DisplayCount(int[] numbers, int x) { int count =0; for (int i = 0; i <numbers.length ; i++) { if(x==numbers[i]) count++; } System.out.println(x+" is in the array "+count+" times"); } }
// Test Class
public class Main { public static void main(String[] args) { int[] array = {7,5,9,0,1,2,3,2,7,9}; Checker checker= new Checker(); checker.DisplayCount(array,7); } }
//Output
//This code in c#
using System;
class Checker
{
public void DisplayCount(int[] numbers, int x)
{
int count = 0;
for (int i = 0; i < numbers.Length; i++)
{
if (x == numbers[i])
count++;
}
Console.WriteLine(x + " is in the array " + count + "
times");
}
}
class Program
{
static void Main(string[] args)
{
int[] array = { 7, 5, 9, 0, 1, 2, 3, 2, 7, 9 };
Checker checker = new Checker();
checker.DisplayCount(array, 7);
//pause
Console.ReadLine();
}
}
//Output
//If you need any help regarding this solution ........... please leave a comment ........ thanks
"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" ... 7 is in the array 2 times Process finished with exit code 0