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

How to do it...

  1. Define the RoadCondition enumeration in the SpeedModel interface (because it is part of the SpeedModel API):
        public interface SpeedModel {          double getSpeedMph(double timeSec, int weightPounds,                             int horsePower);          enum RoadCondition {            DRY(1.0), WET(0.2), SNOW(0.04);            private double traction;            RoadCondition(double traction){ this.traction = traction;}            public double getTraction(){ return this.traction; }          }        }

There is no need to add the public keyword in front of enum RoadCondition because, in an interface, it defaults to public (if the private keyword is not used instead).

  1. Run the following loop:
        for(RoadCondition v: RoadCondition.values()){          System.out.println(v + " => " + v.getTraction());        }
  1. You'll get the following result: ...

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