Circuit.java


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


/******************************************************************************
 *  Compilation:  javac Circuit.java
 *  Execution:    java Circuit
 *
 ******************************************************************************/

public abstract class Circuit {
    public abstract double getResistance();
    public abstract double getPotentialDiff();
    public abstract void applyPotentialDiff(double V);

    public double getPower() {
            return getPotentialDiff() * getPotentialDiff() / getResistance();
    }

    public double getCurrent() {
            return getPotentialDiff() / getResistance();
    }

    public static void main(String[] args) {

/*
        // "circuit2.ai"
        Resistor a = new Resistor(3.0);
        Resistor b = new Resistor(3.0);
        Resistor c = new Resistor(6.0);
        Resistor d = new Resistor(3.0);
        Resistor e = new Resistor(2.0);

        Series   f = new Series(a, b);
        Parallel g = new Parallel(c, d);
        Series   h = new Series(g, e);
        Parallel completeCircuit = new Parallel(h, f);

        // "circuit.ai"
        Resistor a = new Resistor(8);
        Resistor b = new Resistor(8);
        Resistor c = new Resistor(4);
        Resistor d = new Resistor(2);
        Resistor e = new Resistor(6);
        Resistor f = new Resistor(6);
        Resistor g = new Resistor(4);

        Parallel circuit = new Parallel(new Series(a, new Parallel(b, new Series(c, d))), new Series(e, new Parallel(f, g)));


        // 3 in parallel
        Resistor a = new Resistor(4.0);
        Resistor b = new Resistor(8.0);
        Resistor c = new Resistor(5.0);
        Resistor d = new Resistor(3.0);
        Parallel e = new Parallel(a, b);
        Parallel f = new Parallel(e, c);
        Series circuit = new Series(f, d);


        // 3 in series
        Resistor a = new Resistor(2.0);
        Resistor b = new Resistor(4.0);
        Resistor c = new Resistor(6.0);
        Resistor d = new Resistor(7.0);
        Resistor g = new Resistor(3.4);
        Resistor h = new Resistor(2.7);
        Series   e = new Series(a, b);
        Series   f = new Series(e, c);
        Parallel i = new Parallel(f, d);
        Parallel j = new Parallel(g, h);
        Series circuit = new Series(i, j);

        // "bigcircuit.ai"
        Resistor a = new Resistor(4.1);
        Resistor b = new Resistor(6.5);
        Resistor c = new Resistor(2.5);
        Resistor d = new Resistor(4.0);
        Resistor e = new Resistor(2.8);
        Resistor f = new Resistor(4.3);
        Resistor g = new Resistor(2.7);
        Resistor h = new Resistor(3.5);
        Resistor i = new Resistor(8.0);
        Resistor j = new Resistor(5.0);
        Resistor k = new Resistor(7.7);
        Resistor l = new Resistor(4.4);
        Resistor m = new Resistor(6.9);
        Resistor n = new Resistor(9.2);
        Series   o = new Series(c, d);
        Series   p = new Series(o, e);
        Parallel q = new Parallel(b, p);
        Series   r = new Series(q, f);
        Series   s = new Series(r, g);
        Series   t = new Series(j, k);
        Parallel u = new Parallel(i, t);
        Parallel v = new Parallel(u, l);
        Series   w = new Series(h, v);
        Series   x = new Series(w, m);
        Parallel y = new Parallel(s, x);
        Series   z = new Series(a, y);
        Series circuit = new Series(z, n);

*/
        // "bigcircuit2.ai"
        Resistor a = new Resistor(3.5);
        Resistor b = new Resistor(8.5);
        Resistor c = new Resistor(1.8);
        Resistor d = new Resistor(0.7);
        Resistor e = new Resistor(4.6);
        Resistor f = new Resistor(4.8);
        Resistor g = new Resistor(2.4);
        Resistor h = new Resistor(6.3);
        Resistor i = new Resistor(3.7);
        Resistor j = new Resistor(1.4);
        Resistor k = new Resistor(6.9);
        Resistor l = new Resistor(3.8);
        Resistor m = new Resistor(3.7);
        Resistor n = new Resistor(4.8);
        Resistor o = new Resistor(3.2);
        Resistor p = new Resistor(3.7);
        Resistor q = new Resistor(4.4);
        Resistor r = new Resistor(7.9);
        Resistor s = new Resistor(4.5);
        Parallel pa = new Parallel(b, c);
        Parallel pb = new Parallel(pa, d);
        Parallel pc = new Parallel(pb, e);
        Series   sa = new Series(a, pc);
        Parallel pd = new Parallel(g, h);
        Series   sb = new Series(f, pd);
        Series   sc = new Series(i, j);
        Series   sd = new Series(sc, k);
        Parallel pe = new Parallel(sb, sd);
        Series   se = new Series(pe, l);
        Series   sf = new Series(n, o);
        Series   sg = new Series(p, q);
        Parallel pf = new Parallel(sf, sg);
        Series   sh = new Series(m, pf);
        Parallel pg = new Parallel(se, sh);
        Series   si = new Series(sa, pg);
        Parallel ph = new Parallel(r, s);
        Series circuit = new Series(si, ph);


        StdOut.println("Resistance: " + circuit.getResistance() + " ohms");
        circuit.applyPotentialDiff(12.0);    //  attach 12V battery
        StdOut.println("Power of resistor d at 12V = " +d.getPower() + " W");

        circuit.applyPotentialDiff(15.0);    // attach 15V battery
        StdOut.println("Power of resistor d at 15V = " + d.getPower() + " W");

        StdOut.println("Current in resistor b = " + b.getCurrent());
    }
}




Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:27:28 EDT 2022.