This methods compute circuit resistance when the resistors are in parallel and when the resistors are serial (do not worry if this means nothing to you -- I copied this from Wikipedia -- both methods are correct and their calculations easy to see). Write test cases that check all possible branches (ways to execute each if statement).
methods:
package computer science;
public class CircuitResistance {
public double resistorsInParallel(double r1, double r2, double r3) {
if (r1 == 0) {
return Double.NaN;
}
if (r2 == 0) {
return Double.NaN;
}
if (r3 == 0) {
return Double.NaN;
}
double inverseResist = (1/r1) + (1/r2) + (1/r3);
return 1/inverseResist;
}
public double resistorsInSerial(double r1, double r2, double r3) {
// Because Java uses "short-circuited" boolean operators, it executes the single if statement below as if it were written
// like the 3 if statements above.
if ((r1 == 0) || (r2 == 0) || (r3 == 0)) {
return Double.NaN;
}
double resist = r1 + r2 + r3;
return resist;
}
}