Download presentation
Presentation is loading. Please wait.
Published byLauren Porter Modified over 6 years ago
1
Lecture 3: Operators, Expressions and Type Conversion
Nada alzahrani CS2301
2
Outline Arithmetic Expressions Comparison and Logical Operators
Operators and Operands Arithmetic Expressions Comparison and Logical Operators Shortcut Assignment Operators Programming Style and Documentation Type Conversion Programming Errors
3
Operators and Operands
Java programs specify computation in the form of arithmetic expressions. The most common operators in Java are the ones that specify arithmetic computation: + Addition Subtraction * Multiplication / Division % Remainder Operators in Java usually appear between two subexpressions, which are called its operands. Operators that take two operands are called binary operators.
4
Numeric Operators
5
Integer Division of type int if both operands are of type int,
Whenever you apply a binary operator to numeric values in Java, the result will be: of type int if both operands are of type int, a double if either operand is a double. example: 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5
6
Remainder Operator Example: For example,
% computes the remainder when the first divided by the second. Example: 5 % 2 yields 1 (the remainder of the division) Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd.
7
Remainder Operator Suppose today is Saturday and you and your friends are going to meet in 10 days. What day is in 10 days? You can find that day is Tuesday using the following expression:
8
Problem: Displaying Time
Write a program that converts seconds into minutes and displaying the remaining seconds.
9
Problem: Displaying Time
10
Arithmetic Expressions
is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
11
How to Evaluate an Expression
Though Java has its own way to evaluate an expression behind the scene The result of a Java expression and its corresponding arithmetic expression are the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java expression.
12
Examples ?= 1+2*(3+4) - Evaluated as 1+(2*(3+4)) and results is 15
?= 5*2+9%4 -Evaluated as (5*2) + (9%4) and the result is 11 ?= 5* 2 % (7-4) Evaluated as (5*2) % (7-4) and the result is 1
13
Problem: Converting Temperatures
Write a program that converts a Fahrenheit degree to Celsius using the formula:
14
Comparison and Logical Operators
Comparison Operators Operator Name < less than <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to Logical Operators Operator Name ! not && and || or ^ exclusive or
15
Truth Table for Operator ! And &&
16
Truth Table for Operator || And ^
17
Examples Here is a program that checks whether a number is divisible by 2 and 3, whether a number is divisible by 2 or 3, and whether a number is divisible by 2 or 3 but not both: System.out.println("Is " + number + " divisible by 2 and 3? " + ((number % 2 == 0) && (number % 3 == 0))); System.out.println("Is " + number + " divisible by 2 or 3? " + ((number % 2 == 0) || (number % 3 == 0))); System.out.println("Is " + number +" divisible by 2 or 3, but not both? " + ((number % 2 == 0) ^ (number % 3 == 0)));
18
Shortcut Assignment Operators
19
Increment and Decrement Operators
Name Description ++var preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++ postincrement The expression (var++) evaluates to the original value in var and increments var by 1. --var predecrement The expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. var-- postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.
20
Increment and Decrement Operators
21
Increment and Decrement Operators
Notes: Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i. The following types of expressions can be statements: variable op= expression; // Where op is +, -, *, /, or % ++variable; variable++; --variable; variable--;
22
Programming Style and Documentation
Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles
23
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, instructor, date, and a brief description at the beginning of the program.
24
Naming Conventions Variables and method names:
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. Class names: Capitalize the first letter of each word in the name. For example, the class name ComputeArea. Constants: Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI and MAX_VALUE
25
Proper Indentation and Spacing
Indent two spaces. Spacing Use blank line to separate segments of the code.
26
Block Styles Use end-of-line style for braces.
27
Numeric Type Conversion
Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * k / 2;
28
Conversion Rules When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int.
29
Type Casting double d = 3; (type widening) Explicit casting
Implicit casting double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) What is wrong? int x = 5 / 2.0;
30
Problem: Keeping Two Digits After Decimal Points
Write a program that displays the sales tax with two digits after the decimal point.
31
Casting between char and Numeric Types
int i = 'a'; // Same as int i = (int)'a'; value of i = 97. char c = 97; // Same as char c = (char)97; value of c = a.
32
Converting Strings to Integers
The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number. To convert a string into an int value, you can use the static parseInt method in the Integer class as follows: int intValue = Integer.parseInt(intString); where intString is a numeric string such as “123”.
33
Converting Strings to Doubles
To convert a string into a double value, you can use the static parseDouble method in the Double class as follows: double doubleValue =Double.parseDouble(doubleString); where doubleString is a numeric string such as “123.45”.
34
Programming Errors Syntax Errors Detected by the compiler
Runtime Errors Causes the program to abort Logic Errors Produces incorrect result
35
Syntax Errors
36
Runtime Errors
37
Logic Errors
38
Debugging Logic errors are called bugs.
The process of finding and correcting errors is called debugging. Debugger is a program that facilitates debugging. You can use a debugger to Execute a single statement at a time. Trace into or stepping over a method. Set breakpoints. Display variables. Display call stack. Modify variables.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.