02b Slide
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
553
On Slideshare
553
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Chapter 2 Primitive Data Types and Operations (continue)
  • 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. Short-Circuit/conditional && and || Operators
    • Eg: (1 > x) && (1 > x++)
    • Eg: (1 > x) || (1 > x++)
  • 4. conditional & and | Operators (Bitwise Operators)
    • Same as && and || but both operands are evaluated.
    • Eg: x & y
    • Eg: x | y
    • Can also be used for bitwise operations
    http://www.geocities.com/practicalvb/java/java-operators.html
  • 5. Equality Operators
    • These operators evaluate to a boolean value. The equality operators == and != test whether two values are equal or not, respectively:
    • int x = 3, y = 6;
    • boolean result; result = (x == y); // false
    • result = (x != y); // true
    • Caution: Whenever you test whether two values are equal, always use two equal signs! If you use
    • a = b instead of a == b, a is assigned the value of b instead.
  • 6. Relational Operators
    • These operators also evaluate to a boolean value.
    • 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
    • result = (x < y); // true, 3 < 6
  • 7. The Conditional Operator
    • 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:
      • boolean-expression ? result-if-true : result-if-false
    • This operator returns result-if-true if boolean-expression is true ; otherwise, it returns result-if-false .
    • For example:
      • value = (n >= 0) ? n : -n;
    • This statement returns n or -n depending on whether n is greater than or equal to 0;
  • 8. Operator Precedence
    • 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.
  • 9. Operator Precedence
    • var++, var—-
    • ++var , --var
    • Casting
    • !
    • * , / , %
    • + , -
    • < , <= , > , >=
    • == , !=;
    • &
    • ^
    • |
    • &&
    • ||
    • ?:
    • = , += , -= , *= , /= , %=
  • 10. Operator Associativity
    • 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 .
    • a + b – c – d is equivalent to  ((a + b) - c) – d
    • Assignment operators are right-associative . Therefore, the expression
    • a = b += c = 5 is equivalent to a = (b += (c = 5))
  • 11. Operand Evaluation Order
    • 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.
    • The left-hand operand of a binary operator is evaluated before any part of the right-hand operand is evaluated.
  • 12. Operand Evaluation Order, cont.
    • 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. 
      • int a = 0;
      • int x = a + (++a);
    • But x becomes 2 in the following code, because ++a is evaluated to 1, then a is evaluated to 1.
      • int a = 0;
      • int x = ++a + a;
  • 13. Programming Style and Documentation
    • Appropriate Comments
    • Naming Conventions
    • Proper Indentation and Spacing Lines
    • Block Styles
  • 14. Appropriate Comments
    • 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.
    • Include your name, class section, instruction, date, and a brief description at the beginning of the program.
  • 15. Naming Conventions
    • Choose meaningful and descriptive names.
    • Variables and method names:
      • 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 .
  • 16. Naming Conventions, cont.
    • Class names:
      • Capitalize the first letter of each word in the name. For example, the class name ComputeArea .
    • Constants:
      • Capitalize all letters in constants. For example, the constant PI .
  • 17. Proper Indentation and Spacing
    • Indentation
      • Indent two spaces.
    • Spacing
      • Use blank line to separate segments of the code.
  • 18. Block Styles
    • Use next-line style for braces.
  • 19. Programming Errors
    • Syntax Errors
      • Detected by the compiler
    • Runtime Errors
      • Causes the program to abort
    • Logic Errors
      • Produces incorrect result
  • 20. Example 2.1 - a nother approach
    • Example 2.1 Computing the Area of a Circle
    • This program reads the radius from the keyboard and computes the area of the circle.
    ComputeArea Run
  • 21. The MyInput Class
    • This class contains the methods for reading an int, a double, or a string from the keyboard.
    • The methods are readInt, readDouble, and readString
    MyInput
  • 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