954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create logic formula using a recursive method

Good morning everyone,

I have a small problem here. I tried to code a piece of program which allows users to construct logic formula. (E.g A V NOT(B))

When the button NOT (unary connective) is pressed 2 times then this happens: NOT(NOT()), using a static recursive method called Rec returning the string and has 2 following parameters:

--> int counter : the number of time the button has been clicked
--> string strConnective: the name of the button pressed(E.g NOT).

But the problem is when another button than NOT is pressed (E.g[path]) which is also an unary connective, the output is not updating, however the output changes to [path]( [path]()) instead of having the correct output NOT(NOT([path]())).


recursive method REC

public static String Rec (int counter, String strConnective){
	if (counter==0) return "";
	else {
				
	counter--;
	strFormulaContent=strConnective+("+Rec(counter,strConnective)+")";
	return  strFormulaContent;
				
	   }
	}


actionPerformed method which is overridden in my main class

public void actionPerformed(ActionEvent e) {
	
	//If statement for NOT button	
	if (e.getSource().equal(not_button)){	

			if(formula_text_JT.getText().equals("")) 
				not_counter=0;
			
			not_counter++;
			if (not_counter!=0){
			
formulaStrings=Rec(not_counter,e.getActionCommand());
				formula_text_JT.setText(formulaStrings);
      }

I spend the all the day to figure it out but no success at all.. Thank you

Attachments Screen_shot_2012-01-26_at_03.14_.12_.png 21.6KB
jkembo
Newbie Poster
8 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: