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 33 min 30 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

6 pts

This course has no free-response prompt tagged to this unit, so one from elsewhere in the course is used. It is still worth writing — the skill transfers.

Design a class Coupon that represents a store coupon. A Coupon is constructed with a description (String) and a discount percent (int from 0 to 100). Values below 0 are stored as 0 and values above 100 are stored as 100. The class must provide: public Coupon(String desc, int percent) public int getPercent() public double priceAfter(double price) — the price after applying the discount public boolean betterThan(Coupon other) — true if this coupon’s percent is strictly greater public String toString() — for example "20% off winter boots" Write the complete class, including instance variables.