O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

The higher the temperature, the quicker the road dries out, and the traction coefficient is getting higher. To account for this, we can add a temperature property to enum RoadCondition and override the getTraction() method for the WET element, for example:

enum RoadCondition {  public int temperature;  DRY(1.0),  WET(0.2) {    public double getTraction(){       RoadCondition return temperature > 60 ? 0.4 : 0.2     }   },  SNOW(0.04);  private double traction;  RoadCondition(double traction){this.traction = traction; }  public double getTraction(){ return this.traction; }}

Now we can set the temperature property on RoadCondition before the speed calculation and get a different value of speed for the wet road condition. Add this line before calling ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required