Question 1141
This question involves analyzing daily high temperatures recorded over a season. The partial class TempLog below stores the temperatures in a double array.
public class TempLog
{
/** highs[k] is the recorded high temperature (in degrees Fahrenheit)
* for day k, where 0 <= k < highs.length. highs is never null
* and always contains at least one element. */
private double[] highs;
// Constructor not shown.
/** Returns the number of days whose high temperature was
* strictly greater than threshold, as described in part (a). */
public int daysHotterThan(double threshold)
{
/* to be implemented in part (a) */
}
/** Returns the length of the longest run of consecutive days
* whose high temperature was strictly greater than threshold,
* as described in part (b). */
public int longestHotStreak(double threshold)
{
/* to be implemented in part (b) */
}
}
Part (a)
Write the daysHotterThan method. The method returns the number of entries in highs that are strictly greater than threshold.
For example, if highs contains {72.5, 80.0, 68.1, 95.4, 80.0, 81.2}, then:
| Call | Returned value |
|---|---|
daysHotterThan(80.0) | 2 (only 95.4 and 81.2 are strictly greater) |
daysHotterThan(100.0) | 0 |
daysHotterThan(0.0) | 6 |
Complete method daysHotterThan below.
/** Returns the number of days whose high temperature was
* strictly greater than threshold, as described in part (a). */
public int daysHotterThan(double threshold)
Part (b)
Write the longestHotStreak method. The method returns the length of the longest run of consecutive days whose high temperature is strictly greater than threshold. If no day satisfies the condition, the method returns 0.
For example, if highs contains {60.0, 85.0, 90.0, 72.0, 95.0, 95.0, 95.0, 70.0}, then:
| Call | Returned value |
|---|---|
longestHotStreak(80.0) | 3 (days at indices 4, 5, 6) |
longestHotStreak(88.0) | 3 |
longestHotStreak(100.0) | 0 |
In writing longestHotStreak, you may assume daysHotterThan works as specified regardless of what you wrote in part (a). Complete method longestHotStreak below.
/** Returns the length of the longest run of consecutive days
* whose high temperature was strictly greater than threshold,
* as described in part (b). */
public int longestHotStreak(double threshold)
Main Solution
Sample Solution
Part (a) — Solution
public int daysHotterThan(double threshold)
{
int count = 0;
for (int i = 0; i < highs.length; i++)
{
if (highs[i] > threshold)
{
count++;
}
}
return count;
}
Scoring (4 pts): +1 correctly traverses highs (bounds correct); +1 uses strict > comparison; +1 increments a counter on match; +1 returns count after loop.
Part (b) — Solution
public int longestHotStreak(double threshold)
{
int longest = 0;
int current = 0;
for (int i = 0; i < highs.length; i++)
{
if (highs[i] > threshold)
{
current++;
if (current > longest)
{
longest = current;
}
}
else
{
current = 0;
}
}
return longest;
}
Scoring (5 pts): +1 correctly traverses highs; +1 increments a running streak on a hot day; +1 resets streak to 0 on a non-hot day; +1 updates maximum correctly; +1 returns the maximum and handles the all-cold case (returns 0). Common errors: (i) forgetting to reset current in the else; (ii) updating longest only at the end, which misses streaks that don't end on the last day (this solution avoids both).
Teacher Explanation
Education Coach Advice
Traps
Answer images

