Results 1 to 9 of 9
Thread: Boolean help
- 04-11-2011, 06:05 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 43
- Rep Power
- 0
Boolean help
For my programming 1 class we are learning java and he told us to display this boolean expression
p+q((!p||q)&&(!q||p))
this is what i have so far but it keeps giving me an error saying it can't find q...what am i doing wrong? any help is greatly appreciated
public class booleans
{
public static void main(String[] args)
{
boolean p;
boolean q;
p=false;
q=false;
for(int i=0; i<=1; i++){
for(int j=0; j<=1; j++)
System.out.println(p+q((!p||q)&&(!q||p)));
}
}
}
- 04-11-2011, 06:20 PM #2
I'm curious, why are you not initializing your variable in the declaration and why are you putting a loop inside of a loop to display the same output each time?
Your code would look much better like this:
Java Code:public class booleans { public static void main(String[] args) { boolean p = false; boolean q = false; System.out.println(p+q((!p||q)&&(!q||p))); } }
When you just use ((!p||q)&&(!q||p)) it works just fine. However you can't use the +, -, * or / in a boolean expression. That's why you're getting your error.
EDIT2: Ok, so after consulting google about boolean functions you're trying to use operations that aren't supported in that function by Java. You have to change the q( with a && for multiplication and the p + q with a || according to google.
Addition is represented as OR in boolean algebra and multiplication is represented as AND.
Thus if your formula is System.out.println(p||q&&(!p||q)&&(!q||p)); it will work.Last edited by Dark; 04-11-2011 at 06:42 PM.
- 04-11-2011, 06:22 PM #3
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
what do you exactly mean by :
(p+q((!p||q)&&(!q||p)))Last edited by baf06; 04-11-2011 at 06:24 PM.
Click on REP and add to member reputation, if you find their advices/solutions effective.
- 04-11-2011, 06:25 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
You do have a local variable q but no method with the same name, i.e. q( ... ). The Java compiler considers that notation a method call. Most likely the expression should read: p+q&&((!p||q)&&(!q||p)). Java doesn't know implicit multiplications or conjunctions.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 04-11-2011, 06:27 PM #5
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
JosAh this will cause another error,
operator + cannot be applied to boolean,booleanClick on REP and add to member reputation, if you find their advices/solutions effective.
- 04-11-2011, 06:30 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 43
- Rep Power
- 0
all the symbols within it are supposed to be boolean...
for example the & means and the ! means not the | means or, etc...
and as for your post dark that is how the professor had it listed but for some reason he likes to put errors within the code for me to try and find even though i have no background whatsoever in java...
the output is supposed to be
false false true
false true false
true false false
true true true
i don't understand it at all and i tried darks suggestion and i got the same error... :eek:
- 04-11-2011, 06:36 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 43
- Rep Power
- 0
- 04-11-2011, 06:50 PM #8
Look at my EDIT2, I don't believe its supposed to be p&&q&& in the front. According to what I found from google Multiplication is && and Addition is ||.
EDIT To avoid double posting: I put in the for loops you had into my code and the just looped the display on the output. It just displayed False 4 times. I'm not sure how you're supposed to get:
false false true
false true false
true false false
true true true
Out of your current code. You would have to change a variable to true or false every time it was looped.Last edited by Dark; 04-11-2011 at 06:57 PM.
- 04-11-2011, 09:41 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 43
- Rep Power
- 0
Similar Threads
-
use boolean as 0 or 1
By joost_m in forum New To JavaReplies: 10Last Post: 04-13-2010, 11:22 AM -
Boolean value not working?
By zerkz in forum New To JavaReplies: 3Last Post: 09-29-2009, 06:42 AM -
Boolean problems
By Chasingxsuns in forum New To JavaReplies: 2Last Post: 09-15-2009, 10:57 PM -
transfer boolean to 1's and 0's
By Nikohw in forum Java AppletsReplies: 5Last Post: 09-12-2009, 09:05 PM -
Boolean Expression
By ritwik07 in forum New To JavaReplies: 3Last Post: 07-11-2007, 04:11 AM
Bookmarks