einjelle 0 Newbie Poster

Hi guys..
I am currenly coding a program which outputs the next state table for flip flop circuits.
I have already finished the the generator for one type of flip flop, and I have 3 more to go..

So far, my arithmetic parser is working good but I suddenly remembered that it is included in our specifications that the user could also input XOR operations, which my parser couldn't handle.

So now,
I am looking for existing codes in expanding XOR equations so that the expanded equation would just be the one to be substituted and processed by the parser.

like this one:
/*XOR = & */
(A&(B&C)) = ABC + A'B'C' + A'B'C + A'BC + AB'C' + ABC'


If you want to see my program, here it is:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


/**
 * @author Jenielle Gabriel
 *
 */
public class FFGen extends JFrame{
	
	private String FlipflopType = "";
	private boolean isOneFlipflop = true;
	private boolean isInput = false;
	private boolean isOutput = false;
	
	String JAEquation = "";
	String KAEquation = "";
	String JBEquation = "";
	String KBEquation = "";
	String OutputEquation = "";
	
	JFrame first;
	JFrame JKFrame;
	JFrame JKTableFrame;
	
	public void First(){
		
		/**
		 * @param args
		 */
		JLabel pleaseChooseFFType;
		String[] FlipflopTypes = {"JK", "RS", "D", "T"};; 
		JComboBox FlipflopList;
		
		JLabel pleaseChooseFFNum;
		ButtonGroup FlipflopNumberGroup;
		JRadioButtonMenuItem oneFlipflop;
		JRadioButtonMenuItem twoFlipflop;
		
		JLabel pleaseCheck;
		JCheckBox oneInput;
		JCheckBox oneOutput;
		
		JButton next;
		
		
		JPanel firstConLeft;
		JPanel firstConRight;
	
		FlipflopList = new JComboBox(FlipflopTypes);
		FlipflopList.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						JComboBox cb = (JComboBox)e.getSource();
				        FlipflopType = (String)cb.getSelectedItem();
					}
				}
		);
		
		pleaseChooseFFType = new JLabel("Type of Flipflop:");
		oneFlipflop = new JRadioButtonMenuItem ("1");
		oneFlipflop.addActionListener(
				new ActionListener(){
					public void  actionPerformed(ActionEvent e){
						isOneFlipflop = true;
					}
				}
		);
		twoFlipflop = new JRadioButtonMenuItem ("2");
		twoFlipflop.addActionListener(
				new ActionListener(){
					public void  actionPerformed(ActionEvent e){
						isOneFlipflop = false;
					}
				}
		);
		
		pleaseChooseFFNum = new JLabel("Number of Flipflops:");
		FlipflopNumberGroup = new ButtonGroup();
		FlipflopNumberGroup.add(oneFlipflop);
		FlipflopNumberGroup.add(twoFlipflop);
		
		pleaseCheck = new JLabel("There is an:");
		oneInput = new JCheckBox ("Input");
		oneInput.addItemListener(
				new ItemListener(){
					public void itemStateChanged(ItemEvent e) {
						if (e.getStateChange() == ItemEvent.SELECTED)
							isInput = true;
						else if (e.getStateChange() == ItemEvent.DESELECTED)
					    	isInput = false;
					}
				}
		);
		
		oneOutput = new JCheckBox ("Output");
		oneOutput.addItemListener(
				new ItemListener(){
					public void itemStateChanged(ItemEvent e) {
						if (e.getStateChange() == ItemEvent.SELECTED)
							isOutput = true;
						else if (e.getStateChange() == ItemEvent.DESELECTED)
					    	isOutput = false;
					}
				}
		);
		
		next = new JButton ("Next");
		next.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						first.setVisible(false);
						if(FlipflopType == "JK")
							JK();
					}
				}
		);
		
		first =  new JFrame();
		
		firstConLeft = new JPanel();
		firstConRight = new JPanel();
		firstConRight.setLayout(new GridLayout(9,1,0,0));
		firstConRight.add(pleaseChooseFFType);
		firstConRight.add(FlipflopList);
		firstConRight.add(pleaseChooseFFNum);
		firstConRight.add(oneFlipflop);
		firstConRight.add(twoFlipflop);
		firstConRight.add(pleaseCheck);
		firstConRight.add(oneInput);
		firstConRight.add(oneOutput);
		firstConRight.add(next);
		
		JPanel Empty = new JPanel();
		Empty.setLayout(new GridLayout(1,2));
		Empty.add(firstConLeft);
		Empty.add(firstConRight);
		first.add(Empty);
		
		first.setSize(250,235);
		first.setVisible(true);
		//first.validate();
	
	}
	
	public void JK(){
		
		JKFrame = new JFrame();
		
		final JTextField JAEq = new JTextField();
		final JTextField KAEq = new JTextField();
		final JTextField JBEq = new JTextField();
		final JTextField KBEq = new JTextField();
		final JTextField OutputEq = new JTextField();
		
		JPanel JKLeftPanel = new JPanel();
		JPanel JKRightPanel = new JPanel();
		JKRightPanel.setLayout(new GridLayout(7,2));
		JKRightPanel.add(new JLabel("Enter the equations. "));
		JKRightPanel.add(new JLabel("Observe Proper Syntax."));
		JKRightPanel.add(new JLabel("JA = "));
		JKRightPanel.add(JAEq);
		JKRightPanel.add(new JLabel("KA = "));
		JKRightPanel.add(KAEq);
		JKRightPanel.add(new JLabel("JB = "));
		JKRightPanel.add(JBEq);
		JKRightPanel.add(new JLabel("KB = "));
		JKRightPanel.add(KBEq);
		JKRightPanel.add(new JLabel("Output = "));
		JKRightPanel.add(OutputEq);
		JKRightPanel.add(new JLabel("Generate: "));
		JButton next = new JButton("next");
		JKRightPanel.add(next);
		
		//disabling unneeded textfields
		if(!isOutput){
			OutputEq.setEditable(false);
		}
		if(isOneFlipflop){
			JBEq.setEditable(false);
			KBEq.setEditable(false);
		}
		
		next.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						KAEquation = KAEq.getText();
						JAEquation = JAEq.getText();
						JBEquation = JBEq.getText();
						KBEquation = KBEq.getText();
						OutputEquation = OutputEq.getText();
						JKFrame.setVisible(false);
						genarateJK();
					}
				}
		);
		
		JKFrame.add(JKRightPanel);
		JKFrame.setSize(250,235);
		JKFrame.setVisible(true);
	}
	
	public void genarateJK(){
		
		JKTableFrame = new JFrame();
		
		int c = 0;
		int nextA[]= {0,0,0,0,0,0,0,0};  //Storage for next state A (has default values)
		int nextB[]= {0,0,0,0,0,0,0,0};  //Storage for next state B (has default values)

		int JA[] = {0,0,0,0,0,0,0,0}; //Storage for JA Values
		int JB[] = {0,0,0,0,0,0,0,0}; //Storage for JB Values
		int KA[] = {0,0,0,0,0,0,0,0}; //Storage for KA Values
		int KB[] = {0,0,0,0,0,0,0,0}; //Storage for KB Values
		int Output[] = {0,0,0,0,0,0,0,0}; //Storage for Output Values
		String AComp = null;
		String InputComp = null;
		String subsJA = null;

		if(isOneFlipflop && !isInput && !isOutput){ // one flipflop, no input, no output
			String[] A = {"1","0"}; 
			c = 2;
			
			for(int j=0; j<c; j++){

				//Complement of A
				AComp = "";
				if(A[j] == "0")
					AComp = "1";
				else{
					AComp = "0";
				}
				
				//replace JA Formula with current value of A'
				subsJA = replace(JAEquation,"A'",AComp);
				while(!subsJA.equals(replace(JAEquation,"A'",AComp))){
					subsJA = replace(JAEquation,"A'",AComp);
				}
				
				//replace JA Formula with current value of A
				subsJA = replace(subsJA,"A",A[j]);
				while(!subsJA.equals(replace(subsJA,"A",A[j]))){
					subsJA = replace(subsJA,"A",A[j]);
				}
				
				
				//replace KA Formula with current value of A'
				String subsKA = replace(KAEquation,"A'",AComp);
				while(!subsKA.equals(replace(KAEquation,"A'",AComp))){
					subsKA = replace(KAEquation,"A'",AComp);
				}
				
				
				//replace KA Formula with current value of A
				subsKA = replace(subsKA,"A",A[j]);
				while(!subsKA.equals(replace(subsKA,"A",A[j]))){
					subsKA = replace(subsKA,"A",A[j]);
				}
				
				
				try{
					
				Parser parser =  new Parser();
				//compute the value of JA
				int computedJAValue = (int)parser.evaluate(subsJA);
				JA[j] = computedJAValue;
				
				//compute the value of KA
				int computedKAValue = (int)parser.evaluate(subsKA);
				KA[j] = computedKAValue;
				
				}catch(Exception e){
					//testlang.setText(e.getMessage());
				}
				
				if(JA[j] == 0 && KA[j] == 0){
					nextA[j] = Integer.parseInt(A[j]);
				}
				else  if(JA[j] == 0 && KA[j] == 1){
					nextA[j] = 0;
				}
				else  if(JA[j] == 1 && KA[j] == 0){
					nextA[j] = 1;
				}
				else  if(JA[j] == 1 && KA[j] == 1){
					if(A[j] == "0")
						nextA[j] = 1;
					else if(A[j] == "1")
						nextA[j] = 0;
				}
				
			}//End for loop
			JLabel a = new JLabel("<html>Present<br>States<br>"+A[0]+"<br>"+A[1]);
			a.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel b = new JLabel("<html>JA<br><br>"+JA[0]+"<br>"+JA[1]);
			b.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel cee = new JLabel("<html>KA<br><br>"+KA[0]+"<br>"+KA[1]);
			cee.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel d = new JLabel("<html>Next<br>States<br>"+nextA[0]+"<br>"+nextA[1]);
			d.setHorizontalTextPosition(JLabel.CENTER);
			
			JKTableFrame.setLayout(new GridLayout(1,4));
			JKTableFrame.add(a);
			JKTableFrame.add(b);
			JKTableFrame.add(cee);
			JKTableFrame.add(d);
			
			JKTableFrame.setSize(250,235);
			JKTableFrame.setVisible(true);
			
		}//End one flipflop, no input, no output
		
		
		else if(isOneFlipflop && isInput && !isOutput){ // one flipflop, isInput, no output
			String[] A = {"1","1","0","0"}; 
			String[] Input = {"1","1","0","0"}; 
			c = 4;
			
			for(int j=0; j<c; j++){

				//Complement of A
				AComp = "";
				if(A[j] == "0")
					AComp = "1";
				else
					AComp = "0";
				
				//complement of input
				InputComp = "";
				if(Input[j] == "0")
					InputComp = "1";
				else
					InputComp = "0";
					
				//replace JA Formula with current value of A'
				subsJA = replace(JAEquation,"A'",AComp);
				while(!subsJA.equals(replace(JAEquation,"A'",AComp))){
					subsJA = replace(JAEquation,"A'",AComp);
				}
				
				//replace JA Formula with current value of A
				subsJA = replace(subsJA,"A",A[j]);
				while(!subsJA.equals(replace(subsJA,"A",A[j]))){
				subsJA = replace(subsJA,"A",A[j]);
				}
				
				//replace x Formula with current value of x'
				subsJA = replace(subsJA,"x'",InputComp);
				while(!subsJA.equals(replace(subsJA,"x'",InputComp))){
					subsJA = replace(subsJA,"x'",InputComp);
				}
				
				//replace JA Formula with current value of x
				subsJA = replace(subsJA,"x",A[j]);
				while(!subsJA.equals(replace(subsJA,"x",A[j]))){
					subsJA = replace(subsJA,"x",A[j]);
				}
				
				
				//replace KA Formula with current value of A'
				String subsKA = replace(KAEquation,"A'",AComp);
				while(!subsKA.equals(replace(KAEquation,"A'",AComp))){
					subsKA = replace(KAEquation,"A'",AComp);
				}
				
				//replace KA Formula with current value of A
				subsKA = replace(subsKA,"A",A[j]);
				while(!subsKA.equals(replace(subsKA,"A",A[j]))){
				subsKA = replace(subsKA,"A",A[j]);
				}
				
				//replace x Formula with current value of x'
				subsKA = replace(subsKA,"x'",InputComp);
				while(!subsKA.equals(replace(subsKA,"x'",InputComp))){
					subsKA = replace(subsKA,"x'",InputComp);
				}
				
				//replace KA Formula with current value of x
				subsKA = replace(subsKA,"x",A[j]);
				while(!subsKA.equals(replace(subsKA,"x",A[j]))){
					subsKA = replace(subsKA,"x",A[j]);
				}
				
				
				
				try{
					
				Parser parser =  new Parser();
				//compute the value of JA
				int computedJAValue = (int)parser.evaluate(subsJA);
				JA[j] = computedJAValue;
				
				//compute the value of KA
				int computedKAValue = (int)parser.evaluate(subsKA);
				KA[j] = computedKAValue;
				
				}catch(Exception e){
					//testlang.setText(e.getMessage());
				}
				
				if(JA[j] == 0 && KA[j] == 0){
					nextA[j] = Integer.parseInt(A[j]);
				}
				else  if(JA[j] == 0 && KA[j] == 1){
					nextA[j] = 0;
				}
				else  if(JA[j] == 1 && KA[j] == 0){
					nextA[j] = 1;
				}
				else  if(JA[j] == 1 && KA[j] == 1){
					if(A[j] == "0")
						nextA[j] = 1;
					else if(A[j] == "1")
						nextA[j] = 0;
				}
				
			}//End for loop
			
			JLabel a = new JLabel("<html>Present<br>States<br>"+A[0]+"<br>"+A[1]+"<br>"+A[2]+"<br>"+A[3]);
			a.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel b = new JLabel("<html>JA<br><br>"+JA[0]+"<br>"+JA[1]+"<br>"+JA[2]+"<br>"+JA[3]);
			b.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel cee = new JLabel("<html>KA<br><br>"+KA[0]+"<br>"+KA[1]+"<br>"+KA[2]+"<br>"+KA[3]);
			cee.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel d = new JLabel("<html>Next<br>States<br>"+nextA[0]+"<br>"+nextA[1]+"<br>"+nextA[2]+"<br>"+nextA[3]);
			d.setHorizontalTextPosition(JLabel.CENTER);
			
			JKTableFrame.setLayout(new GridLayout(1,4));
			JKTableFrame.add(a);
			JKTableFrame.add(b);
			JKTableFrame.add(cee);
			JKTableFrame.add(d);
			
			JKTableFrame.setSize(250,235);
			JKTableFrame.setVisible(true);
			
		}//End one flipflop, isInput, no output
		
		else if(isOneFlipflop && !isInput && isOutput){ // one flipflop, no input, isOutput
			String[] A = {"1","0"}; 
			c = 2;
			
			for(int j=0; j<c; j++){

				//Complement of A
				AComp = "";
				if(A[j] == "0")
					AComp = "1";
				else{
					AComp = "0";
				}
				
				//replace JA Formula with current value of A'
				subsJA = replace(JAEquation,"A'",AComp);
				while(!subsJA.equals(replace(JAEquation,"A'",AComp))){
					subsJA = replace(JAEquation,"A'",AComp);
				}
				
				//replace JA Formula with current value of A
				subsJA = replace(subsJA,"A",A[j]);
				while(!subsJA.equals(replace(subsJA,"A",A[j]))){
					subsJA = replace(subsJA,"A",A[j]);
				}
				
				
				//replace KA Formula with current value of A'
				String subsKA = replace(KAEquation,"A'",AComp);
				while(!subsKA.equals(replace(KAEquation,"A'",AComp))){
					subsKA = replace(KAEquation,"A'",AComp);
				}
				
				
				//replace KA Formula with current value of A
				subsKA = replace(subsKA,"A",A[j]);
				while(!subsKA.equals(replace(subsKA,"A",A[j]))){
					subsKA = replace(subsKA,"A",A[j]);
				}
				
				//replace Ouput Formula with current value of A'
				String subsOutput = replace(OutputEquation,"A'",AComp);
				while(!subsOutput.equals(replace(OutputEquation,"A'",AComp))){
					subsOutput = replace(OutputEquation,"A'",AComp);
				}
				
				
				//replace Output Formula with current value of A
				subsOutput = replace(subsOutput,"A",A[j]);
				while(!subsOutput.equals(replace(subsOutput,"A",A[j]))){
					subsOutput = replace(subsOutput,"A",A[j]);
				}
				
				
				try{
					
				Parser parser =  new Parser();
				//compute the value of JA
				int computedJAValue = (int)parser.evaluate(subsJA);
				JA[j] = computedJAValue;
				
				//compute the value of KA
				int computedKAValue = (int)parser.evaluate(subsKA);
				KA[j] = computedKAValue;
				
				//compute the value of Output
				int computedOutputValue= (int)parser.evaluate(subsOutput);
				Output[j] = computedOutputValue;
				
				}catch(Exception e){
					//testlang.setText(e.getMessage());
				}
				
				if(JA[j] == 0 && KA[j] == 0){
					nextA[j] = Integer.parseInt(A[j]);
				}
				else  if(JA[j] == 0 && KA[j] == 1){
					nextA[j] = 0;
				}
				else  if(JA[j] == 1 && KA[j] == 0){
					nextA[j] = 1;
				}
				else  if(JA[j] == 1 && KA[j] == 1){
					if(A[j] == "0")
						nextA[j] = 1;
					else if(A[j] == "1")
						nextA[j] = 0;
				}
				
			}//End for loop
			JLabel a = new JLabel("<html>Present<br>States<br>"+A[0]+"<br>"+A[1]);
			a.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel b = new JLabel("<html>JA<br><br>"+JA[0]+"<br>"+JA[1]);
			b.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel cee = new JLabel("<html>KA<br><br>"+KA[0]+"<br>"+KA[1]);
			cee.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel d = new JLabel("<html>Next<br>States<br>"+nextA[0]+"<br>"+nextA[1]);
			d.setHorizontalTextPosition(JLabel.CENTER);
			
			JLabel e = new JLabel("<html>Output<br>"+Output[0]+"<br>"+Output[1]);
			e.setHorizontalTextPosition(JLabel.CENTER);
			
			JKTableFrame.setLayout(new GridLayout(1,5));
			JKTableFrame.add(a);
			JKTableFrame.add(b);
			JKTableFrame.add(cee);
			JKTableFrame.add(d);
			JKTableFrame.add(e);
			
			JKTableFrame.setSize(250,235);
			JKTableFrame.setVisible(true);
			
		}//End one flipflop, no input, isoutput
		
	}
	
	static String replace(String str, String pattern, String replace) {
        int s = 0;
        int e = 0;
        StringBuffer result = new StringBuffer();
    
        while ((e = str.indexOf(pattern, s)) >= 0) {
            result.append(str.substring(s, e));
            result.append(replace);
            s = e+pattern.length();
        }
        result.append(str.substring(s));
        return result.toString();
    }

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FFGen gui = new FFGen();
		gui.First();
		gui.repaint();
	}

}

I still have a lot of coding to do because of the several condition combinations i have to make for each flip flop type.

I hope you guys can help me..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.