4. The Item class is used to represent products available in a store. A partial declaration of the Item class is shown.
public class Item
{
/**
* Returns the category of the item (e.g., "food", "electronics")
*/
public String getCategory()
{ /* implementation not shown */ }
/**
* Returns the price of the item
*/
public double getPrice()
{ /* implementation not shown */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}public class StoreStock
{
private Item[][] items;
/**
* Returns the index of the row containing the most
* items with the category indicated by the parameter
* category.
* Preconditions: items is not null and no elements
* of items are null.
* items has at least one row and at
* least one column.
*/
public int rowWithMost(String category)
{ /* to be implemented */}
/* There may be instance variables, constructors, and methods
that are not shown. */
}When an element of the two-dimensional array items is accessed, the first index is used to specify the row and the second index is used to specify the column.
Write the StoreStock method rowWithMost. The method should return the index of a row in items that contains the maximum number of occurrences of the parameter category. If there are multiple rows that have the maximum number of occurrences, any of their row indices can be returned.
Suppose items has the following contents. For each element, the first value is the category and the second value is the price.
Row | 0 | 1 | 2 |
|---|---|---|---|
0 | "tech" 50.0 | "tech" 20.0 | "home" 15.0 |
1 | "home" 10.0 | "home" 10.0 | "home" 12.0 |
2 | "food" 5.0 | "tech" 100.0 | "food" 3.0 |
3 | "tech" 80.0 | "home" 25.0 | "tech" 45.0 |
Call: rowWithMost("tech")
Expected Return: should return either 0 or 3
Explanation: because "tech" appears two times in row 0, two times in row 3, and fewer times in the other rows.
Call: rowWithMost("home")
Expected Return: should return 1
Explanation: because "home" appears three times in row 1 and fewer times in each of the other rows.
Call: rowWithMost("food")
Expected Return: should return 2
Explanation: because "food" appears two times in row 2 and zero times in the other rows.
Complete method rowWithMost.
/**
* Returns the index of the row containing the most
* items with the category indicated by the parameter
* category.
* Preconditions: items is not null and no elements
* of items are null.
* items has at least one row and at
* least one column.
*/
public int rowWithMost(String category)00:00