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

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 3” 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

final variable

2

Constructor

3

Object state vs behavior

4

Why a static method cannot use instance variables

5

Writing a class from a specification

6

toString

7

Precondition

8

Local variable

9

Object reference aliasing

10

Instance variable default values

11

Immutability by design

12

Class definition

Short answer 1. Define or explain: Encapsulation

3 pts

Short answer 2. Define or explain: Default constructor

3 pts

Short answer 3. Define or explain: Accessor (getter)

3 pts

Short answer 4. Define or explain: Scope

3 pts

Free response

9 pts

CLASS DESIGN (Question 2, 9 points). Write the complete Bottle class, representing a bottle of liquid. The constructor takes a double capacity in milliliters (≥ 0); a newly constructed bottle is filled to capacity. public double updateAmount(double used) — removes used mL from the bottle (precondition: 0 < used ≤ amount remaining). If the removal would leave the bottle below 25% of its capacity, the bottle is immediately refilled to capacity. The method returns the amount remaining after the update. Examples: a 1000 mL bottle after updateAmount(400.0) returns 600.0; then updateAmount(100.0) returns 500.0; then updateAmount(300.0) leaves 200 mL — below 250 mL (25% of 1000) — so the bottle refills and 1000.0 is returned. A 40 mL bottle after updateAmount(30.0) returns 10.0 (exactly 25% — not below — so no refill); then updateAmount(1.0) leaves 9 mL < 10 mL, so it refills and returns 40.0.

Write the complete Bottle class: instance variables, constructor, and updateAmount, meeting all specifications and conforming to the examples.