Java help

Gamah

Banned
Joined
Dec 22, 2003
Messages
13,042
I have written a table of results for 2 booleans after applying logical opperators, but I need to the results to display 1, 0 in instead of true, false using casting.

code is as follows:

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));

}
}

Any ideas?
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
sounds challenging, ask me in 4years after i go uni etc and i may be able to help you :D
 

Himse

FH is my second home
Joined
Jan 31, 2004
Messages
2,179
what did you just say?

i didn't get any of that :[
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,988
Okay, coders feel free to slate me.. But something like this? (It's 2:30am)

If (p = true)
{
System.out.print("1");
}
else
{
System.out.print("0");
}

Totally not done programming in fucking ages, so that's more than likely totally fubar. But you get the idea.. Or use a switch and add in the conditions?
 

Manisch Depressiv

Part of the furniture
Joined
Mar 6, 2005
Messages
7,727
If this

Code:
boolean b = true;
int i = (int) p;

doesn't work and it does not, because boolean and int are inconvertible types, then a short solution is

Code:
boolean b = true;
int i = b ? 1 : 0;

but it isn't casting, it's working with a conditional operator.

Code:
i = b ? 1 : 0;

is the same as

Code:
if (b)
  i = 1;
else
  i = 0;

I don't know if this exercise was a kind of a homework or so, maybe it's a trick to say you got to do it with casting.
 

crispy

Can't get enough of FH
Joined
Mar 9, 2004
Messages
2,706
Yeah, you cant cast boolean to integers :>

You'll need to make a method something like this:

Code:
private int booleanConverter(boolean b) {
 if b = true
 return 1
 else
 return 0
 else return null //just a precaution, you never know :)
}

Then use this method in your System.out lines like this:

Code:
System.out.println(booleanConverter((p^q)) + "\t" + booleanConverter((!p)));

Im not 100% sure bout my syntax, havent coded for a long while, but i think you get my idea :)

edit: but i dont get why you cant use the true/false output :D

Its just System.out anyways :p
 

Jeremiah

Fledgling Freddie
Joined
Aug 10, 2004
Messages
1,131
Crispy's way will work! Just remember the brackers round the if statements and the semicolons =)
 

Users who are viewing this thread

Top Bottom