4. The SensorSystem class is used to monitor a network of sensors arranged in a grid. A partial declaration of the Sensor class is shown below.
public class Sensor
{
/**
* Returns the current integer reading of the sensor
*/
public int getReading()
{ /* implementation not shown */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}public class SensorSystem
{
private Sensor[][] sensors;
/**
* Returns the index of the row containing the most sensors
* with a reading greater than the parameter threshold.
* If multiple rows have the same maximum number of such sensors,
* the method returns the smallest index among those rows.
*
* Preconditions: sensors is not null and no elements
* of sensors are null.
* sensors has at least one row and at
* least one column.
*/
public int rowWithMostAlerts(int threshold)
{ /* to be implemented */}
/* There may be instance variables, constructors, and methods
that are not shown. */
}When an element of the two-dimensional array sensors is accessed, the first index is used to specify the row and the second index is used to specify the column.
Write the SensorSystem method rowWithMostAlerts. The method should return the index of the row in the sensors array that contains the greatest number of sensors with a reading strictly greater than the parameter threshold. In the event of a tie, return the smallest row index.
Suppose sensors has the following contents. The value shown in each cell is the result of calling getReading() on the Sensor object at that position.
Row | 0 | 1 | 2 |
|---|---|---|---|
0 | 10 | 15 | 12 |
1 | 55 | 60 | 20 |
2 | 80 | 90 | 75 |
3 | 50 | 50 | 10 |
Call: rowWithMostAlerts(40)
Expected Return: should return 2
Explanation: because row 2 has 3 values greater than 40 (80, 90, 75), which is more than any other row.
Call: rowWithMostAlerts(100)
Expected Return: should return 0
Explanation: because no values in the grid exceed 100, so all rows have 0 alerts. Row 0 is returned as it is the first row with the maximum count (0).
Call: rowWithMostAlerts(18)
Expected Return: should return 1
Explanation: because row 1 and row 2 both have 3 values greater than 18. Since 1 is the smaller index, 1 is returned.
Complete method rowWithMostAlerts.
/**
* Returns the index of the row containing the most sensors
* with a reading greater than the parameter threshold.
* If multiple rows have the same maximum number of such sensors,
* the method returns the smallest index among those rows.
*
* Preconditions: sensors is not null and no elements
* of sensors are null.
* sensors has at least one row and at
* least one column.
*/
public int rowWithMostAlerts(int threshold)00:00
public class Sensor
{
/**
* Returns the current integer reading of the sensor
*/
public int getReading()
{ /* implementation not shown */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}public class SensorSystem
{
private Sensor[][] sensors;
/**
* Returns the index of the row containing the most sensors
* with a reading greater than the parameter threshold.
* If multiple rows have the same maximum number of such sensors,
* the method returns the smallest index among those rows.
*
* Preconditions: sensors is not null and no elements
* of sensors are null.
* sensors has at least one row and at
* least one column.
*/
public int rowWithMostAlerts(int threshold)
{ /* to be implemented */}
/* There may be instance variables, constructors, and methods
that are not shown. */
}/**
* Returns the index of the row containing the most sensors
* with a reading greater than the parameter threshold.
* If multiple rows have the same maximum number of such sensors,
* the method returns the smallest index among those rows.
*
* Preconditions: sensors is not null and no elements
* of sensors are null.
* sensors has at least one row and at
* least one column.
*/
public int rowWithMostAlerts(int threshold)