02slide

24
Chapter 2 Primitive Data Types and Operations

Upload: madzani-nusa

Post on 11-Jun-2015

290 views

Category:

Economy & Finance


0 download

TRANSCRIPT

Page 1: 02slide

Chapter 2 Primitive Data Types and Operations

Page 2: 02slide

Introduce Programming with an Example Identifiers, Variables, and Constants Primitive Data Types

– byte, short, int, long, float, double, char, boolean Expressions Operators, Precedence, Associativity, Operand Evaluation

Order Style and Documentation Syntax Errors, Runtime Errors, and Logic Errors Another example: E.g. 2.2, The MyInput class

Page 3: 02slide

Introducing Programming with an Example

Example 2.1: Computing the area of a circle with radius 2.

Step 1: Design the algorithm. Algorithm describes how to solve the problem in terms of the actions to be executed and the order of execution of the actions.

Step 2: Translate the algorithm into programming code

Page 4: 02slide

Example 2.1: Computing the area of a circle (with radius 2)

Questions to ask yourself:– What do you want to do? Compute the area of a circle with radius 2.– How to compute the area? What is the formula?

area = radius 2 Π – From this formula, what are the things that you need?

The value for radius and Π. Radius is 2 and Π is 3.14

Step 1: Design the algorithm. – Calculate the area for circle with radius 2 using this formula:

area = radius 2 Π – Display the area.

Step 2: Translate the algorithm into programming code– ComputeArea1.java (radius is 2)– ComputeArea2.java (radius is 4)

Page 5: 02slide

Example 2.1: Computing the area of a circle (variables and assigning values)

What you want to do? Want to reuse the value. Step 1: Design the algorithm.

Declaring variables Assign value to variables Compute the area using this formula

– area = radius 2 Π Display results

Step 2: Translate the algorithm into programming code – ComputeArea3.java

Page 6: 02slide

Example 2.1: Computing the area of a circle (read in value from user)

What you want to do? Want to get value from user.

Step 1: Design the algorithm. Declaring variables Prompt user to enter radius Get the string from the keyboard Assign value to variable Compute the area using this formula

– area = Π radius 2

Display results Step 2: Translate the algorithm into programming

code (ComputeArea4.java)

Page 7: 02slide

Identifiers Give name to programming entities such as

variables, constants, methods, classes and packages.

An identifier is a sequence of characters that consists of letters, digits, underscore and dollar sign.

An identifier must start with a letter, an underscore, or a dollar sign. Cannot start with digit.

Page 8: 02slide

Identifiers An identifier cannot contain operators, such as

+, -, and so on. An identifier cannot be a reserved word. (See

Appendix A, “Java Keywords,” for a list of reserved words).

An identifier cannot be true, false, ornull.

An identifier can be of any length.

Page 9: 02slide

Variables

// Compute the first arearadius = 1.0;area = radius*radius*3.14159;System.out.println("The area is “ + area + " for radius "+radius);

// Compute the second arearadius = 2.0;area = radius*radius*3.14159;System.out.println("The area is “ + area + " for radius "+radius);

Page 10: 02slide

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;

General syntax:

datatype identifier;datatype identifier, identifier;

Page 11: 02slide

Assignment Statements Assignment operator =

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

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

Page 12: 02slide

Declaring and Initializingin One Step

int x = 1;

double d = 1.4;

float f = 1.4; //Is this statement correct?

General syntax:

datatype identifier = value;

datatype identifier = value, identifier = value;

Page 13: 02slide

Constants

final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;

final int SIZE = 3;

Page 14: 02slide

Numerical Data Types

byte 8 bits

short 16 bits

int 32 bits

long 64 bits

float 32 bits

double 64 bits

Page 15: 02slide

Number Literals int i = 34;

long l = 1000000;

float f = 100.2f; orfloat f = 100.2F;

double d = 100.2d ordouble d = 100.2D;

Page 16: 02slide

Operators

Multiplicative Operators : *, /, and %

Integer division

- 5/2 yields an integer 2.

Real number division

- 5.0/2 yields a double value 2.5

- 5 /2.0 yields a double value 2.5 too

5 % 2 yields 1 (the remainder of the division)

Additive Operators: +, -

Page 17: 02slide

Shortcut Operators / Combination 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

Page 18: 02slide

Increment andDecrement Operators

x = 1;

y = 1 + x++;

y = 1 + ++x;

y = 1 + x--;

y = 1 + --x;

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 19: 02slide

Numeric Type Conversion

Consider the following statements:

byte i = 100;

long l = i*3+4;

double d = i*3.1+l/2;

int x = l; (Wrong)

long l = x;(fine,implicit casting)

Page 20: 02slide

Type Casting

double float long int short byte

Range increases

Page 21: 02slide

Type Casting, cont.

Implicit casting double d = 3; (type widening – from int to double)

Explicit casting int i = (int)3.0; (type narrowing – from double to int)

What is wrong? int x = 5/2.0;

Page 22: 02slide

Character Data Type

char letter = 'A'; (ASCII)

char numChar = '4'; (ASCII)

char letter = '\u0041'; (Unicode)

//http://www.pccl.demon.co.uk/java/unicode.html

Page 23: 02slide

Unicode Format

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 24: 02slide

The boolean Type and boolean Operators

boolean lightsOn = true;

boolean lightsOn = false;

&& (and) (1 < x) && (x < 100) || (or) (lightsOn) ||(isDayTime) ! (not) !(isStopped)