You are here:

Java/Java Logical Circuit

Advertisement


Question
I was proposed a project that sounds like this.

   Consider a set of logical gates like AND, OR, XOR or NOT. Allow for building any logical circuit by connecting such gates.
   Provide input (0/1) to a number of gates and collect the results at the output of some of the gates.
   Be able to read the description of the circuit from a file.

I was also offered an idea like.. "a single AND gate, 2 inputs and a single output. You could have something like:

public class LogicAnd extends Gate
{
   public LogicAnd(int a, int b, int q) {
       a_ = a; b_ =b; q_ = q;
   }

   public void Evaluate(int[] signals) {
       signals[q_] = signals[a_]&signals[b_];
   }
}



Now the difficult bit would be the order in which you call the Evaluate functions of the gates (there'll be dependances you'd need to traverse.)"

This is what I was suggested though I don't know how to shape this problem into ending somewhere. Would appreciate the help. What I have to use is Java Language. Thank you.

Answer
Assuming that the Gate class defines the variables a_, b_, c_, you can put all your Gates in an array and execute them in order.

E.g.

Gate[] gates = new Gate[]
{
  new LogicAnd(0,1,2), //2 = 1 & 2
  new LogicOr(3,2,2), //2 = 3 | 2
  new LogicNot(3,4),  //4 = !3
  ...
  
};

int[] signals = new int[10]; //number of signals
//initial states
signal[0] = 1;
signal[1] = 0;
signal[3] = 1;
//Execute all gates
for(Gate gate : gates)
{
  gate.Evaluate(signals);
}

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.