Reputation: 11
I am pretty new to Java and learning from a book. I've just learned about casting types and boolean values/logical operators in the book and i was getting to a challenge. A logical operator table with true and false values program, i have to adjust it so that it shows 1 and 0 instead of true and false, so i came up with this:
/* Project 2-2: a truth table for the logical operators.
show 1 en 0 ipv true en false.
*/
class LogicalOpTableSimon {
public static void main(String args[]) {
int p, q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
p = 1; q = 1;
System.out.print(p + "\t" + q +"\t");
System.out.print(p + "\t" + q + "\t");
p = 0;
System.out.println(p + "\t" + p);
p = 1; q = 0;
System.out.print(p + "\t" + q +"\t");
System.out.print(q + "\t" + p + "\t");
System.out.println(p + "\t" + q);
p = 0; q = 1;
System.out.print(p + "\t" + q +"\t");
System.out.print(p + "\t" + q + "\t");
System.out.println(q + "\t" + q);
p = 0; q = 0;
System.out.print(p + "\t" + q +"\t");
System.out.print(p + "\t" + q + "\t");
p = 1; q = 0;
System.out.println(q + "\t" + p);
}
}
This works fine and the table shows the correct values. However, i was wondering, since i've just learned in the book how to convert or cast different types, was the challenge meant to use this? In other words, can i use casting / converting to get the same result? i've tried different things, but it did not work. Or maybe i am thinking to difficult. Thanks for any tips if you have them :). By the way, the original code is:
// Project 2-2: a truth table for the logical operators.
class LogicalOpTable {
public static void main(String args[]) {
boolean p, q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
p = true; q = true;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));
p = true; q = false;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));
p = false; q = true;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));
p = false; q = false;
System.out.print(p + "\t" + q +"\t");
System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));
}
}
I have searched on internet and read some more in the book. But i could not find a casting version or something like that.
Upvotes: 0
Views: 1610
Reputation: 11
if you have to utilize only the stuff from the first two chapters, then the code will probably look like this
package ChapterTwo;
// Try this 2-2: a truth table for the logical operators. package ChapterTwo;
public class LogicalOpTable {
public static void main(String [] args){
boolean p, q;
int x=0;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
p = q = true;
if(p) x=1; System.out.print(x + "\t"); x=0;
if(q) x=1; System.out.print(x + "\t"); x=0;
if (p&q) x=1; System.out.print(x + "\t"); x=0;
if (p|q) x=1; System.out.print(x + "\t"); x=0;
if (p^q) x=1; System.out.print(x + "\t"); x=0;
if (!p) x=1; System.out.println(x + "\t"); x=0;
p = true; q = false;
if(p) x=1; System.out.print(x + "\t"); x=0;
if(q) x=1; System.out.print(x + "\t"); x=0;
if (p&q) x=1; System.out.print(x + "\t"); x=0;
if (p|q) x=1; System.out.print(x + "\t"); x=0;
if (p^q) x=1; System.out.print(x + "\t"); x=0;
if (!p) x=1; System.out.println(x + "\t"); x=0;
p = false; q = true;
if(p) x=1; System.out.print(x + "\t"); x=0;
if(q) x=1; System.out.print(x + "\t"); x=0;
if (p&q) x=1; System.out.print(x + "\t"); x=0;
if (p|q) x=1; System.out.print(x + "\t"); x=0;
if (p^q) x=1; System.out.print(x + "\t"); x=0;
if (!p) x=1; System.out.println(x + "\t"); x=0;
p = q = false;
if(p) x=1; System.out.print(x + "\t"); x=0;
if(q) x=1; System.out.print(x + "\t"); x=0;
if (p&q) x=1; System.out.print(x + "\t"); x=0;
if (p|q) x=1; System.out.print(x + "\t"); x=0;
if (p^q) x=1; System.out.print(x + "\t"); x=0;
if (!p) x=1; System.out.println(x + "\t"); x=0;
}
}
Upvotes: 1
Reputation: 47
I too trying to learn Java from the same book came across this problem and solved it as below.
package ChapterTwo;
// Try this 2-2: a truth table for the logical operators. package ChapterTwo;
public class LogicalOpTable {
public static void main(String[] args) {
boolean p, q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
p = true; q = true;
System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t");
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));
p = true; q = false;
System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t");
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));
p = false; q = true;
System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t");
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));
p = false; q = false;
System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t");
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));
}
public static String printBoolean(boolean p) {
return p ? "1" : "0";
}
public static String printBoolean2(boolean q) {
return q ? "1" : "0";
}
}
Upvotes: 0
Reputation: 2074
Try this in your code....
public String returnedValueFromAnd(Boolean p,Boolean q)
{
if(p&q)
{ return "true";}
return "false";
}
public String returnedValueFromOR(Boolean p,Boolean q)
{
if(p|q)
{ return "true";}
return "false";
}
public String returnedValueFromNegation(Boolean p)
{
if(!p)
{ return "true";}
return "false";
}
public String returnedValueFromXOR(Boolean p,Boolean q)
{
if(p^q)
{ return "true";}
return "false";
}
now call the methods where you want to print the value and pass the parameters p and q (as applicable).
Upvotes: 0
Reputation: 37845
There's no way to cast between boolean
and int
. boolean
is not a numeric type in Java.
The following expressions can be used instead:
boolean b = true;
int i;
i = ( b ? 1 : 0 );
b = ( i != 0 );
Upvotes: 0