Question

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 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");
}
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Java Program For Restaurants
Homework Solution
$18.00
Java
Restaurants
Computer Science
Programming
Tips
Variables
Functions
Dollar Amounts
Functions
Input
Output
Statements
Multiplication
Bills
Percentage
Division
NumberFormat Class
Railroads Problem
Homework Solution
$100.00
Java Programming
Computer Science
Graph
Input
Arrival
Output
Railroad
Scenario
Location
Class Money in Java
Homework Solution
$20.00
Java
Programming
Coding
Money
UML Diagram
Computer Science
Dollars
Cents
Parameters
Statements
Methods
Strings
Integers
Finance
Arithmetic Operations
Java Programming Assignment
Homework Solution
$20.00
Java Test Application
Java Programming
Program
3-D Geometric Shape
Java Class
Constructor
Get help from a qualified tutor
Live Chats