290 likes | 399 Views
Arithmetic operation Session 5. Course : T-0974 Algorithm & Object-Oriented Programming I Year : 2010. Learning Outcomes. After taking this lecture, students should be expected to use aritmetic operation & naming convention correctly. Lecture Outline. Numeric Operator Assignment Statement
E N D
Arithmetic operation Session 5 Course : T-0974 Algorithm & Object-Oriented Programming I Year : 2010
Learning Outcomes After taking this lecture, students should be expected to use aritmetic operation & naming convention correctly.
Lecture Outline • Numeric Operator • Assignment Statement • Assignment Expressions • Arithmetic Expressions • Operator Shorthand • Operator Increment & Decrement • Naming Convention
Numeric Operator • 4 Real Number Data types in Java : byte, short, int, and long • 2 Decimal Number Data types in Java : float, and double. • Using Double Decimal is more accurate than float • The result of two Real Number division produce Real Number • 5/2 = 2 (not 2.5), the result produce 2 instead of 2.5 due to truncation.
Numeric Operator • Define an area = 3.14 * rad * rad • area, 3.14, rad, rad operand • * operator • = assignment statement • Operand could have a different data type and Java can convert it by widening a cast automatically. • Example : byte i = 100; long k = i * 3 + 4; double d = i * 3.1 + k / 2;
Numeric Operator • `%’ is an operator to calculate remainder/modulo from a division. • This operator can be used with positive/negative number or decimal number. • Example : 10%7 = 3 6 % 7 = 6 -7 % 3 = -1 -12 % 4 = 0 20 % -13 = 7 -26 % -8 = -2
Numeric Operator • Let 20 % -13 = 7 20 = dividend -13 = divisor 7 = result • If a dividend is negative, the result of modulo/remainder operation is negative. • Modulo/remainder is important in programmig. • Example : Algorithm for determining even/odd number. Search algorithm for finding day in Calendar.
Assignment Statements • Use for set or reset the value located in storage location. • Using (=) as assignment operator. • Its format is “Variabel = expression” • Example: area = 3.14 * radius * radius; Assignment statements Variabel Expression Assignment operator
Assignment Expressions • Expression can be declared by assignment statetements. • Example: r = j = k = 1; Assignment statements Assignment Expression Variabel Assignment operator
Arithmetic Expressions • Expressions that include numeric operator. • Example : • Which can be declared like : (3+4*x)/5 – 10*(y-5)*(a+b+c)/x+9*(4/x+(9+x)/y) • Operation inside parentheses will be executed first. • Possibility to use nested parentheses (parentheses inside parentheses)
Arithmetic Expressions • Execution priority is * and / as highest, + and – as lowest. It will executed after parentheses. • If there is more than one Arithmetic Expression at the same level, the highest priority is from the left to the right which is lowest priority. • When executing expression, Java will refer to operator precedence and associative.
Operator Shorthand • A variable which is used, modified and assigned to the same variable. • Example : i = i + 8 Can be changed to i += 8 • “+=“ is addition assignment operator in shorthand operator. • This kind of operator must not be separated by space (+=, NOT + =)
Operator Shorthand • Example: A = A – 3 A -= 3 B = B % 7 B %= 7 C = C * 8 C *= 8
Increment & Decrement Operator • Shorthand operator increases and decreases 1 point. • Usually use in looping. • Operator: ++ dan – • It can be use as prefix which is before the variable or as postfix which is after the variable. • It must not be separated by space. (++, NOT + +)
Operator Increment dan Decrement • Example 3: • double x = 1.0; • double y = 5.0; • double z = x-- + (++y); • Equal to : • double x = 1.0; • double y = 5.0; • y = y + 1; • double z = x + y; • x = x – 1; • Example 1: int i = 10; int newNum = 10 * i++; Equal to : int i = 10; int newNum = 10 * i; i = i + 1; • Example 2: int i = 10; int newNum = 10 * (++i); Equal to int i = 10; i = i + 1; int newNum = 10 * i;
Operator Increment & Decrement • Starts i=8; and j=3; • i+=(++j); Equal to: j=j+1; j=4 i=i+j; i=12 • i+=(j++); Equal to: i=i+j; i=16 j=j+1; j=5
Did You Know? • Statements/Expressions can be used directly to System.out.println(…); • Example : System.out.println(i=10); System.out.println(++i); System.out.println(i%=2); System.out.println(i=i+(i=5)); System.out.println(i=(i=1)+2);
Did You Know? • Using quotes with System.out.println(…) means String. • Using (+) with System.out.println(…) means concatenate 2 / more Strings. • Example : System.out.println(“Result of i+j=”+i+j); Will differ to, System.out.println(“Result of i+j=“+(i+j));
Advanced Learning • Variable and Function Naming Convention. • Lowercase is used for naming variable and function. If it more than one word, concatenate and use Capital for second word and next. • Example : radius, area, showInputDialog, println, nextInt • Class Naming Convention • Use Capital for every word. • Example: ComputeArea, JOptionPane, System, Math, Scanner • Constant Naming Convention • Use Capital for every character and every word is separated by underscore(_). • Example: PI, MAX_VALUE
Advanced Learning • Java is used as Standard for Naming Convention. • It makes other programmer easier to understand the code. • Don’t write Class Name which has been defined in Java. • Example: Math, System • Avoid shortening the names. • Example: numberOfStudents akan lebih baik (deksriptif) daripada numStuds, numOfStuds, atau numOfStudents
Advanced Learning • Be consistent to indentations and spaces • Example : int i= 3+4 * 4 bad style int i = 3 + 4 * 4 good style • Block Styles • Next-line style public class Test { public static void main(String[] args) { } } • End-of-line style public class Test { public static void main(String[] args) { } }
Referensi • Introduction to Java Programming. 7ed. Liang. 2009. p58-81 • Java Software Solutions. 5ed. Lewis & Loftus. 2007. p96-99, p103-114 • Java A Beginner’s Guide. 3ed. Herbert. 2005. p52-55, p58-68 • Dasar Pemrograman Java 2. Abdul Kadir. 2004. p42-61 • The Complete Referenc Java. 5ed. Herbert. 2005. p58-62