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

Printable View

• 07-12-2013, 01:07 AM
YoHeHo
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 :[
• 07-12-2013, 01:29 AM
superhaNds
Re: How do i solve a java precedence PLEEEASE HELP ME :(
use parentheses?
• 07-12-2013, 03:22 AM
kjkrum
Re: How do i solve a java precedence PLEEEASE HELP ME :(
What do you mean, "solve" it?
• 07-12-2013, 04:01 AM
jim829
Re: How do i solve a java precedence PLEEEASE HELP ME :(
What does your book say (you are using a book, right?)

Regards,
Jim
• 07-12-2013, 09:26 AM
JosAH
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
• 07-12-2013, 09:47 AM
khairul
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
• 07-12-2013, 10:12 AM
gimbal2
Re: How do i solve a java precedence PLEEEASE HELP ME :(
2 = Orders, not of.
• 07-12-2013, 11:57 AM
javaBee
Re: How do i solve a java precedence PLEEEASE HELP ME :(
@YoHeHo :
Quote:

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

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); } }```
:o::o::o::o::o::o:
• 07-12-2013, 12:18 PM
JosAH
Re: How do i solve a java precedence PLEEEASE HELP ME :(
Quote:

Originally Posted by javaBee
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
• 07-12-2013, 06:51 PM
jim829
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