Transcript expression

Problem Solving
with Java™
Second Edition
Elliot Koffman and
Ursula Wolz
Prepared by: Elliot Koffman, Temple University and Dorina Petriu, Carleton University
Copyright
Copyright©©2003
2003Pearson
PearsonEducation,
Education,Inc.
Inc.
Slide 2-1
2.2 Processing numeric data
Arithmetic
operator
Meaning
Example
+
Addition
5 + 2 is 7
5.0 + 2.0 is 7.0
–
Subtraction
5 - 2 is 3
5.0 - 2.0 is 3.0
*
Multiplication 5 * 2 is 10
5.0 * 2.0 is 10.0
/
Division
5.0
5 /
5.0
5 /
%
Remainder
7 % 4 is 3 (use only w/ int)
4 % 7 is 4
Copyright © 2003 Pearson Education, Inc.
/ 2.0 is 2.5
2.0 is 2.5
/ 2 is 2.5
2 is 2 (integer division)
Slide 2-2
Integer Division and Remainder
• int data type
+ - * / = (assignment)
% Used only with integers, gives remainder
• Examples of integer division and remainder
15 / 3 = 5, 15 % 3 = 0
15 / 2 = 7, 15 % 2 = 1
2 / 15 = 0, 2 % 15 = 2
299 / 100 = 2, 299 % 100 = 99
Copyright © 2003 Pearson Education, Inc.
Slide 2-3
Statements and Expressions
• Statement: an instruction that performs an
operation. There are two kinds of
statements:
– data declarations - tell the Java compiler what
kind of storage locations to allocate
– executable statements - instruct the computer
how to process the information in storage
• Expression: Java code that produces a
result. Expressions are used mostly as parts
of statements.
Copyright © 2003 Pearson Education, Inc.
Slide 2-4
Assignment Statement
• Form: variable = expression;
• Example: x = y + z;
• Interpretation: If variable is declared as a primitive type, the
value of the expression is stored in variable. If variable is a
declared as a class type, a reference to the object formed by
expression is stored in variable.
• The previous value of variable is lost.
• The expression can be a variable, a constant, a literal,
a method call returning a value, or a combination of
the above connected by appropriate operators (such as
+, -, * and / ).
Copyright © 2003 Pearson Education, Inc.
Slide 2-5
Assignment Compatability
• In an assignment statement, the value of the
expression must be assignment compatible with the
variable, meaning that:
• Either the data types of the expression and variable are
the same, or
• The expression’s type can be converted to the variable’s
type. If this condition is met, the conversion (which is
named assignment conversion) is performed
automatically.
Copyright © 2003 Pearson Education, Inc.
Slide 2-6
Effect of assignment statements
• sum = sum + item;
before assignment: sum = 25.0; item = 5.0
after assignment: sum = 30.0; item = 5.0
• newx = -x;
before assignment: newx = 0.0; x = -5.123;
after assignment: newx = 5.123; x = -5.123;
Copyright © 2003 Pearson Education, Inc.
Slide 2-7
Data Type of Arithmetic Operation
• The type of the result of an arithmetic
operation is double if an operand is type
double. e.g., 1+ 2.0 is 3.0
• If both operands are type int, then the
result is type int. e.g., 1 / 2 is 0.
Copyright © 2003 Pearson Education, Inc.
Slide 2-8
Mixed-type assignments
•
•
•
•
Mixed-type assignment statement: the assignment of an expression of one type
to a variable of another type.
Assignment conversion: automatic conversion of the expression value (after
the expression was completely evaluated) to the variable type. Such a
conversion must be a widening one (e.g., from int to double).
int m = 3;
int n = 2;
double x, y;
y = m + n;
assignment conversion: y becomes 5.0 (not 5)
x = y + m / n; m / n is 1 (not 1.5), 5.0 + 1 is 6.0, assign 6.0 to x
Example of invalid assignments due to possible loss of the fractional part
(narrowing conversion is not allowed)
int count;
count = 3.6;
invalid: can’t assign double to int
count = count + 1.0; invalid: expr. result is double
Error: Incompatible type for =. Explicit cast needed to convert double to
int.
Copyright © 2003 Pearson Education, Inc.
Slide 2-9
Type casting
•
•
Casting: the most general form of conversion in Java. A cast is a Java operator
specified by a type in parentheses, that is applied to the value of an expression.
Type casting syntax:
Form: (type) value
Example: double cost; int dollars;
dollars = (int) cost;
Interpretation: The cast in the example creates an int value by converting
cost to an integer (which truncates any fractional part). The content of cost
remains unchanged.
More type casting examples:
int count;
count = (int) 3.6;
the cast operator creates an int value (i.e. 3),
which is assigned to count
int m = 7; int n = 2;
double x;
• the cast operator creates a double value (i.e., 7.0)
x = (double) m / n;
• n is converted to a double by arithmetic promotion
• division produces the result 3.5, which is assigned to x
int m = 7; int n = 2;
double x;
• the integer division 7 / 2 gives the result 3
x = (double) (m /n);
• the cast operator creates a double value (i.e., 3.0),
which is assigned to x
Copyright © 2003 Pearson Education, Inc.
Slide 2-10
Rules for Evaluating Expressions
• Parentheses rule: Evaluate expressions in
parentheses separately. Evaluate nested parens from
the inside out.
• Operator precedence rule: Operators in the same
expression are evaluated in the order determined by
their precedence (from the highest to the lowest).
method
call
Operator
- (unary)
new, type cast
*, /, %
+, - (binary)
=
highest
precedence
Precedence
lowest precedence
• Left associative rule: Operators in the same
expression and at the same precedence level are
Copyright © 2003 Pearson Education, Inc.
Slide 2-11
Evaluation of z – (a + b / 2) * w / y
z - (a + b / 2) * w / y Operator, reason evaluated
--/--/, parens and precedence
---+------+, parens
-------------*-*, precendence, left assoc.
------------------/-- /, precedence
-- - -------------------- -
Copyright © 2003 Pearson Education, Inc.
Slide 2-12
Mathematical Formulas in Java
• a = bc not valid Java syntax
– insert * Operator
a = b * c;
• m=y-b
x-a
– insert ( ) and /
Copyright © 2003 Pearson Education, Inc.
m = (y - b) / (x - a);
Slide 2-13
Class TwoNumbers
public class TwoNumbers {
public static void main(String[] args) {
double num1 = 8;
double num2 = 6;
System.out.println("First number is " + num1);
System.out.println("Second number is " + num2);
System.out.println("Sum is " + (num1 + num2));
System.out.println("Difference is " + (num1 - num2));
System.out.println("Product is " + (num1 * num2));
System.out.println("Quotient is " + (num1 / num2));
}
}
Copyright © 2003 Pearson Education, Inc.
Slide 2-14
Sample run of class TwoNumbers
Copyright © 2003 Pearson Education, Inc.
Slide 2-15