The code is supposed to calculate the total distance travele
The code is supposed to calculate the total distance traveled. It does this by using the time and average speed from each of the three sections of a trip. The code contains a bug. Write a set of JUnit tests that provide 100% coverage of the RateCalculator class . If done correctly, one (or more) of your tests should fail because of the error in the code. Now that you can see the error, update RateCalculator to fix the error.
package ratecalc;
public class RateCalculator {
public double totalDistance(double rate1, double time1, double rate2, double time2, double rate3,
double time3) {
// This code contains a bug
double distance1 = rate1 * time1;
double distance2 = rate2 * time2;
double distance3 = rate1 * time3;
double retVal = distance1 + distance2 + distance3;
return retVal;
}
}
Solution
Hi,
There is an issue in logic. I have modified the code and highlighted the code change below.
import junit.framework.Assert;
public class RateCalculator {
public static void main(String a[]){
Assert.assertEquals(14.0, totalDistance(1,1,2,2,3,3));//pass
Assert.assertFalse(15.0==totalDistance(1,1,2,2,3,3));
}
public static double totalDistance(double rate1, double time1, double rate2, double time2, double rate3,
double time3) {
// This code contains a bug
double distance1 = rate1 * time1;
double distance2 = rate2 * time2;
double distance3 = rate3 * time3;
double retVal = distance1 + distance2 + distance3;
return retVal;
}
}
