02b Slide
-
Upload
madzani-nusa -
Category
Technology
-
view
309 -
download
2
Transcript of 02b Slide
Chapter 2 Primitive Data Types and Operations(continue)
The boolean Type and boolean/logical Operators
P Q P && Q P || Q !P P^Q
True True True True False False
True False False True False True
False True False True True True
False False False False True False
Short-Circuit/conditional && and || Operators
Eg: (1 > x) && (1 > x++)
Eg: (1 > x) || (1 > x++)
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
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.
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
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;
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.
Operator Precedence var++, var—- ++var,--var Casting ! *, /, % +, - <, <=, >, >= ==, !=; & ^ | && || ?: =, +=, -=, *=, /=, %=
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))
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.
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;
Programming Style and Documentation
Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles
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.
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.
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.
Proper Indentation and Spacing
Indentation– Indent two spaces.
Spacing – Use blank line to separate segments of the code.
Block Styles
Use next-line style for braces.
Programming Errors
Syntax Errors– Detected by the compiler
Runtime Errors– Causes the program to abort
Logic Errors– Produces incorrect result
Example 2.1 - another 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.
ComputeAreaComputeArea RunRun
The MyInput Class
MyInputMyInput
This class contains the methods for reading an int, a double, or a string from the keyboard.
The methods are readInt, readDouble, and readString
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.
ComputeChangeComputeChange RunRun