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.
final variable
Constructor
Object state vs behavior
Why a static method cannot use instance variables
Writing a class from a specification
toString
Precondition
Local variable
Object reference aliasing
Instance variable default values
Immutability by design
Class definition
Short answer 1. Define or explain: Encapsulation
3 ptsShort answer 2. Define or explain: Default constructor
3 ptsShort answer 3. Define or explain: Accessor (getter)
3 ptsShort answer 4. Define or explain: Scope
3 ptsFree response
6 ptsThis 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.