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

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

Accessor (getter)

2

Object state vs behavior

3

Class definition

4

Local variable

5

Mutator (setter)

6

toString

7

equals for your own class

8

Why instance variables are private

9

Default toString

10

Method decomposition

11

Scope

12

Accessing a private field of another object of the same class

Short answer 1. Define or explain: public vs private

3 pts

Short answer 2. Define or explain: final variable

3 pts

Short answer 3. Define or explain: Postcondition

3 pts

Short answer 4. Define or explain: Object reference aliasing

3 pts

Free response

9 pts

CLASS (Question 2, 9 points). Write the complete Coupon class, which models a discount coupon that may be used a limited number of times. The class must satisfy the following specification. Coupon(String code, double percentOff, int uses) — constructs a coupon with the given code, the given percentage off (a value from 0.0 to 100.0), and the given number of remaining uses. String getCode() — returns the coupon's code. boolean isUsedUp() — returns true if the coupon has no uses remaining. double applyTo(double price) — if the coupon has at least one use remaining, reduces the remaining uses by one and returns the discounted price; otherwise returns price unchanged. A coupon with percentOff of 25.0 applied to a price of 80.00 returns 60.00. String toString() — returns a string of the form "SAVE20 (20.0% off, 3 left)" using the coupon's code, percentage, and remaining uses. For example, a Coupon constructed with ("SAVE20", 20.0, 2) returns 80.0 from applyTo(100.0), then 40.0 from applyTo(50.0), and then 30.0 from a third call to applyTo(30.0), because the coupon is used up.

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