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 it works...

If you look closely at the trafficByLane() method, you will notice that the problem is caused by the location of the calculation--inside the private class TrafficUnitWrapper. We can move it from there and create a new method of the TrafficDensity class:

double calcSpeed(double timeSec) {  double speed = this.vehicle.getSpeedMph(timeSec);  return Math.round(speed * this.trafficUnit.getTraction());}

Then, we can change its signature to this:

double calcSpeed(Vehicle vehicle, double traction, double timeSec) {  double speed = vehicle.getSpeedMph(timeSec);  return Math.round(speed * traction);}

Add these two methods to the TrafficUnitWrapper class: 

public Vehicle getVehicle() { return vehicle; }public double getTraction() { return ...

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