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 an Engine class with the horsePower property, the setHorsePower() method that sets this property's value, and the getSpeedMph() method that calculates the speed of a vehicle, based on the time since the vehicle started moving, the vehicle weight, and the engine power:
        public class Engine {           private int horsePower;           public void setHorsePower(int horsePower) {             this.horsePower = horsePower;           }           public double getSpeedMph(double timeSec,                                     int weightPounds) {             double v = 2.0*this.horsePower*746;             v = v*timeSec*32.17/weightPounds;             return Math.round(Math.sqrt(v)*0.68);           }         }
  1. Create the Vehicle class: 
        public class Vehicle {           private int weightPounds;           private Engine engine;  public Vehicle(int weightPounds, Engine engine) ...

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