#Coding an exclusive OR program by creating a truth chart.

47 messages · Page 1 of 1 (latest)

silver berry
#

I'm not sure how to code this into my program... I've been thinking about using a loop and printing the values but I'm not sure how I would hit every condition (especially the False False Condition).

old depotBOT
#

This post has been reserved for your question.

Hey @silver berry! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

silver berry
#
public class PXORQ {
  public static void main (String [] args) {
    boolean P = true; 
    boolean Q = true; 
    System.out.format("%s\t\t%s\t\t%s", "p", "q", "(p xor q)"); 
    System.out.println("--------------------------------");
    
    for (int i = 0; i < 4; i++) {
      if ((P || Q) && (P && Q)) {
        System.out.format("%16s", "false"); 
        Q = !Q; 
      }
      else if ((P || Q) == false) {
        System.out.format("16s", "false"); 
      }
      else if ((P || Q) == true) {
        System.out.format("%16s", "true"); 
        P = !P; 
        Q = !Q; 
      }
     
    }
  }
}```
#

This is what I have rn

covert phoenix
#

could just hardcode the 4 cases

silver berry
#

Like

#

ohhh

covert phoenix
#

that would be much easier

silver berry
#

I don't really need a loop do I

covert phoenix
#

yeah

silver berry
#

I'll try that right now

covert phoenix
#

have a separate method that does the XOR, then just do System.out.format(..., true, true, xor(true, true)); or something like that

#

or you could have the print in the method for guaranteed consistency as well

#

(sidenote, xor can be done with just !=, although this task seems like it wants you to use the full form)

silver berry
#

xor can be done with !=?

#

hmm

#

oh yea

#

that makes a lot of sense

#

yea I think the teacher wants me to do it manually...

#
  public static void main (String [] args) {
    boolean P = true; 
    boolean Q = true; 
    System.out.format("%s\t\t%s\t\t%s\n", "p", "q", "(p xor q)"); 
    System.out.println("--------------------------------");
  
    if ((P || Q) && (P && Q)) {
      System.out.format("%s%8s%16s\n", "true", "true", "false"); 
    }

    Q = false;
    if ((P || Q) == true) {
      System.out.format("%s%8s%16s\n", "true", "false", "true"); 
    }

    P = true;
    Q = false; 
    if ((P || Q) == true) {
      System.out.format("%s%8s%16s\n", "false", "true", "true"); 
    }

    P = false;
    if ((P || Q) == false) {
      System.out.format("%s%8s%16s\n", "false", "false", "false"); 
    }
    
  }
}```
#

oh

#
public class PXORQ {
  public static void main (String [] args) {
    boolean P = true; 
    boolean Q = true; 
    System.out.format("%s\t\t%s\t\t%s\n", "p", "q", "(p xor q)"); 
    System.out.println("--------------------------------");
  
    if ((P || Q) && (P && Q)) {
      System.out.format("%s%8s%16s\n", "true", "true", "false"); 
    }

    Q = false;
    if ((P || Q) == true) {
      System.out.format("%s%8s%16s\n", "true", "false", "true"); 
    }

    P = true;
    Q = false; 
    if ((P || Q) == true) {
      System.out.format("%s%8s%16s\n", "false", "true", "true"); 
    }

    P = false;
    if ((P || Q) == false) {
      System.out.format("%s%8s%16s\n", "false", "false", "false"); 
    }
    
  }
}```
#

Some weird formatting errors...

covert phoenix
#

make a separate method, it'll make it much easier to work with

#

also that isn't exactly doing XOR

silver berry
#

is it not?

covert phoenix
#

no, you're hardcoding the results as well

#

you need to only hardcode the inputs here

silver berry
#

ah

#

oh right

#

Ok ok

#

let's create a method

covert phoenix
#

also you could use %b with booleans directly instead of %s and stringified booleans

silver berry
#

Oh

#

great point

#
public class PXORQ {

  public static boolean exclusiveOr (boolean case1, boolean case2) {
    if ((case1 || case2) && (case1 && case2)) {
      return false; 
    }
    else if (case1 || case2) {
      return true; 
    }
    else {
      return false; 
    }
  }
  
  public static void main (String [] args) {
    boolean P = true; 
    boolean Q = true; 
    System.out.format("%s\t\t%s\t\t%s\n", "p", "q", "(p xor q)"); 
    System.out.println("--------------------------------");
    System.out.format("%b%8b%16b\n", true, true, exclusiveOr(true, true)); 
    System.out.format("%b%8b%16b\n", true, false, exclusiveOr(true, false)); 
    System.out.format("%b%8b%16b\n", false, true, exclusiveOr(false, true)); 
    System.out.format("%b%8b%16b\n", false, false, exclusiveOr(false, false)); 
  

  }
}```
#

formatting is scuffed

#

I'll fix that

#

👯‍♂️

#

oops

#

🥳

#

!clse

#

!lose

#

!close