1

So I'm currently trying to construct Sentences in Propositional Logic in java but I ran into a problem with what is probably basic Java but I've tried researching it and I don't know what's actually wrong.

The problem I'm running into is that when I construct a new Complex Sentence (which has a binary connective, a left side, and a right side) it changes my old sentences. How can it change previous instances if I'm initiating a brand new one and not even adding it to the KB(Knowledge Base)? When I ran the debugger it looks like when the new 2nd sentence is being constructed, it still uses the old instance, as the old instance was the "this" in the debugger variables.

Here is my main method:

 public static void main(String[] args) {

    runModusPonens();
}


public static void runModusPonens(){
    KB modusKB=new KB();
    Symbol p=modusKB.intern("P");
    Symbol q=modusKB.intern("Q");
    modusKB.add(p);
    Sentence im=new ComplexSentence(LogicalConnective.IMPLIES,p,q);

    modusKB.add(im);

    modusKB.printWorld();

    Sentence i=new ComplexSentence(LogicalConnective.AND,q,p);

    modusKB.printWorld();
}

**The output is:

First modusKB print out:

P

(P IMPLIES Q)

Second modusKB print out (after the 2nd sentence was created)

P

(Q AND P)   //-->Should still be (P IMPLIES Q)**

And my ComplexSentence class

public class ComplexSentence implements Sentence{
//Sentence structure: leftSide(left hand side sentence) binarycon(connective) rightSide(right hand side sentence)
public static LogicalConnective binarycon;
public static Sentence leftSide;
public static Sentence rightSide;

public ComplexSentence(LogicalConnective connective,Sentence left, Sentence right){
    binarycon=connective;
    leftSide=left;
    rightSide=right;
}



public String toString() {
    return "("+this.leftSide+" "+this.binarycon+" "+this.rightSide+")";
}

}

Note: Sentence interface doesn't have anything in it yet so can't be it.

1
  • All your variables are static. They shouldn't be static if you don't want any one instance changing their value. Commented Mar 13, 2016 at 9:09

1 Answer 1

2

Your problem seems to be the static members, which are shared by all instances of your ComplexSentence class :

public static LogicalConnective binarycon;
public static Sentence leftSide;
public static Sentence rightSide;

make them non static in order to allow different instances of the class to have different values.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh your right thank you so much! I had overlooked that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.