Thermostat
This single part question is worth 7 points.
AP CSA Alignment
| Unit Alignment |
Learning Objectives |
Skills |
| Unit 3: Class Creation |
3.3.A Develop code to designate access and visibility constraints to classes, data, constructors, and methods. |
2.B Write program code involving data abstractions. |
| Unit 3: Class Creation |
3.4.A Develop code to declare instance variables for the attributes to be initialized in the body of the constructors of a class. |
2.B Write program code involving data abstractions. |
| Unit 3: Class Creation |
3.5.A Develop code to define behaviors of an object through methods written in a class using primitive values and determine the result of calling these methods. |
2.C Write program code involving procedural abstraction. |
| Unit 2: Selection and Iteration |
2.9.A Develop code for standard and original algorithms (without data structures) and determine the result of these algorithms. |
2.A Write program code to implement an algorithm. |
Directions
The Thermostat class, which you will write, represents a thermostat which controls the temperature in a school. Thermostat objects are created by calls to a constructor with two parameters.
- The first parameter is an
int that represents the initial temperature the thermostat should be set to.
- The second parameter is an
int that represents the variability of the temperature above and below the initial temperature. For example, if the initial temperature is 70 and the variability is 3, the thermostat can be set to temperatures in the range of 67 to 73 inclusive.
The Thermostat class contains a adjust method, which will modify the current temperature by adding the value of the int parameter to the current temperature and returns true, as long as the new temperature remains in the allowable range based on the initial temperature and the variability. If the amount the current temperature is being adjust is outside of the range, false is returned. The value of the parameter could be positive or negative.
| Statement |
Return Value (blank if no value) |
Explanation |
boolean isChanged; |
|
Thermostat classroomA = new Thermostat(70, 2); |
|
Thermostat classroomA is constructed with the initial temperature set to 70 and the variability of the temperature being 2. |
isChanged = classroomA.adjust(2); |
true |
The current temperature is set to 72. |
isChanged = classroomA.adjust(-4); |
true |
The current temperature is set to 68. |
isChanged = classroomA.adjust(4); |
true |
The current temperature is set to 72. |
isChanged = classroomA.adjust(2); |
false |
The maximum value for the current temperature is 72 (70 + 2). The current temperature remains 72. |
isChanged = classroomA.adjust(-5); |
false |
The minimum value for the current temperature is 68 (70 - 2). The current temperature remains 72. |
Write Your Response
- In order to test your program, we have provided some test cases for calls to
adjust.
- Write the code for the class
Thermostat in the Java Playground above the provided test cases.
- All four test should return
true if your code is correct.