Skip to main content

Question 2: Class Design

Thermostat modes

  • Units 2 and 3
  • 7 points
  • About 25 minutes

You write a whole class from a description and a table of example calls and results: the class header, private instance variables, a constructor and the required method. New since the course was revised for 2025–26 (first tested in May 2026): it's worth 7 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 2 of 4, 7 points. Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.

The question and its sources

The Thermostat class, which you will write, decides whether a home's heater or air conditioner should run.

A Thermostat object is created with a double parameter, the target temperature in degrees.

The update method has a double parameter, the current temperature. It returns a String for the mode the thermostat chooses:

- "heat" if the current temperature is more than 2 degrees below the target;

- "cool" if the current temperature is more than 2 degrees above the target;

- "off" otherwise (including when it is exactly 2 degrees above or below the target).

The thermostat also counts how many times the heater has been *started*. The heater is started when update returns "heat" and the previous call to update did not return "heat" (or there was no previous call). The getHeatStarts method has no parameters and returns that count.

The table shows a sample sequence of statements and their results. The statements are in a class other than Thermostat.

Sample statements and results

StatementValue returned (— if none)Explanation
Thermostat t = new Thermostat(20.0);—The target is 20 degrees.
t.update(19.0)"off"19.0 is within 2 degrees of 20.0.
t.update(17.5)"heat"17.5 is more than 2 below 20.0. The heater is started (1 start).
t.update(17.9)"heat"Still more than 2 below. The heater was already on, so this is not a new start.
t.update(18.0)"off"Exactly 2 below the target, so the mode is off.
t.update(22.5)"cool"More than 2 above 20.0.
t.update(15.0)"heat"The heater is started again (2 starts).
t.getHeatStarts()2The heater was started twice.

Source: Hypothetical example

Suggested time: 25 minutes

Your answers are saved in this browser as you type.

Something wrong with this question?

What's wrong?

Please don't include personal details.

Part (a)

7 points

Write the complete Thermostat class. Your implementation must meet all specifications and conform to the examples shown in the table.

Tab indents. Press Esc, then Tab, to move on.

0 lines · 0 / 2,500 characters

Checking scoring…

Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.