DOC PREVIEW
UNF COP 2551 - Data and Expressions

This preview shows page 1-2-14-15-30-31 out of 31 pages.

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

Unformatted text preview:

Chapter 2OutlineExpressionsDivision and RemainderArithmetic Expressions - continuedOperator PrecedenceSlide 7Expression TreesAssignment RevisitedSlide 10Increment and DecrementSlide 12Assignment OperatorsSlide 14Slide 15Slide 16Slide 17Data ConversionSlide 19Assignment ConversionSlide 21CastingSlide 23The Scanner ClassScanner Object MethodsScanner ObjectsScanner ObjectsScanner ExampleSample Outputs:We will stop hereSummaryChapter 2Data and ExpressionsPart Two2-2/30OutlineCharacter StringsVariables and AssignmentPrimitive Data TypesExpressionsData ConversionInteractive Programs2-3/30Expressions•An expression is a combination of one or more operators and operands•Arithmetic expressions compute numeric results and make use of the arithmetic operators:Addition +Subtraction -Multiplication *Division /Remainder %•If either or both operands used by an arithmetic operator are floating point, then the result is a floating point2-4/30Division and Remainder•If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded)•The remainder operator (%) returns the remainder after dividing the second operand into the first14 / 3 equals8 / 12 equals4014 % 3 equals8 % 12 equals282-5/30Arithmetic Expressions - continued•If operands are mixed, results are ‘promoted.’ 4.5 + 2 = 6.5 (double)Sometimes called “widened.”2-6/30Operator Precedence•Operators can be combined into complex expressionsresult = total + count / max - offset;•Operators have a well-defined precedence which determines the order in which they are evaluated•Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation•Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order2-7/30Operator Precedence•What is the order of evaluation in the following expressions?a + b + c + d + e1 432a + b * c - d / e3 241a / (b + c) - d % e2 341a / (b * (c + (d - e)))4 1232-8/30Expression Trees•The evaluation of a particular expression can be shown using an expression tree•The operators lower in the tree have higher precedence for that expressiona + (b – c) / da+/- db c2-9/30Assignment Revisited•The assignment operator has a lower precedence than the arithmetic operatorsFirst the expression on the right handside of the = operator is evaluatedThen the result is stored in thevariable on the left hand sideanswer = sum / 4 + MAX * lowest;14 3 2Then the result is stored in the variable on the left hand sideNOTE: the ‘assignment operator (again) IS an operator – (merelyhas lower precedence than arithmetic operators….)2-10/30Assignment Revisited•The right and left hand sides of an assignment statement can contain the same variableFirst, one is added to theoriginal value of countThen the result is stored back into count(overwriting the original value)count = count + 1;KNOW THE OPERATOR PRECEDENCE TABLE ON PAGE 78. It will grow significantly!2-11/30Increment and Decrement•The increment and decrement operators use only one operand•The increment operator (++) adds one to its operand•The decrement operator (--) subtracts one from its operand•The statementcount++;is functionally equivalent tocount = count + 1;2-12/30Increment and Decrement•The increment and decrement operators can be applied in postfix form:count++•or prefix form:++count When used as part of a larger expression, the two forms can have different effects•Because of their subtleties, the increment and decrement operators should be used with care2-13/30Assignment Operators•Often we perform an operation on a variable, and then store the result back into that variable•Java provides assignment operators to simplify that process•For example, the statementnum += count;is equivalent tonum = num + count;2-14/30Assignment Operators•There are many assignment operators in Java, including the following:Operator+=-=*=/=%=Examplex += yx -= yx *= yx /= yx %= yEquivalent Tox = x + yx = x - yx = x * yx = x / yx = x % y2-15/30Assignment Operators The right hand side of an assignment operator can be a complex expression•The entire right-hand expression is evaluated first, then the result is combined with the original variable•Thereforeresult /= (total-MIN) % num;is equivalent toresult = result / ((total-MIN) % num);2-16/30Assignment Operators•The behavior of some assignment operators depends on the types of the operands•If the operands to the += operator are strings, the assignment operator performs string concatenation•The behavior of an assignment operator (+=) is always consistent with the behavior of the corresponding operator (+)2-17/30OutlineCharacter StringsVariables and AssignmentPrimitive Data TypesExpressionsData ConversionInteractive Programs2-18/30Data Conversion•Sometimes it is convenient to convert data from one type to another•For example, in a particular situation we may want to treat an integer as a floating point value•These conversions do not change the type of a variable or the value that's stored in it – they only convert a value as part of a computation2-19/30Data Conversion•Conversions must be handled carefully to avoid losing information•Widening conversions are safest because they tend to go from a small data type to a larger one (such as a short to an int)•Narrowing conversions can lose information because they tend to go from a large data type to a smaller one (such as an int to a short)•In Java, data conversions can occur in three ways:assignment conversionpromotioncasting2-20/30Assignment Conversion•Assignment conversion occurs when a value of one type is assigned to a variable of another•If money is a float variable and dollars is an int variable, the following assignment converts the value in dollars to a floatmoney = dollars•Only widening conversions can happen via assignment (I do NOT believe this to be true!)(I do NOT believe this to be true!)•Note that the value or type of dollars did not change2-21/30Data Conversion•Promotion happens automatically when operators in expressions convert their operands•For example, if sum is a float and count is an int, the value of count is converted to a floating point value to perform the following calculation:result = sum / count;•Result is stored in result.•BUT count is still an int. It was promoted in order


View Full Document

UNF COP 2551 - Data and Expressions

Download Data and Expressions
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 Data and Expressions 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 Data and Expressions 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?