π» Activity: Create a Class for Parallel Circuits#
We built a class for circuits in series. Use what you learned last week about circuits with resistors in parallel and what you now know about classes to construct a class for circuits in parallel.
Consider this circuit with 9 volts of energy and three different resistors in parallel.
Build a class for a parallel type circuit called
ParallelCircuit
that
holds information about a circuit with resistors in parallel and
calculates the total resistance, current, and voltages across each resistor.
Add to the
ParallelCircuit
class a method that allows the resistance of each resistor to be associated with the circuit.Add to the
ParallelCircuit
class a method to the class to calculate the total resistance.Add to the
ParallelCircuit
class methods that calculate the current flowing through the circuit or the voltage across each resistor.
# create a class object
...
# create a constructor for the ParallelCircuit class
...
# create a method that adds a resistor to an instantiation of the ParallelCircuit class
...
# create a method that sums the list `components` of an instantiation of the ParallelCircuit class
...
# create a method that calculates the current flowing through an instantiation of the ParallelCircuit class
...
# create a method that calculates the voltage across each resistor an instantiation of the ParallelCircuit class
...
Apply this class to revisit the circuit with 9 volts of energy and three different resistors in parallel that we considered with nested lists last week.
# instantiate the class
...
# construct the circuit
...
# apply the total_resistance method to the instantiation of the class
total_resistance = ...
# apply the circuit_current method to the instantiation of the class
voltage_source = 9 # 9V voltage source
current = ...
# apply the component_voltages method to the instantiation of the class
voltages = ...