1 / 29

Arithmetic operation Session 5

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

didina
Download Presentation

Arithmetic operation Session 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arithmetic operation Session 5 Course : T-0974 Algorithm & Object-Oriented Programming I Year : 2010

  2. Learning Outcomes After taking this lecture, students should be expected to use aritmetic operation & naming convention correctly.

  3. Lecture Outline • Numeric Operator • Assignment Statement • Assignment Expressions • Arithmetic Expressions • Operator Shorthand • Operator Increment & Decrement • Naming Convention

  4. Numeric Operator

  5. 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.

  6. 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;

  7. 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

  8. 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.

  9. 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

  10. Assignment Expressions • Expression can be declared by assignment statetements. • Example: r = j = k = 1; Assignment statements Assignment Expression Variabel Assignment operator

  11. 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)

  12. 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.

  13. Arithmetic Expressions

  14. 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 + =)

  15. Operator Shorthand • Example: A = A – 3  A -= 3 B = B % 7  B %= 7 C = C * 8  C *= 8

  16. 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 + +)

  17. Increment & Decrement Operator

  18. 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;

  19. Operator Increment dan Decrement

  20. 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

  21. 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);

  22. Did You Know?

  23. 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));

  24. Did You Know?

  25. 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

  26. 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

  27. 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) { } }

  28. 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

More Related