Part 6 of 6 · 3 min read
Exercises
Easy
Easy 1: Predict the output (20 minutes)
Without running it, write what test prints for a Rectangle and for a Square using the code of section 2. Then run it.
Done when: your predictions and the real output (10 and 4) are written side by side.
Hint
The second call sets both sides to 2.
Easy 2: Spot the warning signs (20 minutes)
Write three short code snippets: one with instanceof, one with "not supported", one with a vendor name in an if. Label each with the sign it shows.
Done when: each snippet has a one-line label.
Hint
The warning sign is in the caller, not in the subclass.
Easy 3: Check the License (20 minutes)
Write License, PersonalLicense and BusinessLicense, with different calcFee() rules. Write a Billing method that takes a License.
Done when: Billing contains no instanceof and works for both subclasses.
Hint
Keep calcFee() as the only method the billing code calls.
Medium
Medium 1: Fix the closed room (45 minutes)
Redesign Room so that ClosedRoom no longer throws. Show a list of rooms processed by a loop with no try and no type check.
Done when: the program prints results for all rooms and ClosedRoom does not implement a method it cannot do.
Hint
Move book to a smaller interface.
Medium 2: A breaking subclass (45 minutes)
Write Account with withdraw(int amount) that works for any positive amount up to the balance. Add FixedDepositAccount that refuses withdrawals before a date. Explain which rule of section 4 it breaks and redesign.
Done when: the explanation names the rule, and the new design has no surprising subclass.
Hint
The parent allows any withdrawal up to the balance, and the subclass says no.
Medium 3: Equals with a new field (45 minutes)
Write Point(x, y) with equals. Write ColorPoint that extends it and adds a colour used in equals. Show that p.equals(cp) and cp.equals(p) give different answers.
Done when: you print two different booleans, and a second version using composition prints consistent answers.
Hint
Use two points with the same x and y but different colours.
Hard
Hard 1: Review a hierarchy (90 minutes)
Draw a hierarchy Vehicle, Car, Bicycle, ElectricScooter with methods refuel(), pedal(), charge(). Find every LSP violation and redesign the interfaces.
Done when: the redesign shows interfaces for each ability, and no class has a method that throws "not supported".
Hint
Start by listing what each vehicle can actually do.
Hard 2: Make the taxi dispatcher (90 minutes)
Build a Dispatcher that sends requests to two vendors. One vendor uses dest, the other destination. Use a table keyed by vendor, not an if on the name.
Done when: adding a third vendor is a new table entry, and the Dispatcher code has no vendor name.
Hint
A Map<String, Function<Request, String>> keeps the vendor code in data.
Hard 3: Write a contract test (90 minutes)
For Shape, write a test method that takes any Shape and checks the shared promises (non-negative area). Run it over three classes and add a broken one that the test catches.
Done when: the same test passes for good classes and fails for the broken one.
Hint
Give the broken class a negative area.