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–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:27:28 EDT 2022.