#Coding an exclusive OR program by creating a truth chart.
47 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @silver berry! Please use
/closeor theClose Postbutton 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.
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
could just hardcode the 4 cases
that would be much easier
I don't really need a loop do I
yeah
I'll try that right now
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)
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...
make a separate method, it'll make it much easier to work with
also that isn't exactly doing XOR
is it not?
no, you're hardcoding the results as well
you need to only hardcode the inputs here
also you could use %b with booleans directly instead of %s and stringified booleans
Oh
great point
wooo
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