02b Slide


Published on

Published in: Technology, Travel
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

02b Slide

  1. 1. Chapter 2 Primitive Data Types and Operations (continue)
  2. 2. The boolean Type and boolean/logical Operators True True False False !P False True True True P || Q False False False False True False True False True False False True False True True True P^Q P && Q Q P
  3. 3. Short-Circuit/conditional && and || Operators <ul><li>Eg: (1 > x) && (1 > x++) </li></ul><ul><li>Eg: (1 > x) || (1 > x++) </li></ul>
  4. 4. conditional & and | Operators (Bitwise Operators) <ul><li>Same as && and || but both operands are evaluated. </li></ul><ul><li>Eg: x & y </li></ul><ul><li>Eg: x | y </li></ul><ul><li>Can also be used for bitwise operations </li></ul>http://www.geocities.com/practicalvb/java/java-operators.html
  5. 5. Equality Operators <ul><li>These operators evaluate to a boolean value. The equality operators == and != test whether two values are equal or not, respectively: </li></ul><ul><li>int x = 3, y = 6; </li></ul><ul><li>boolean result; result = (x == y); // false </li></ul><ul><li>result = (x != y); // true </li></ul><ul><li>Caution: Whenever you test whether two values are equal, always use two equal signs! If you use </li></ul><ul><li>a = b instead of a == b, a is assigned the value of b instead. </li></ul>
  6. 6. Relational Operators <ul><li>These operators also evaluate to a boolean value. </li></ul><ul><li>The relational operators <, <=, >, and >= test whether the first value is less than, less than or equal to, greater than, or greater than or equal to the second value, respectively </li></ul><ul><li>result = (x < y); // true, 3 < 6 </li></ul>
  7. 7. The Conditional Operator <ul><li>The only ternary (i.e. takes three values) operator in Java is the conditional operator . This operator returns one of two values depending on a third value: </li></ul><ul><ul><li>boolean-expression ? result-if-true : result-if-false </li></ul></ul><ul><li>This operator returns result-if-true if boolean-expression is true ; otherwise, it returns result-if-false . </li></ul><ul><li>For example: </li></ul><ul><ul><li>value = (n >= 0) ? n : -n; </li></ul></ul><ul><li>This statement returns n or -n depending on whether n is greater than or equal to 0; </li></ul>
  8. 8. Operator Precedence <ul><li>In Java, certain operators are evaluated before others. For example, multiplication is always done before addition, unless parentheses are used. This is known as operator precedence . In the following table, operators with the highest precedence are at the top. Operators at the same level are evaluated left to right. </li></ul>
  9. 9. Operator Precedence <ul><li>var++, var—- </li></ul><ul><li>++var , --var </li></ul><ul><li>Casting </li></ul><ul><li>! </li></ul><ul><li>* , / , % </li></ul><ul><li>+ , - </li></ul><ul><li>< , <= , > , >= </li></ul><ul><li>== , !=; </li></ul><ul><li>& </li></ul><ul><li>^ </li></ul><ul><li>| </li></ul><ul><li>&& </li></ul><ul><li>|| </li></ul><ul><li>?: </li></ul><ul><li>= , += , -= , *= , /= , %= </li></ul>
  10. 10. Operator Associativity <ul><li>When two operators with the same precedence are evaluated, the associativity of the operators determines the order of evaluation. All binary operators except assignment operators are left-associative . </li></ul><ul><li>a + b – c – d is equivalent to  ((a + b) - c) – d </li></ul><ul><li>Assignment operators are right-associative . Therefore, the expression </li></ul><ul><li>a = b += c = 5 is equivalent to a = (b += (c = 5)) </li></ul>
  11. 11. Operand Evaluation Order <ul><li>The precedence and associativity rules specify the order of the operators, but do not specify the order in which the operands of a binary operator are evaluated. Operands are evaluated from left to right in Java. </li></ul><ul><li>The left-hand operand of a binary operator is evaluated before any part of the right-hand operand is evaluated. </li></ul>
  12. 12. Operand Evaluation Order, cont. <ul><li>If no operands have side effects that change the value of a variable, the order of operand evaluation is irrelevant. Interesting cases arise when operands do have a side effect. For example, x becomes 1 in the following code, because a is evaluated to 0 before ++a is evaluated to 1.  </li></ul><ul><ul><li>int a = 0; </li></ul></ul><ul><ul><li>int x = a + (++a); </li></ul></ul><ul><li>But x becomes 2 in the following code, because ++a is evaluated to 1, then a is evaluated to 1. </li></ul><ul><ul><li>int a = 0; </li></ul></ul><ul><ul><li>int x = ++a + a; </li></ul></ul>
  13. 13. Programming Style and Documentation <ul><li>Appropriate Comments </li></ul><ul><li>Naming Conventions </li></ul><ul><li>Proper Indentation and Spacing Lines </li></ul><ul><li>Block Styles </li></ul>
  14. 14. Appropriate Comments <ul><li>Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. </li></ul><ul><li>Include your name, class section, instruction, date, and a brief description at the beginning of the program. </li></ul>
  15. 15. Naming Conventions <ul><li>Choose meaningful and descriptive names. </li></ul><ul><li>Variables and method names: </li></ul><ul><ul><li>Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area , and the method computeArea . </li></ul></ul>
  16. 16. Naming Conventions, cont. <ul><li>Class names: </li></ul><ul><ul><li>Capitalize the first letter of each word in the name. For example, the class name ComputeArea . </li></ul></ul><ul><li>Constants: </li></ul><ul><ul><li>Capitalize all letters in constants. For example, the constant PI . </li></ul></ul>
  17. 17. Proper Indentation and Spacing <ul><li>Indentation </li></ul><ul><ul><li>Indent two spaces. </li></ul></ul><ul><li>Spacing </li></ul><ul><ul><li>Use blank line to separate segments of the code. </li></ul></ul>
  18. 18. Block Styles <ul><li>Use next-line style for braces. </li></ul>
  19. 19. Programming Errors <ul><li>Syntax Errors </li></ul><ul><ul><li>Detected by the compiler </li></ul></ul><ul><li>Runtime Errors </li></ul><ul><ul><li>Causes the program to abort </li></ul></ul><ul><li>Logic Errors </li></ul><ul><ul><li>Produces incorrect result </li></ul></ul>
  20. 20. Example 2.1 - a nother approach <ul><li>Example 2.1 Computing the Area of a Circle </li></ul><ul><li>This program reads the radius from the keyboard and computes the area of the circle. </li></ul>ComputeArea Run
  21. 21. The MyInput Class <ul><li>This class contains the methods for reading an int, a double, or a string from the keyboard. </li></ul><ul><li>The methods are readInt, readDouble, and readString </li></ul>MyInput
  22. 22. Example 2.3 Computing Changes This program lets the user enter the amount in decimal representing dollars and cents and output a report listing the monetary equivalent in single dollars, quarters, dimes, nickels, and pennies. Your program should report maximum number of dollars, then the maximum number of quarters, and so on, in this order. ComputeChange Run