Presentation on theme: "Computation u When processing data we subject it to a variety of transformations or computations. u The type of data determines the set of valid operations."— Presentation transcript:
Computation u When processing data we subject it to a variety of transformations or computations. u The type of data determines the set of valid operations. u Arithmetic would be a typical operation on numeric data. u As we have seen, with strings we often split large values into smaller units or join two or more pieces of text together. u An object has a set of operations defined by its class.
Operators for int Data Types u The following operators are defined for the int data types :- +addition -subtraction *multiplication /division yielding quotient (e.g. 8/5 = 1) % division yielding remainder (e.g. 8%5 = 3)
Operators for int Data Types u The / and % operators deserve special mention. u If I and J are integers then I / J is the quotient obtained when I is divided by J with the fractional portion dropped when the result is not an integer. u If I and J are integers then I % J is the remainder after dividing I by J. u What is the result of u 8/5 u 8%5 u N%M
Operators for float u The following operators are defined for the float and double data types :- + addition - subtraction * multiplication / division u The essential difference between float and int expressions is the type of the result. u Also, the division operator behaves differently. u Obviously there is no % for floats.
Problems with Expressions u A problem sometimes arises with the evaluation of expressions. u Consider the statement SomeVariable = 4 + 3 * 7 ; u The value assigned depends on how the computation proceeds. Strategy 1. Evaluate left to right 4 + 3 = 7 * 7 = 49 Strategy 2. Do multiplication first 4 + (3 * 7) = 4 + 21 = 25 u To avoid confusion Java applies the standard mathematical rules for expression evaluation.
u When Java encounters an expression involving several operators it evaluates them in the following order OperatorMeaningOrder -Unary minusRight to Left *MultiplicationLeft to Right /Division %Modulus +AdditionLeft to Right -Subtraction Operator Precedence
Using Brackets in Expressions u Brackets may be used to change the order of evaluation. For example AverageTemp = HighTemp + LowTemp / 2 should be written AverageTemp = (HighTemp + LowTemp) / 2 u Brackets may also be used to emphasise the order of evaluation. For example SomeVariable = 4 + (3 * 7 )
Java ShortCuts - Assignment u We have seen several examples of the importance and frequency of occurrence of counting in algorithms. u Some solutions have included statements of the type count = count + 1 ; u This type of tedious variable name duplication tends to encourage cryptic variable naming.
Java ShortCuts - Assignment u The Java language designers have solved this problem by including an Increment operator, a Decrement operator and a set of Combined Operators all of which can be used to abbreviate assignment statements. u It should be noted that the exploitation of shortcuts for frequently occurring operations is widespread in programming languages. u We are interested in how Java does it but other language designers have used similar but different solutions.
Java ShortCuts - Assignment u Java uses ++ as an Increment operator. u Java uses -- as a Decrement operator. u Thus Count++ ; is equivalent to Count = Count + 1 ; Count-- ; is equivalent to Count = Count - 1 ; i++ ; is equivalent to i = i +1 u The Increment and Decrement operators are exploited extensively by Java programmers so it is worthwhile becoming familiar with them.
Java ShortCuts - Assignment u Obviously, the Increment and Decrement operators only substitute for the addition and subtraction operators and only allow the variables to be incremented or decremented by 1. u The Combined Operators provide a more general mechanism for abbreviating assignment statements which involve the same variable name on both sides of the assignment operator.
C ShortCuts - Assignment u When using the Combined Operators it is important to note that the variable to the left of the assignment operator is applied to the complete expression on the right. u For example Salary *= PercentageIncrease + 1 is equivalent to Salary = Salary * (PercentageIncrease + 1) and not Salary = Salary * PercentageIncrease + 1
C ShortCuts - Assignment u Frequently we encounter situations where a collection of variables have to be assigned the same value. u We can of course code individual statements for these assignments, however, Java provides a multiple assignment feature which allows the same value to be assigned to a collection of variables. Thus a = b = c = 6 height = width = depth = 12