copyright © 2003 pearson education, inc. slide 2-1 problem solving with java™ second edition...
TRANSCRIPT
Copyright © 2003 Pearson Education, Inc. Slide 2-1
Problem Solving with Java™Second Edition
Elliot Koffman and Ursula Wolz
Copyright © 2003 Pearson Education, Inc.
Prepared by: Elliot Koffman, Temple University and Dorina Petriu, Carleton University
Copyright © 2003 Pearson Education, Inc. Slide 2-2
2.2 Processing numeric data
Arithmeticoperator Meaning Example
+ Addition 5 + 2 is 75.0 + 2.0 is 7.0
– Subtraction 5 - 2 is 35.0 - 2.0 is 3.0
* Multiplication 5 * 2 is 105.0 * 2.0 is 10.0
/ Division 5.0 / 2.0 is 2.5 5 / 2.0 is 2.5 5.0 / 2 is 2.5 5 / 2 is 2 (integer division)
% Remainder 7 % 4 is 3 (use only w/ int)4 % 7 is 4
Copyright © 2003 Pearson Education, Inc. Slide 2-3
Integer Division and Remainder
• int data type+ - * / = (assignment)
% Used only with integers, gives remainder
• Examples of integer division and remainder15 / 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-4
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-5
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-6
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-7
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-8
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-9
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 intcount = 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-10
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;
int m = 7; int n = 2; double x;x = (double) m / n;
int m = 7; int n = 2; double x;x = (double) (m /n); • the cast operator creates a double value (i.e., 7.0)
• n is converted to a double by arithmetic promotion• division produces the result 3.5, which is assigned to x
the cast operator creates an int value (i.e. 3), which is assigned to count
• the integer division 7 / 2 gives the result 3• the cast operator creates a double value (i.e., 3.0),
which is assigned to x
Copyright © 2003 Pearson Education, Inc. Slide 2-11
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). Operator Precedence
• Left associative rule: Operators in the same expression and at the same precedence level are evaluated in left-to-right order.
method call highest precedence- (unary)new, type cast*, /, %+, - (binary)= lowest precedence
Copyright © 2003 Pearson Education, Inc. Slide 2-12
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-13
Mathematical Formulas in Java
• a = bc not valid Java syntax– insert * Operator a = b * c;
• m = y - b
x - a– insert ( ) and / m = (y - b) / (x - a);
Copyright © 2003 Pearson Education, Inc. Slide 2-14
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-15
Sample run of class TwoNumbers