Results 1 to 10 of 10
  1. #1
    YoHeHo is offline Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Unhappy How do i solve a java precedence PLEEEASE HELP ME :(

    hi ...

    i need someone to help to a problem..

    4+3*2/3-7*3+15%2*7/2+1

    how could i solve this and what should i do first... please help me :[

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    use parentheses?

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    What do you mean, "solve" it?
    Get in the habit of using standard Java naming conventions!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    What does your book say (you are using a book, right?)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    Read one of my blog articles (see the link near the top-right of this reply); the article discusses a fairly complete expression parser and several evaluators (all code is included). You can also do it the lazy way and let the embedded Javascript engine do the nitty-gritty work.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    khairul is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    one acronym: BODMAS

    1. Brackets
    2. Of
    3. Divide
    4. Multiplication
    5. Addition
    6. Subtration

    Precedence indicated by the numbers

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,277
    Rep Power
    6

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    2 = Orders, not of.

  8. #8
    javaBee is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    @YoHeHo :
    Use the BODMAS principle
    I think your equation can be re write to this form ::

    4+(3*(2/3))-(7*3)+((15/2)*(7/2))+1

    I wrote a java code for you, i also didn't understand what do you mean by the word "solve" here
    Java Code:
    import java.io.*;
    class operation
    {
    public static void main(String args[])
    {
    float a=4+(3*(2/3))-(7*3)+((15/2)*(7/2))+1;
    
    System.out.println("Result : "+a);
    }
    }

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

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    Quote Originally Posted by javaBee View Post
    Java Code:
    import java.io.*;
    class operation
    {
    public static void main(String args[])
    {
    float a=4+(3*(2/3))-(7*3)+((15/2)*(7/2))+1;
    
    System.out.println("Result : "+a);
    }
    }
    Did you test your code? What was the answer? Why?

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: How do i solve a java precedence PLEEEASE HELP ME :(

    The BODMAS principle doesn't apply to Java. For example, division and multiplication have the same precedence so they then evaluate from left to right in the absence of parens.

    int a = 2/3*3; // evaluates to 0
    int b = 3*2/3; // evaluates to 2

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Need help with setting operator precedence.
    By chris1 in forum New To Java
    Replies: 1
    Last Post: 10-13-2011, 09:13 AM
  2. About precedence of methods
    By RichersooN in forum New To Java
    Replies: 12
    Last Post: 06-22-2011, 09:30 AM
  3. How to i solve this java senario??
    By Rachel_B in forum New To Java
    Replies: 7
    Last Post: 12-16-2010, 07:47 PM
  4. Replies: 5
    Last Post: 08-13-2010, 11:04 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
  •