Circuit Simulator in Java
October 3, 2007 9:15 PM
Subscribe
Can you help me solve this (not homework) computer science problem?
A friend of mine (who is a physics professor) asked me to write a program for him that would implement this:
http://yellowbkpk.com/algebra%20circuits.pdf
To do this, I wrote a Java program with a data model that looks somewhat like this:
- Addition, Negation, Reciprocal, Opposite, Input, and Output are all concrete implementations of a "circuit element" abstract class.
- A circuit element has an array of inputs, each of which is a reference to another circuit element.
- A circuit element has an abstract
getValue() method. For example, the Addition circuit element returns input[0].getValue() + input[1].getValue().
- The output for the circuit is computed when the Output circuit element calls its input's
getValue() (which initiates the recursion).
The problem with this design is that it only works for combinatoric circuits (like the "subtraction" example in the PDF). When I try something like the bottom three examples, I get infinite recursion.
Can you suggest a better way to implement this?
posted by yellowbkpk to computers & internet (21 comments total)
3 users marked this as a favorite
posted by yellowbkpk at 9:15 PM on October 3, 2007