CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 7 of 7 · 2 min read

Exercises

Easy

Easy 1: Is-a or has-a (20 minutes)

Decide is-a or has-a for: car and engine, circle and shape, team and player, square and rectangle.

Done when: four answers with a reason.

Hint

Say the sentence "a B is an A".

Easy 2: Run the failure (20 minutes)

Compile the inheritance CountingSet and print the count after adding three items with addAll.

Done when: a printed 6 and a one-line reason.

Hint

The parent's addAll calls the overridden add.

Easy 3: Name the reuse (20 minutes)

Classify as white-box or black-box: a class that extends ArrayList, a class with a List field, a class that implements Runnable.

Done when: three answers.

Hint

Ask whether internals of the parent are visible.

Medium

Medium 1: Run the fix (30 minutes)

Compile the composition CountingSet. Add three items with addAll and print the count. Then wrap a TreeSet instead.

Done when: two printed 3s.

Hint

The inner set's addAll calls the inner add.

Medium 2: Logging wrapper (30 minutes)

Write a LoggingList<E> that wraps a List<E> and prints a message on every add. Forward the rest.

Done when: code that uses it where a List is expected.

Hint

Wrap, print, then forward.

Medium 3: Window and shape (30 minutes)

Write Window holding a Shape interface with area(). Swap a rectangle for a circle at run time.

Done when: two printed areas from one window object.

Hint

Replace the held shape with a setter or a new window.

Hard

Hard 1: Fix a stack (45 minutes)

Write a Stack that extends ArrayList and show a call that breaks its rules. Rewrite it by composition so that only push, pop and peek are visible.

Done when: a broken version and a safe version.

Hint

ArrayList has add(index, e), which a stack should not allow.

Hard 2: Reduce the forwarding (45 minutes)

Write a reusable ForwardingList<E> that forwards every method, then write LoggingList<E> by extending only your forwarding class.

Done when: a forwarding class and a three-method wrapper.

Hint

Put all forwarding in the base wrapper.

Hard 3: Design for extension (50 minutes)

Read EJ Item 19 on designing and documenting for inheritance. Write the documentation comment for a class method that calls another overridable method.

Done when: a method with a comment that states its self-use.

Hint

Say which methods the method calls and whether they can be overridden.