Page 157 - Cs_withBlue_J_C11_Flipbook
P. 157

Some Solved Examples:
                 If int a=5, b=6, c=7, d;
                 a.  d  =  a%b/c*b;
                     =  5%6/7*6
                     =  5/7*6
                     =  0*6
                     =  0
                 b.  d  =  a+b-c+a;
                     =  5+6-7+5
                     =  11-7+5
                     =  4+5
                     =  9


                     7.5 ARITHMETICAL EXPRESSION AND STATEMENT
                 While doing calculations in Java, we come across the term Java expression. Before understanding what is it and how
                 it differs from a mathematical expression, we must understand the difference between arithmetical expression and
                 arithmetical statement.

                 Let us see an example:
                                                                 c = a+b;
                 Here, a + b is said to be an arithmetic expression and c = a + b; is called an arithmetic statement.

                                    Arithmetic Expression                        Arithmetic Statement

                         Any meaningful  statement containing  identifiers,  When an  arithmetical  expression  is  assigned  to  a
                         literals and arithmetical operators which can produce  variable, then it is called an arithmetical statement.
                         a result is called an arithmetical expression.
                         Examples: a * b, 2 * (l + b)                 Examples: c = a * b, p = 2 * (l + b)

                 7.5.1 Interconversion of Mathematical Expression and Java Expression
                 We are already familiar with the fact that we cannot write a mathematical expression in a Java program. We must
                 follow certain rules and convert a mathematical expression into a Java expression. The following table demonstrates
                 some examples of mathematical expressions and their equivalent Java expressions:

                                   Mathematical Expression                          Java Expression
                         pr 2                                         22.0/7.0 * r * r
                          2
                         3x + 2y                                      3 * x * x + 2 * y
                         prt
                         100                                          (p * r * t) / 100.0
                         ut+1/2at 2                                   (u * t) + (1.0 / 2.0 * a * t * t)

                         T = 2p√Lg                                    T = 2 * 22.0 / 7.0 * Math.sqrt (L) * g
                           1                                          E = 1.0 / 2.0 * m * v * v
                         E= mv 2
                           2
                         1     1                                      1.0 / 3.0 * a * b + 1.0 / 2.0 * c * d
                               2
                         3 ab +  cd
                         ab+bc+ca                                     a * b + b * c + c * a

                            b
                         d =   -+ b 2 -4ac                            d = (-b + Math.sqrt(b * b - 4 * a * c))/(2 * a)
                               2 a


                                                                                                                       155
                                                                                               Variables and Expressions  155
   152   153   154   155   156   157   158   159   160   161   162