QuestionQuestion

Create a generic method for counting the number of times that a given item occurs in an array. The method accepts an array and an item to find in the array. These are a generic data type. The method returns an integer. Here is an example of calling the method.

If wordList is a variable of type String[] and word is a variable of type String

int count = countOccurrences(wordList, word);

Test your method using an array of String and an array of Integer (not int, can’t use primitive types with generics).

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public static void main(String[] args)
{
String[] test = {"test1", "test2", "test1", "test3", "test4", "test5", "test1"};
int occCount = countOccurrences(test, "test1");

System.out.println("Test 1 occured " + occCount + " times in the array");

Integer[] test2 = {1, 4, 5, 24, 53, 23, 53, 24, 59, 35 ,24, 43, 53, 26, 24};
occCount = countOccurrences(test2, 53);

System.out.println("53 occured " + occCount + " times in the array");
}
}...
$20.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats