[go: up one dir, main page]

0% found this document useful (0 votes)
311 views7 pages

CSA Unit 6 TemperatureAction FRQ

The document describes a smart thermostat that schedules temperature changes using a ScheduleManager class. It stores TemperatureAction objects in an ArrayList, representing the time, temperature, and whether to heat or cool. Part (a) asks to implement a method to add a TemperatureAction to the schedule. Part (b) asks to implement a method to remove TemperatureActions based on temperature and mode.

Uploaded by

Kalpita Acharya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
311 views7 pages

CSA Unit 6 TemperatureAction FRQ

The document describes a smart thermostat that schedules temperature changes using a ScheduleManager class. It stores TemperatureAction objects in an ArrayList, representing the time, temperature, and whether to heat or cool. Part (a) asks to implement a method to add a TemperatureAction to the schedule. Part (b) asks to implement a method to remove TemperatureActions based on temperature and mode.

Uploaded by

Kalpita Acharya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CSA Unit 6 FRQ

Name(s) __________________________________________________ Period _______ Date _____________________

Activity Guide - 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.

The TemperatureAction class is shown below.

public class TemperatureAction {


/* Constructs a TemperatureAction object with the hour, the minute, the temperature
* value in degrees, and the desired mode ("heat" or "cool").
* Precondition: Hours are in the range 0-23, minutes are in the range 0-59.
*/
public TemperatureAction(int hour, int minute, double degrees, String mode)
{ /* implementation not shown */ }

/* Returns the hour portion of the time */


public int getHour()
{ /* implementation not shown */ }

/* Returns the minute portion of the time */


public int getMinute()
{ /* implementation not shown */ }

/* Returns the temperature value */


public double getTemperature()
{ /* implementation not shown */ }

/* Returns the mode of the action */


public String getMode()
{ / * implementation not shown * / }

// 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.

public class ScheduleManager {


private ArrayList<TemperatureAction> schedule;

/* Adds a new temperature change to the schedule list.


*/
public void addTemperatureAction(TemperatureAction setting)
{ /* to be implemented in part (a) */ }

/* 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.

The TemperatureAction object newSetting:

8
45
80
"heat"

The ArrayList schedule before the method call addTemperatureAction(newSetting):

8 9
00 00
75 70
"heat" "cool"

The ArrayList schedule after the method call addTemperatureAction(newSetting):

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.

The TemperatureAction object newSetting:

9
15
80
"heat"

The ArrayList schedule before the method call addTemperatureAction(newSetting):

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"

Complete the method addTemperatureAction below.

/** Adds a new temperature change to the schedule list.


*/
public void addTemperatureAction(TemperatureAction setting)

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".

Complete the method removeTemperatureAction below.

/** Removes a TemperatureAction based on degrees and mode.


* Precondition: mode is "heat" or "cool"
*/
public boolean removeTemperatureAction(double temperature, String mode)

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.

Begin your response to each question at the top of a new page.

Use a pencil only. Do NOT write outside the box.


6
FRQ Response
Important: Completely fill in the circle that corresponds to the question that Part (a) Part (b)
you are answering on this page.

Begin your response to each question at the top of a new page.

Use a pencil only. Do NOT write outside the box.


7

You might also like