Unit 3: Class Creation
CS A · Unit 3 · Paper 1

Class Creation unit test

A test on this unit alone, marked as a percentage and a letter grade — for the test your class is actually sitting, rather than for May. Answer everything, then submit once: seeing the answer to question 3 before attempting question 4 makes the final percentage meaningless.

Each paper is built from this unit’s 32 terms and is the same for everyone, so a teacher can assign “Unit 3, Paper 1” and every student sits the identical test. Multiple choice is marked objectively; the written sections you mark yourself against the model answer and rubric.
Suggested time 36 min 33 points0/17 attempted
1

toString

2

Immutability by design

3

Class definition

4

static method

5

Mutator (setter)

6

Instance variable default values

7

Accessor (getter)

8

Encapsulation

9

Default toString

10

Precondition

11

Constructor

12

Object reference aliasing

Short answer 1. Define or explain: equals for your own class

3 pts

Short answer 2. Define or explain: Writing a class from a specification

3 pts

Short answer 3. Define or explain: Return type of a constructor

3 pts

Short answer 4. Define or explain: Calling one constructor from another

3 pts

Free response

9 pts

CLASS (Question 2, 9 points). Write the complete Thermostat class, which models a heating thermostat. The class must satisfy the following specification. A Thermostat object keeps track of a target temperature, the most recent temperature reading, and whether the heater is currently running. It has the following public members: Thermostat(int target) — constructs a thermostat with the given target temperature, an initial reading equal to the target, and the heater off. void setTarget(int target) — changes the target temperature. The heater's state is not changed by this method. void update(int reading) — records reading as the most recent temperature. If the reading is more than 1 degree below the target, the heater turns on. If the reading is more than 1 degree above the target, the heater turns off. Otherwise the heater's state is unchanged. (This one-degree band prevents the heater from switching on and off repeatedly near the target.) boolean isHeating() — returns true if the heater is currently running. int getReading() — returns the most recent temperature reading. For example, with a Thermostat constructed at target 68: update(70) leaves the heater off; update(66) turns it on; update(67) leaves it on (67 is within one degree of 68); update(70) turns it off.

A. Write the complete Thermostat class, including all instance variables, the constructor, and all methods.