# in need of help (easy)

• 12-30-2009, 08:03 PM
fasck
in need of help (easy)
The problem is easy to solve, i cant figure it out myself tho...:D

i got a table with boolean, it got two vars p and q (p = false q = true)

I want to make the true and the false 0 and 1 instead of p and q

p and q is the var for the true false.

Anyways, i know it has something to do with casting, conversion or the if to get.

the conversion should be boolean to int right? kinda lost...

Help is greatly appreciated
• 12-30-2009, 09:00 PM
pbrockway2
You can't cast: boolean values don't have any intrinsic numerical value or vice versa.

"conversion" is an extremely vague term. Certainly you want to start with false and end up with zero, so you could say the false value is changed or converted. But this just begs the question: how?

You are left with using an if statement. Try that. If you get stuck, post your code and describe what goes wrong (compiler error or output that differs from what you want etc).

If you have studied the ternary ?:, it does something similar to if.
• 12-30-2009, 09:15 PM
fasck
Heres the code

As you see on the first line i have tried with a if statement but i dont know how to connect it with the f = 0

Code:

```// Project 2-2: a truth table for the logical operators. class LogicalOpTable {         public static void main(String args[]) {                 boolean p, q;         int t, f;                 t = 1; f = 0;                         System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");                 [B]p = true; q = false;[/B]         [B]if(p==true); f=0; [/B]         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)); } }```
• 12-30-2009, 11:39 PM
pbrockway2
Quote:

if(p==true); f=0;

Close, but using an if statement it would be

Code:

```if(p) {     f = 0; } else {     f = 1; }```

Notice how if(p) is the same as if(p==true). And how the value of f has to be set for either option. (by the way, didn't you say you wanted zero, not one, for false?)

One way of achieving what you are trying to do would be to use a separate method to do the conversion.

Code:

```public class BooleanDisplay {     public static void main(String[] args) {         boolean p, q;                 System.out.println("P\tQ\tXOR");         p = true;         q = false;         System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(p^q));         p = false;         q = false;         System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(p^q));     }         static int asInt(boolean b) {         if(b) {             return 1;         } else {             return 0;         }             // same thing         //return b ? 1 : 0;     }             // not used, but it could be!     static void display(boolean p, boolean q, boolean r) {         System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(r));     } }```
• 12-30-2009, 11:43 PM
fasck
Thanks alot mate =)
• 12-30-2009, 11:45 PM
pbrockway2
You're welcome.