CSA Unit 6 TemperatureAction FRQ
CSA Unit 6 TemperatureAction FRQ
Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN
IN JAVA. You may plan your answers on a scratch piece of paper, but no credit will be given for anything
written on a scratch piece of paper. You will only earn credit for what you write on the response pages.
Notes:
● Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
● Unless otherwise noted in the question, assume that parameters in method calls are not null and that
methods are called only when their preconditions are satisfied.
● In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
1
A smart thermostat schedules temperature changes throughout the day and stores the information about the
change in a ScheduleManager object. Each temperature change is represented by an object called
TemperatureAction, which has the hours and minutes of when a change occurred, the new temperature to
set the thermostat, and whether the command is to heat or cool.
// There may be instance variables, constructors, and methods that are not shown.
}
The ScheduleManager class contains a list of the temperature changes scheduled by users within a given day.
The declaration of the ScheduleManager class is shown below.
/* Removes the TemperatureAction that has the temperature and mode specified.
* Precondition: mode is either "heat" or "cool"
*/
public boolean removeTemperatureAction(double temperature, String mode)
{ /* to be implemented in part (b) */ }
}
2
a. Write the ScheduleManager method addTemperatureAction, which takes a TemperatureAction object
as the parameter that represents the new temperature setting to be added to the schedule. The method
adds the new TemperatureAction object to the schedule instance variable. The schedule should store
temperature settings by time, with the earlier times stored first. If a time already exists in the schedule, it
should be overwritten.
EXAMPLE 1: If a setting already exists at hour 8, and another at hour 9, a new setting at hour 8 minutes
45 should be inserted between the two.
8
45
80
"heat"
8 9
00 00
75 70
"heat" "cool"
8 8 9
00 45 00
75 80 70
"heat" "heat" "cool"
EXAMPLE 2: If a setting already exists at hour 8, and another at hour 9, a new setting at hour 9 minutes
15 should be inserted after both settings.
9
15
80
"heat"
8 9
00 00
75 70
"heat" "cool"
3
The ArrayList schedule after the method call addTemperatureAction(newSetting):
8 9 9
00 00 15
75 70 80
"heat" "cool" "heat"
4
b. Write the ScheduleManager method removeTemperatureAction, which completes the following actions:
● Returns a boolean indicating whether any TemperatureAction objects were removed. The method
returns true if elements were removed and returns false if the list remains the same.
● Removes from schedule all TemperatureAction objects with a mode equal to the given mode and
which contain a temperature above the given value if the mode is "cool" or below a given value if the
mode is "heat".
5
FRQ Response
Important: Completely fill in the circle that corresponds to the question that Part (a) Part (b)
you are answering on this page.