Results 1 to 9 of 9

Thread: Boolean help

  1. #1
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

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

    }

    }

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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)));
        }
    }
    EDIT: I put the code into my editor to see what errors were popping up and its saying "q(boolean) is undefined for the type booleans." So the problem is its trying to put everything after the first q into a method. Since q doesn't have any methods you're going to have to review your boolean expression. My question is what does q( mean in a true or false? Its been a while since I took geometry so I'm lost as to what the expression is trying to represent.

    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 07:42 PM.

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    what do you exactly mean by :
    (p+q((!p||q)&&(!q||p)))
    It is the first time for me to perform mathematical operations with booleans, q is declared but not q(//something) i think your problem
    Last edited by baf06; 04-11-2011 at 07:24 PM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DMarsh12 View Post
    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)));
    }

    }

    }
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    JosAh this will cause another error,
    operator + cannot be applied to boolean,boolean
    maybe he means to write : p && q && (!p||q) && (!q||p) this will work
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  6. #6
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by baf06 View Post
    what do you exactly mean by :


    It is the first time for me to perform mathematical operations with booleans, q is declared but not q(//something) i think your problem
    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:

  7. #7
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    Quote Originally Posted by baf06 View Post
    JosAh this will cause another error,


    maybe he means to write : p && q && (!p||q) && (!q||p) this will work

    yes i did both of your suggestions and it worked...thank you very much +rep to the both of you

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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 07:57 PM.

  9. #9
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    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.
    yeah i have been getting the same output as well...i'm going to ask the professor tomorrow for some help with it and post back the correct form. thank you for the help tho.. +rep

Similar Threads

  1. use boolean as 0 or 1
    By joost_m in forum New To Java
    Replies: 10
    Last Post: 04-13-2010, 12:22 PM
  2. Boolean value not working?
    By zerkz in forum New To Java
    Replies: 3
    Last Post: 09-29-2009, 07:42 AM
  3. Boolean problems
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 09-15-2009, 11:57 PM
  4. transfer boolean to 1's and 0's
    By Nikohw in forum Java Applets
    Replies: 5
    Last Post: 09-12-2009, 10:05 PM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •