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. Create the Engine class as an inner class of the Vehicle class:
        public class Vehicle {          private int weightPounds;          private Engine engine;          public Vehicle(int weightPounds, int horsePower) {            this.weightPounds = weightPounds;            this.engine = new Engine(horsePower);          }          public double getSpeedMph(double timeSec){            return this.engine.getSpeedMph(timeSec);          }          private int getWeightPounds(){ return weightPounds; }          private class Engine {            private int horsePower;            private Engine(int horsePower) {              this.horsePower = horsePower;            }            private double getSpeedMph(double timeSec){              double v = 2.0 * this.horsePower * 746;              v = v * timeSec * 32.174 / getWeightPounds();              return Math.round(Math.sqrt(v) * 0.68);            }          }        }
  1. Notice that the getSpeedMph() method of ...

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