DOC PREVIEW
FIU COP 2210 - Java Arithmetic

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java ArithmeticII. How Java Evaluates Arithmetic ExpressionsIII. Integer Division and Modulus (“Mod”)A. Back to the FutureB. Integer DivisionExamples: 10 / 4 = 2 9 / 5 = 1 99 / 100 = 0Unknowingly dividing one integer by another and expecting a “real” result is an extremely common source of logic errors!!!C. Integer ModExamples: 10 % 4 = 2 9 % 5 = 4 99 % 100 = 99For both integer division and modulus, the right-hand operand must be non-zero. Otherwise, the Arithmetic Exception “/ by zero” will be thrown.IV. Self-Check QuestionsComputer Programming I Instructor: Greg ShawCOP 2210 Java ArithmeticArithmetic expressions may contain variables, literals, andarithmetic operators. In order for us to write correctexpressions, we must know how Java will evaluate what we havewritten. Fortunately, Java follows a few simple rules we allremember from Algebra I.I. Arithmetic Operators and Operator PrecedenceOperator precedence means the order in which individualarithmetic operations are performed in an expression havingmore than one operator.Operator Meaning Precedence* , / , %Multiplication,Division,Integer ModulusHighest+ , -Addition,Subtraction,Unary Positive andNegativeLowest There is no exponentiation operator in Java. Exponentiationis done by means of a method defined in Java’s Math class.II. How Java Evaluates Arithmetic Expressions1. Operations of higher precedence are performed before those oflower precedence.2. Operations of equal precedence are performed from left toright.3. Parentheses may be used to override the natural order ofevaluation. Operations in the innermost set ofparentheses are done first. Even if not necessary, parentheses are often used forclarity. This is generally a good idea --- especially incomplex expressions --- because then you needn’t beconcerned with the natural order of evaluation.III. Integer Division and Modulus (“Mod”)A. Back to the FutureTo understand the integer division and mod operations, wemust travel back in time to the second grade, when we firstlearned about “long” division, but were blissfully unaware ofdecimal points and “real” numbers. For example, to divide100 by 16 we would do something like this: 6 r 416 100 96 4Recall that the answer has two parts --- an integer quotientof 6 and an integer remainder of 4.B. Integer DivisionWhen one integer is divided by another integer, the result isthe integer portion of the quotient (only!). Examples: 10 / 4 = 2 9 / 5 = 1 99 / 100 = 0 Unknowingly dividing one integer by another andexpecting a “real” result is an extremely commonsource of logic errors!!!C. Integer ModThe mod operator --- which may be used only with integeroperands --- returns the remainder of an integer division.Examples: 10 % 4 = 2 9 % 5 = 4 99 % 100 = 99 For both integer division and modulus, the right-handoperand must be non-zero. Otherwise, the ArithmeticException “/ by zero” will be thrown.IV. Self-Check QuestionsEach expression below has multiple operators. Above eachoperator, write a number indicating the order in which theoperations will be performed. Then, evaluate each expression.5 * 20 + 8 % 50 – 3 * 6 / 4 + 9 8 % (2 * (5 – 3 * (4 + 6 * (5 / 2)) /


View Full Document
Download Java Arithmetic
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Arithmetic and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Arithmetic 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?