Top Banner
1 Chapter 2: Elementary Programming Shahriar Hossain
25

1 Chapter 2: Elementary Programming Shahriar Hossain.

Dec 22, 2015

Download

Documents

Coral Spencer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Chapter 2: Elementary Programming Shahriar Hossain.

1

Chapter 2: Elementary Programming

Shahriar Hossain

Page 2: 1 Chapter 2: Elementary Programming Shahriar Hossain.

Quiz solution

What will be the value of the variable x after performing the following Java statement:int x = 7 − 10 % 2 / 3;

Solution: 7

2

Consecutive multiplicative operators will be evaluated from left to right in the expression since they have the same priority.

Page 3: 1 Chapter 2: Elementary Programming Shahriar Hossain.

3

Page 4: 1 Chapter 2: Elementary Programming Shahriar Hossain.

Quiz solution

What will be printed as a result of the following piece of code? explain step-by-step:double x = 3;

double y = x + 1;

x = y − 2;

System.out.println(x);

System.out.println(y);

Solution:

2.0

4.0

4

Page 5: 1 Chapter 2: Elementary Programming Shahriar Hossain.

5

Objectives To use augmented assignment operators To distinguish between postincrement and

preincrement and between postdecrement and predecrement

To cast the value of one type to another type To represent characters using the char type To represent a string using the String type

To become familiar with Java programming style and documentation

Page 6: 1 Chapter 2: Elementary Programming Shahriar Hossain.

6

Declaring Variablesint x; // Declare x to be an // integer variable;

double radius; // Declare radius to // be a double variable;

char a; // Declare a to be a // character variable;

Page 7: 1 Chapter 2: Elementary Programming Shahriar Hossain.

7

Assignment Statements

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

a = 'A'; // Assign 'A' to a;

Page 8: 1 Chapter 2: Elementary Programming Shahriar Hossain.

8

Numeric Operators

Name Meaning Example Result

+ Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2

Page 9: 1 Chapter 2: Elementary Programming Shahriar Hossain.

9

Augmented Assignment Operators

Operator Example Equivalent

+= i += 8 i = i + 8

-= f -= 8.0 f = f - 8.0

*= i *= 8 i = i * 8

/= i /= 8 i = i / 8

%= i %= 8 i = i % 8

The operators +, -, *, /, and % can be combined with the assignment operator to form augmented operators.

Page 10: 1 Chapter 2: Elementary Programming Shahriar Hossain.

Caution

There is no spaces in the augmented assignment operators+ = is wrong

+= is correct

10

Page 11: 1 Chapter 2: Elementary Programming Shahriar Hossain.

11

Increment andDecrement Operators

Operator 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.

Page 12: 1 Chapter 2: Elementary Programming Shahriar Hossain.

12

Increment andDecrement Operators, cont.

Page 13: 1 Chapter 2: Elementary Programming Shahriar Hossain.

13

Increment andDecrement Operators, cont.

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.

Page 14: 1 Chapter 2: Elementary Programming Shahriar Hossain.

14

Increment andDecrement Operators, cont.

What is the output of the following code segment?

int i=2;int k=++i+i;System.out.println(i);System.out.println(k);

36

Page 15: 1 Chapter 2: Elementary Programming Shahriar Hossain.

15

Increment andDecrement Operators, cont.

What is the output of the following code segment?

int i=2;int k=i+++i; // equivalent to int k=(i++)+i;System.out.println(i);System.out.println(k);

35

Page 16: 1 Chapter 2: Elementary Programming Shahriar Hossain.

16

Numeric Type Conversion

Consider the following statements:

byte i = 100;

long k = i * 3 + 4;

double d = i * 3.1 + k / 2;

Page 17: 1 Chapter 2: Elementary Programming Shahriar Hossain.

17

Conversion RulesWhen 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.

Page 18: 1 Chapter 2: Elementary Programming Shahriar Hossain.

18

Type CastingImplicit 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;The correct statement is int i=(int)(5/2.0);

Page 19: 1 Chapter 2: Elementary Programming Shahriar Hossain.

19

Type Casting

byte, short, int, long, float, double

range increases

Page 20: 1 Chapter 2: Elementary Programming Shahriar Hossain.

20

Casting in an Augmented Expression

In Java, an augmented expression of the form x1 op= x2 is implemented as x1 = (T)(x1 op x2), where T is the type for x1. Therefore, the following code is correct.

int sum = 0;

sum += 4.5; // sum becomes 4 after this statement

sum += 4.5 is equivalent to sum = (int)(sum + 4.5).

Page 21: 1 Chapter 2: Elementary Programming Shahriar Hossain.

21

Character Data Type

char letter = 'A'; (ASCII)

char numChar = '4'; (ASCII)

char letter = '\u0041'; (Unicode)

char numChar = '\u0034'; (Unicode)

Four hexadecimal digits.

NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b.

char ch = 'a';

System.out.println(++ch);

Page 22: 1 Chapter 2: Elementary Programming Shahriar Hossain.

ASCII (The American Standard Code for Information Interchange)

22

Page 23: 1 Chapter 2: Elementary Programming Shahriar Hossain.

Unicode

An encoding scheme established by the Unicode Consortium

Originally 16-bit but there are supplementary characters beyond the 16-bit limit

23

Page 24: 1 Chapter 2: Elementary Programming Shahriar Hossain.

24

Escape Sequences for Special Characters

Description Escape Sequence Unicode

Backspace \b \u0008

Tab \t \u0009

Linefeed \n \u000A

Carriage return \r \u000D

Backslash \\ \u005C

Single Quote \' \u0027

Double Quote \" \u0022

Page 25: 1 Chapter 2: Elementary Programming Shahriar Hossain.

Example

Is this a correct statement?System.out.println("He said "Java is fun" ");

No. The statement above will give a compiler error

The correct statement will beSystem.out.println("He said \"Java is fun\" ");

25