CSE Roadmap

Core tier · about 2 hours · 7 parts

Part 7 of 7 · 2 min read

Exercises

Easy

Easy 1: Two objects (20 minutes)

Write a Car class with a plate number and a colour. Make two cars and print whether they are == and whether they are equals.

Done when: two printed lines and one sentence about the default equals.

Hint

Two separate new calls make two objects.

Easy 2: Class or object (20 minutes)

Sort into class or object: String, the string "hello", Room, room R7, List, a list with three names.

Done when: a six-row table.

Hint

Ask whether the thing is a definition or an instance.

Easy 3: Entity or value (20 minutes)

Classify as entity or value: order, price, customer, date, address, quantity.

Done when: six answers with a reason.

Hint

Ask whether two with the same data are still two things.

Medium

Medium 1: Break the contract (30 minutes)

Write an equals that is not symmetric (for example, one that accepts a String as equal to your class). Show a case where a.equals(b) is true and b.equals(a) is false.

Done when: a runnable example that prints both results.

Hint

Compare in one direction with a String and in the other with your class.

Medium 2: Missing hashCode (30 minutes)

Remove hashCode from Money. Put one Money(500) in a HashSet and check contains(new Money(500)). Restore the method and repeat.

Done when: two printed results and an explanation.

Hint

The hash code decides which bucket is searched.

Medium 3: Record version (30 minutes)

Rewrite Money as a record. Check that equality and toString still work.

Done when: the same three checks pass with fewer lines.

Hint

A record's equals uses all its components.

Hard

Hard 1: Entity equality (45 minutes)

Write a Booking entity with an id and a status. Implement equals by id only. Show that a set still finds it after the status changes.

Done when: code and a short comparison with an all-fields version.

Hint

Use the id in equals and hashCode, and not the status.

Hard 2: Value or entity design (45 minutes)

For an online store, decide for Product, Price, CartItem and Address whether each is an entity or a value, and state the equals rule.

Done when: a four-row table with a rule each.

Hint

Prices and addresses can be values, orders and customers are entities.

Hard 3: Transitivity trap (50 minutes)

Read EJ Item 10's Point and ColorPoint discussion. Explain in your own words why a subclass that adds a field cannot keep equals symmetric and transitive.

Done when: a half-page explanation with an example.

Hint

A colour-aware comparison and a plain comparison disagree.