4. This question involves reasoning about a farm grid that is represented as a two-dimensional array of Crop objects. The Crop class is used to store information about a planted crop. A partial declaration of the Crop class is shown.
public class Crop
{
/**
* Returns the type of the crop ("corn", "wheat", etc.)
*/
public String getType()
{ /* implementation not shown */ }
/**
* Returns the expected yield of the crop in bushels
*/
public int getYield()
{ /* implementation not shown */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}public class Farm
{
private Crop[][] grid;
/**
* Returns the index of a row containing the highest total
* yield of the crop type indicated by the parameter targetType.
* Preconditions: grid is not null and no elements
* of grid are null.
* grid has at least one row and at
* least one column.
*/
public int rowWithHighestYield(String targetType)
{ /* to be implemented */}
/* There may be instance variables, constructors, and methods
that are not shown. */
}When an element of the two-dimensional array grid is accessed, the first index is used to specify the row and the second index is used to specify the column.
Write the Farm method rowWithHighestYield. The method should return the index of a row in grid that contains the highest total yield of the parameter targetType. To determine the total yield of a targetType in a row, add the yield of all Crop objects in that row whose type matches targetType. If there are multiple rows that have the highest total yield of targetType, any of their row indices can be returned.
Suppose grid has the following contents. For each element, the first value is the crop type and the second value is the yield.
Row | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
0 | "corn" 50 | "wheat" 40 | "corn" 30 | "soy" 20 |
1 | "wheat" 60 | "corn" 10 | "wheat" 50 | "wheat" 20 |
2 | "soy" 30 | "soy" 40 | "corn" 90 | "soy" 10 |
3 | "corn" 40 | "corn" 40 | "wheat" 10 | "soy" 30 |
Call: rowWithHighestYield("corn")
Expected Return: should return 2
Explanation: because the total yield for "corn" is 80 in row 0, 10 in row 1, 90 in row 2, and 80 in row 3. Row 2 has the highest total yield for "corn".
Call: rowWithHighestYield("wheat")
Expected Return: should return 1
Explanation: because the total yield for "wheat" is 40 in row 0, 130 in row 1, 0 in row 2, and 10 in row 3. Row 1 has the highest total yield for "wheat".
Call: rowWithHighestYield("soy")
Expected Return: should return 2
Explanation: because the total yield for "soy" is 20 in row 0, 0 in row 1, 80 in row 2, and 30 in row 3. Row 2 has the highest total yield for "soy".
Complete method rowWithHighestYield.
/**
* Returns the index of a row containing the highest total
* yield of the crop type indicated by the parameter targetType.
* Preconditions: grid is not null and no elements
* of grid are null.
* grid has at least one row and at
* least one column.
*/
public int rowWithHighestYield(String targetType)00:00
public class Crop
{
/**
* Returns the type of the crop ("corn", "wheat", etc.)
*/
public String getType()
{ /* implementation not shown */ }
/**
* Returns the expected yield of the crop in bushels
*/
public int getYield()
{ /* implementation not shown */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}public class Farm
{
private Crop[][] grid;
/**
* Returns the index of a row containing the highest total
* yield of the crop type indicated by the parameter targetType.
* Preconditions: grid is not null and no elements
* of grid are null.
* grid has at least one row and at
* least one column.
*/
public int rowWithHighestYield(String targetType)
{ /* to be implemented */}
/* There may be instance variables, constructors, and methods
that are not shown. */
}/**
* Returns the index of a row containing the highest total
* yield of the crop type indicated by the parameter targetType.
* Preconditions: grid is not null and no elements
* of grid are null.
* grid has at least one row and at
* least one column.
*/
public int rowWithHighestYield(String targetType)