COP 2551 Introduction to Object Oriented Programming with Java Topics –Java Statements –Java Expressions –Postfix Expressions –Prefix Expressions –Evaluating Expressions –Order of Operations –Data Type Conversion Lecture 3
Java Statements (section 2.2) Java program comprised of the following –Source Code Analogous with a document such as Word –Blocks of statements Analogous with a paragraph of a document –Statements Analogous with a sentence within a paragraph –Keywords, variables, constants and function calls Analogous with the words in a sentence
Java Statements (section 2.2) 4 types of statements in a Java program –Null statement Performs no action ; // Has no affect on the program –Expression statement Performs one of the following actions –Declare a variable int count; –Assign a value count = 0; –Call a function count = getCount(); –Return statement Returns a value from a function –Value returned to the calling statement return 0;
Java Statements (section 2.2) 4 types of statements in a Java program –Compound statements A collection of multiple statements –On a single line int count = 0; count = 2 + 1; displayCount(count); –1 statement “nested” within another statement displayCount(getCount()); –Block of statements that complete a specific task »Such as a the statements within a method public static double calcArea(int length, int width) { double area; area = length * width; return area; } A semicolon “;” indicates the end of most statements –Curly braces “{ }” indicate the start and end of a block of statements
Java Expressions (section 2.4) Expressions consists of 2 items –1 or more operands Variables Constants Function calls –1 or more operators i.e., =, +, -, *, /, %, ( and ) The “=“ operator is referred to as the assignment operator –It is used to “assign” a value to a variable Examples: int ans = * 5 / 1 – 4; // ans = 3 int ans = (2 + 1) * 5 / (1 – 4); // ans = -5
Java Expressions (section 2.4) Compound assignments –A common operation is to update the value of a variable Examples int count = 0; count = count + 10; // count = 10 count = count - 5; // count = 5 count = count * 3; // count = 15 count = count / 4; // count = 3 count = count % 2; // count = 1 (determines remainder) –Java shorthand expressions for the above examples Examples int count = 0; count += 10; // count = 10 count -= 5; // count = 5 count *= 3; // count = 15 count /= 4; // count = 3 count %= 2; // count = 1
Postfix Expressions (section 2.4) A postfix expression –Is similar to the “+=“ and “-=“ compound assignment operators Limited to increasing/decreasing value by 1 Examples int count = 0; count = count + 1; count += 1; count++; The postfix expression occurs in 2 steps (when used in an expression) –Evaluates the current value of the variable –Increases/decreases the value of the variable by 1 Examples of use int ans = 0, count = 0; ans = 5 + count++; // ans = 5, count = 1 count++; // count = 2 System.out.println(“The current count is” + count); // Prints 2 All 3 are identical
Prefix Expressions (section 2.4) A prefix expression –Is similar to the “+=“ and “-=“ compound assignment operators Limited to increasing/decreasing value by 1 Examples int count = 0; count = count + 1; count += 1; ++count; The prefix expression occurs in 2 steps (when used in an expression) –Increases/decreases the value of the variable by 1 –Evaluates the new value of the variable Examples of use int ans = 0, count = 0; ans = ++count + 5; // ans = 6, count = 1 ++count; // count = 2 System.out.println(“The current count is” + count); // Prints 2 All 3 are identical
Basic Math Functions (section 3.5) Includes additional functions –Beyond the basic math operators (+,-,*,/,etc.) –Example functions Ceiling – returns next highest whole number Math.ceil(3.14); // 4 Floor – returns next lowest whole number Math.floor(3.14); // 3 Round – rounds value to closest whole number Math.round(3.14); // 3 Max – returns the larger of two values int a = 3, b = 5; Math.max(a, b); // 5 Min – returns the smaller of two values int a = 3, b = 5; Math.min(a, b); // 3
Evaluating Expressions (section 2.4) Rules for evaluating expressions int ans = * 2; –Only 1 variable allowed on the left side of an expression 1 or more variables, constants and functions allowed on the right side –The right side of the expression must be reduced to a single value Once reduced, it can be assigned to a variable –Evaluation follows “Order of Precedence” rules Similar to the precedence rules used in math courses Parentheses can be used to override the precedence rules int ans = * 2; // ans = 14 int ans = (4 + 5) * 2; // ans = 18
Order of Operations (appendix D) Order of operations chart OperatorDescription Example Variable Constant Parentheses count 3.14 (a + b) [], f(x), ++, --Array, Function, Postfix ary[i], doIt(x, y), a++, a-- ++, --, !, &, * Prefix, Not, Address Pointer Reference ++a, --a, !a, &a, *a ()Casting (int)a *, /, % Multiply, divide, modulus a * b, a / b, a% b +, -Add, subtract a + b, a - b =, >Comparison a = b, a > b ==, !=Equal, not equal a == b, a != b &&Logical and a && b ||Logical or a || b =, +=, -=, *=, /=, %=Assignment a = b, a += b, a -= b, a *= b, a /= b, a %= b
Data Type Conversion (section 2.5) Data type conversion –Deals with converting one data type to another Also referred to as “casting” The contents of box C will completely fit in boxes A or B The contents of box B will completely fit in box A, but not box C The contents of box A will not completely fit in boxes B or C A A B B C C
Data Type Conversion (section 3.5) Data type conversion