March 2018
Intermediate to advanced
208 pages
4h 52m
English
| | class CruiseControl { |
| | static final double SPEED_OF_LIGHT_KMH = 1079252850; |
| | static final double SPEED_LIMIT = SPEED_OF_LIGHT_KMH; |
| | |
| | private double targetSpeedKmh; |
| | |
| | void setTargetSpeedKmh(double speedKmh) { |
| » | if (speedKmh < 0) { |
| | throw new IllegalArgumentException(); |
| | } else if (speedKmh <= SPEED_LIMIT) { |
| | targetSpeedKmh = speedKmh; |
| » | } else { |
| | throw new IllegalArgumentException(); |
| | } |
| | } |
| | } |
Let’s take a look at the method setTargetSpeedKmh(). The if conditional with its three branches surely does some exception handling. But do you immediately understand why it’s structured that way? Probably not.
The normal path in this method is a little bit concealed. It’s the second of the three ...