Parallel.java
This is the syntax highlighted version of Parallel.java
from 3.5 Inheritance of
Introduction to Computer Science by
Robert Sedgewick and Kevin Wayne.
/*************************************************************************
* Compilation: javac Parallel.java
* Execution: java Parallel
*
*************************************************************************/
public class Parallel extends Circuit {
private Circuit a;
private Circuit b;
public Parallel(Circuit a, Circuit b) {
this.a = a;
this.b = b;
}
public double getResistance() {
double R1 = a.getResistance();
double R2 = a.getResistance();
return 1.0 / (1.0 / R1 + 1.0 / R2);
}
public double getPotentialDiff() {
if (a.getPotentialDiff() != b.getPotentialDiff())
throw new RuntimeException("Different voltages in branches of parallel circuit.");
return a.getPotentialDiff();
}
public void applyPotentialDiff(double V) {
a.applyPotentialDiff(V);
b.applyPotentialDiff(V);
}
}
Last updated: Fri Jul 16 16:41:04 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.