02b Slide

22
Chapter 2 Primitive Data Types and Operations (continue)

Transcript of 02b Slide

Page 1: 02b Slide

Chapter 2 Primitive Data Types and Operations(continue)

Page 2: 02b Slide

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

Page 3: 02b Slide

Short-Circuit/conditional && and || Operators

Eg: (1 > x) && (1 > x++)

Eg: (1 > x) || (1 > x++)

Page 4: 02b Slide

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

Page 5: 02b Slide

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.

Page 6: 02b Slide

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

Page 7: 02b Slide

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;

Page 8: 02b Slide

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.

Page 9: 02b Slide

Operator Precedence var++, var—- ++var,--var Casting ! *, /, % +, - <, <=, >, >= ==, !=; & ^ | && || ?: =, +=, -=, *=, /=, %=

Page 10: 02b Slide

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

Page 11: 02b Slide

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.

Page 12: 02b Slide

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;

Page 13: 02b Slide

Programming Style and Documentation

Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles

Page 14: 02b Slide

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.

Page 15: 02b Slide

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.

Page 16: 02b Slide

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.

Page 17: 02b Slide

Proper Indentation and Spacing

Indentation– Indent two spaces.

Spacing – Use blank line to separate segments of the code.

Page 18: 02b Slide

Block Styles

Use next-line style for braces.

Page 19: 02b Slide

Programming Errors

Syntax Errors– Detected by the compiler

Runtime Errors– Causes the program to abort

Logic Errors– Produces incorrect result

Page 20: 02b Slide

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

Page 21: 02b Slide

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

Page 22: 02b Slide

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