I will providing a CircuitResistance class which contains 2 methods. One of the
ID: 3585299 • Letter: I
Question
I will providing a CircuitResistance class which contains 2 methods. One of the methods computes circuit resistance when the resistors are in parallel and the second method computes the circuit resistance when the resistors are serial (do not worry if this means nothing to you -- I copied this from Wikipedia and will be giving you a working solution). You then need to write a CircuitResistanceTest class which contains JUnit test cases that validates my code. Each of your tests should be in its own method and, by running all of the tests, executes every line in my solution ("provide 100% coverage").
package edu.buffalo.cse116;
/**
* Calculate the resistance of a circuit. This is important for computer engineers and professors needing good code
* which makes students write lots of test cases.
*
* @author Matthew Hertz
*/
public class CircuitResistance {
/**
* Calculate the resistance of a circuit which includes three parallel resistors. According to Wikipedia, circuit
* resistance when the resistors are parallel is calculated as R^-1 = R1^-1 + R2^-1 + R3^-1.
*
* @see <a href="https://en.wikipedia.org/wiki/Series_and_parallel_circuits">Wikipedia article inspiring this
* problem</a>
* @param r1 Resistance provided by the first parallel component of the circuit.
* @param r2 Resistance provided by the second parallel component of the circuit.
* @param r3 Resistance provided by the third parallel component of the circuit.
* @return The overall resistance of the circuit. {@link Double#NaN} is returned when an impossible resistance is
* supplied. Despite having worked for a company which developed computers, the author's understanding of what
* this means is shaky.
*/
public static 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;
}
/**
* Calculate the resistance of a circuit which includes three resistors one after the other. According to Wikipedia,
* circuit resistance when the resistors are serial is calculated as R^ = R1 + R2 + R3.
*
* @see <a href="https://en.wikipedia.org/wiki/Series_and_parallel_circuits">Wikipedia article inspiring this
* problem</a>
* @param r1 Resistance provided by the first parallel component of the circuit.
* @param r2 Resistance provided by the second parallel component of the circuit.
* @param r3 Resistance provided by the third parallel component of the circuit.
* @return The overall resistance of the circuit. {@link Double#NaN} is returned when an impossible resistance is
* supplied. The author has a better understanding how this equation works.
*/
public static 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;
}
}
Explanation / Answer
Hi,
since there are only 2 methods in your class, you need to write UnitTests for both these methods, like below,
public class CircuitTesistanceTest {
@Test //test method
public void testParallel() {
// assert statements
assertEquals(1.0, CircuitResistance.resistorsInParallel(2.0, 4.0, 4.0));
}
@Test
public void testSerial() {
// assert statements
assertEquals(33.0, CircuitResistance.resistorsInSerial(10, 11, 12));
}
}
If you se in the method above, assertEquals method takes two inputs, expected value and returned output, and compares both the value, above i have written sample test case for both the methods, you can write any number of test case you want in similar fashion,
Thumbs up if this was helpful, otherwise let me know in comments.