Parallel.java


Below is the syntax highlighted version of Parallel.java from §3.6 Case Study: Purple America.


/******************************************************************************
 *  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);
    }
}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.