Chapter 1: Hardware and Software Notes

advertisement
Computer Science 1 Notes: Data 2
Expressions
An expression is a combination of literals, operators, variable names, and parentheses used to
calculate a value.
The parts of an expression must be arranged correctly. The syntax of Java describes the correct
arrangements. The rules for correct Java expressions are about the same as for algebra.
We need to talk about operators and operands. You already know what an operator is (a symbol such
as +, -, *, or / that calls for an arithmetic operation).
An operand is a value that is acted upon by an operator.
For example, in 13 - 5 the 13 and the 5 are the operands and the - is the operator.
Arithmetic Operators
Arithmetic expressions are especially important. As
you have seen, Java has many operators for
arithmetic:
All of these operators can be used on floating point
numbers and on integer numbers. (However, the %
operator is rarely used on floating point.) For
instance, / means integer division if both operands
are integers and means floating point division if
one or both operands are floating point.
Weird Integer Arithmetic
Operator
Meaning
precedence
-
unary minus
highest
+
unary plus
highest
*
multiplication
middle
/
division
middle
%
remainder
middle
The division operator / means integer division if
+
addition
low
there is an integer on both sides of it. If one or two
sides have a floating point number, then it means
subtraction
low
floating point division. The result of integer
division is always an integer; if the exact result
calls for a decimal fraction, that part of the result is dropped (not rounded).
There is a difference between what Java will do and what a calculator will do. A calculator will do
floating point arithmetic for the expression:
7/2
Java will regard this as integer arithmetic and give you:
7/2 = 3
This is easy enough to see when it is by itself, but here is a more confusing case of the same thing:
1.5 + 7/2
The division will be done first, because / has higher precedence than +. The result, 3, is an integer.
Now floating point 1.5 is added to integer 3 to get floating point 4.5.
Integer arithmetic may be used in parts of an expression and not in others, even though the final
value of the expression is floating point. Most programming languages do this. Believe it or not, it is
fortunate that Java works this way. It would be awful if what was done in the middle of a calculation
depended on the final result.
If you really want to add one half to one half you should write 1.0/2.0 + 1.0/2.0 because
now the decimal points make each number a double. Here is a Java application that illustrates these
points:
class IntegerDivision
{
public static void main ( String[] args )
{
System.out.println("The result is: " + (1/2 + 1/2) );
}
}
You can run this program by copying it to a file IntegerDivision.java, compiling and
running it. Notice the parentheses around (1/2 + 1/2). These are necessary so that the
arithmetic is done first, then the result is converted to characters and appended to the string.
Mixed Floating Point and Integer Expressions
But what if one operand is an integer and the other is a floating point? The rule is:
If both operands are integers, then the operation is an integer operation. If any operand is floating
point, then the operation is floating point.
Mixed Expression Gotcha!
Look again at the rule:
If both operands are integers, then the operation is an integer operation. If any operand is floating
point, then the operation is floating point.
The rule has to be applied step-by-step. Consider this expression:
( 1/2 + 3.5 ) / 2.0
What is the result? Apply the rules: innermost parentheses first, within the parentheses the highest
precedence operator first:
( 1/2 + 3.5 ) / 2.0
--do first
Since both operands are integer, the operation is integer division, resulting in:
( 0 + 3.5 ) / 2.0
Now continue to evaluate the expression inside parentheses. The + operator is floating point because
one of its operands is, so the parentheses evaluates to 3.5:
3.5 / 2.0
Finally do the last operation:
1.75
This is close to the result that you might have mistakenly expected if you thought both divisions were
floating point. An insidious bug might be lurking in your program!
Remainder Operator
You may recall how in grade school you did division like this:
13 / 5 == 2 with a remainder of 3. This is because 13 == 2*5 + 3. The symbol for finding the
remainder is % (percent sign). This symbol is also called the modulo operator. If you look in the table
of operators you will see that it has the same precedence as
worth study:
/ and *. Here is a program that is
class RemainderExample
{
public static void main ( String[] args )
{
int quotient, remainder;
quotient =
remainder =
17 / 3;
17 % 3;
System.out.println("The quotient : " + quotient );
System.out.println("The remainder: " + remainder );
System.out.println("The original : " +
(quotient*3 + remainder) );
}
}
Copy this program to a file and play with it. Change the 17 and 3 to other numbers and observe the
result.
Taking an Integer Apart
The integer division operator / and the remainder operator % take an integer apart.
theInteger / divisor ——> quotient
theInteger % divisor ——> remainder
// Example 20/3 is 6
// Example 20%3 is 2
The original integer can be put back together again:
quotient * divisor + remainder
——> theInteger
// 6 * 3 + 2 = 20
In many calculations, it is convenient to do everything with integers, so both / and % are needed.
For positive numbers, int % X means to fit as many X as you can into int, and then the left over
amount is the value of the expression.
Remainder with Negative Integers
The remainder operator can be used with negative integers. The rule is:
1. Perform the operation as if both operands were positive.
2. If the left operand is negative, then make the result negative.
3. If the left operand is positive, then make the result positive.
4. Ignore the sign of the right operand in all cases.
For example:
17 % 3 == 2
17 % -3 == 2
-17 % 3 == -2
-17 % -3 == -2
Constants
Often in a program you want to give a name to a constant value. For example you might have a tax
rate of 0.045 for durable goods and a tax rate of 0.038 for non-durable goods. These are constants,
because their value is not going to change when the program is executed. It is convenient to give
these constants a name. This can be done:
class CalculateTax
{
public static void main ( String[] arg )
{
final double DURABLE = 0.045;
final double NONDURABLE = 0.038;
. . . . . .
}
}
The reserved word final tells the compiler that the value will not change. The names of constants
follow the same rules as the names for variables. (Programmers sometimes use all capital letters for
constants; but that is a matter of personal style, not part of the language.) Now the constants can be
used in expressions like:
taxamount = gross * DURABLE ;
But the following is a syntax error:
DURABLE = 0.441;
// try (and fail) to change the tax rate.
In your programs, use a named constant like DURABLE rather than using a literal like 0.441. There
are two advantages in doing this:
1. Constants make your program easier to read and check for correctness.
2. If a constant needs to be changed (for instance if the tax rates change) all you need to do is
change the declaration of the constant. You don't have to search through your program for
every occurrence of a specific number.
Download