Java
Electric Circuit
Programming
Model Construction
Software Development

Construct a model of an electric circuit in java

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Modeling an electric circuit in Java is mostly a design problem: you need classes that represent components, their electrical properties, and how they are connected. A good first model does not try to simulate every law of physics at once; it usually starts with a small component hierarchy and enough behavior to compute quantities such as total resistance or current in simple circuits.

Start with a Component Abstraction

A common object-oriented approach is to define a shared interface for circuit elements. For a simple DC resistance model, each component can expose its resistance.

java
public interface CircuitComponent {
    double resistanceOhms();
}

Then specific components implement that interface.

java
1public final class Resistor implements CircuitComponent {
2    private final double resistanceOhms;
3
4    public Resistor(double resistanceOhms) {
5        this.resistanceOhms = resistanceOhms;
6    }
7
8    @Override
9    public double resistanceOhms() {
10        return resistanceOhms;
11    }
12}

This keeps the first version of the model simple and focused.

Build a Series Circuit

In a series circuit, resistances add directly. A circuit class can aggregate components and compute the total.

java
1import java.util.ArrayList;
2import java.util.List;
3
4public final class SeriesCircuit {
5    private final List<CircuitComponent> components = new ArrayList<>();
6
7    public void add(CircuitComponent component) {
8        components.add(component);
9    }
10
11    public double totalResistanceOhms() {
12        return components.stream()
13            .mapToDouble(CircuitComponent::resistanceOhms)
14            .sum();
15    }
16}

And use it like this:

java
1public class Main {
2    public static void main(String[] args) {
3        SeriesCircuit circuit = new SeriesCircuit();
4        circuit.add(new Resistor(100));
5        circuit.add(new Resistor(220));
6        circuit.add(new Resistor(330));
7
8        System.out.println(circuit.totalResistanceOhms());
9    }
10}

This does not simulate voltage sources yet, but it is already a working model of a circuit structure.

Add Voltage and Current with Ohm's Law

For a simple DC model, Ohm's law is enough to compute current from voltage and resistance.

java
1public final class VoltageSource {
2    private final double volts;
3
4    public VoltageSource(double volts) {
5        this.volts = volts;
6    }
7
8    public double volts() {
9        return volts;
10    }
11}
java
1public final class SimpleDcCircuit {
2    private final VoltageSource source;
3    private final SeriesCircuit load;
4
5    public SimpleDcCircuit(VoltageSource source, SeriesCircuit load) {
6        this.source = source;
7        this.load = load;
8    }
9
10    public double currentAmps() {
11        return source.volts() / load.totalResistanceOhms();
12    }
13}

Usage:

java
1public class Main {
2    public static void main(String[] args) {
3        SeriesCircuit load = new SeriesCircuit();
4        load.add(new Resistor(100));
5        load.add(new Resistor(200));
6
7        SimpleDcCircuit circuit = new SimpleDcCircuit(new VoltageSource(12.0), load);
8        System.out.println(circuit.currentAmps());
9    }
10}

This gives you a clean domain model for a very simple kind of circuit analysis.

Keep the Scope of the Model Honest

A real electric-circuit simulator can become much more complicated:

  • parallel branches,
  • capacitors and inductors,
  • transient behavior,
  • Kirchhoff's laws,
  • numerical solvers.

If the goal is educational modeling or a business application that stores circuit descriptions, the object model above may be enough. If the goal is realistic simulation, you will eventually need graph-based topology and more advanced mathematics.

That is why it helps to decide early whether you are building:

  • a structural model,
  • a simple analytical calculator,
  • or a full simulation engine.

Common Pitfalls

  • Trying to model all circuit physics in the first version instead of starting with one small subset such as resistive DC series circuits.
  • Mixing structural concerns, such as component connectivity, with simulation math in one giant class.
  • Forgetting that parallel circuits need different resistance logic from series circuits.
  • Using mutable shared state everywhere instead of small immutable component objects.
  • Calling the result a simulator when the code only represents structure or basic algebra.

Summary

  • A good Java circuit model starts with clear abstractions for components and connections.
  • An interface such as CircuitComponent keeps the design extensible.
  • Series circuits are an easy first case because resistances add directly.
  • Ohm's law lets you extend the model from structure into simple current calculations.
  • Decide early whether you are building a structural model or a real simulator, because the architecture requirements are very different.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.