# 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 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.

Java ShortCuts - Assignment OperatorExampleEquivalent +=Total += CountTotal=Total + Count N += 5N = N + 5 -=Total -= CountTotal=Total - Count N -= 5N = N - 5 *=Salary *= 1.1Salary=Salary * 1.1 /=Loot /= GangSizeLoot=Loot/GangSize %=Pence %= 100Pence = Pence % 100

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

Download ppt "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."

Similar presentations
The content of the passage is a presentation on computation, focusing on the operations and transformations that can be applied to different data types in programming, particularly in Java. It covers arithmetic operations, operator precedence, and the use of shortcuts for assignment, such as increment and decrement operators and combined operators.
multiple_questions_with_answers

the passage contains multiple questions and their answers, such as:
- "what is the result of 8/5?" (answer: 1)
- "what is the result of 8%5?" (answer: 3)
- "what is the result of n%m?" (no specific answer provided, but the concept is explained)
- "consider the statement somevariable = 4 + 3 * 7 ;" (the correct evaluation is explained)
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# 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 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.

Java ShortCuts - Assignment OperatorExampleEquivalent +=Total += CountTotal=Total + Count N += 5N = N + 5 -=Total -= CountTotal=Total - Count N -= 5N = N - 5 *=Salary *= 1.1Salary=Salary * 1.1 /=Loot /= GangSizeLoot=Loot/GangSize %=Pence %= 100Pence = Pence % 100

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

Download ppt "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."

Similar presentations
user
The content of the passage is a presentation on computation, focusing on the operations and transformations that can be applied to different data types in programming, particularly in Java. It covers arithmetic operations, operator precedence, and the use of shortcuts for assignment, such as increment and decrement operators and combined operators.

TYPE: MULTIPLE_QUESTIONS_WITH_ANSWERS

The passage contains multiple questions and their answers, such as:
- "What is the result of 8/5?" (Answer: 1)
- "What is the result of 8%5?" (Answer: 3)
- "What is the result of N%M?" (No specific answer provided, but the concept is explained)
- "Consider the statement SomeVariable = 4 + 3 * 7 ;" (The correct evaluation is explained)