Upcoming SlideShare
×

# 02slide

501 views

Published on

Published in: Economy & Finance, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
501
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 02slide

1. 1. Chapter 2 Primitive Data Types and Operations
2. 2. <ul><li>Introduce Programming with an Example </li></ul><ul><li>Identifiers, Variables, and Constants </li></ul><ul><li>Primitive Data Types </li></ul><ul><ul><li>byte, short, int, long, float, double, char, boolean </li></ul></ul><ul><li>Expressions </li></ul><ul><li>Operators, Precedence, Associativity, Operand Evaluation Order </li></ul><ul><li>Style and Documentation </li></ul><ul><li>Syntax Errors, Runtime Errors, and Logic Errors </li></ul><ul><li>Another example: E.g. 2.2, The MyInput class </li></ul>
3. 3. Introducing Programming with an Example <ul><li>Example 2.1: Computing the area of a circle with radius 2. </li></ul><ul><li>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. </li></ul><ul><li>Step 2: Translate the algorithm into programming code </li></ul>
4. 4. Example 2.1: Computing the area of a circle (with radius 2) <ul><li>Questions to ask yourself: </li></ul><ul><ul><li>What do you want to do? Compute the area of a circle with radius 2. </li></ul></ul><ul><ul><li>How to compute the area? What is the formula? </li></ul></ul><ul><ul><ul><li>area = radius 2 Π </li></ul></ul></ul><ul><ul><li>From this formula, what are the things that you need? </li></ul></ul><ul><ul><ul><li>The value for radius and Π . </li></ul></ul></ul><ul><ul><ul><li>Radius is 2 and Π is 3.14 </li></ul></ul></ul><ul><li>Step 1: Design the algorithm. </li></ul><ul><ul><li>Calculate the area for circle with radius 2 using this formula: area = radius 2 Π </li></ul></ul><ul><ul><li>Display the area. </li></ul></ul><ul><li>Step 2: Translate the algorithm into programming code </li></ul><ul><ul><li>ComputeArea1.java (radius is 2) </li></ul></ul><ul><ul><li>ComputeArea2.java (radius is 4) </li></ul></ul>
5. 5. Example 2.1: Computing the area of a circle ( variables and assigning values) <ul><li>What you want to do? Want to reuse the value. </li></ul><ul><li>Step 1: Design the algorithm. </li></ul><ul><ul><ul><li>Declaring variables </li></ul></ul></ul><ul><ul><ul><li>Assign value to variables </li></ul></ul></ul><ul><ul><ul><li>Compute the area using this formula </li></ul></ul></ul><ul><ul><ul><ul><li>area = radius 2 Π </li></ul></ul></ul></ul><ul><ul><ul><li>Display results </li></ul></ul></ul><ul><li>Step 2: Translate the algorithm into programming code </li></ul><ul><ul><li>ComputeArea3.java </li></ul></ul>
6. 6. Example 2.1: Computing the area of a circle ( read in value from user) <ul><li>What you want to do? Want to get value from user. </li></ul><ul><li>Step 1: Design the algorithm. </li></ul><ul><ul><ul><li>Declaring variables </li></ul></ul></ul><ul><ul><ul><li>Prompt user to enter radius </li></ul></ul></ul><ul><ul><ul><li>Get the string from the keyboard </li></ul></ul></ul><ul><ul><ul><li>Assign value to variable </li></ul></ul></ul><ul><ul><ul><li>Compute the area using this formula </li></ul></ul></ul><ul><ul><ul><ul><li>area = Π radius 2 </li></ul></ul></ul></ul><ul><ul><ul><li>Display results </li></ul></ul></ul><ul><li>Step 2: Translate the algorithm into programming code ( ComputeArea4.java) </li></ul>
7. 7. Identifiers <ul><li>Give name to programming entities such as variables, constants, methods, classes and packages. </li></ul><ul><li>An identifier is a sequence of characters that consists of letters, digits, underscore and dollar sign. </li></ul><ul><li>An identifier must start with a letter, an underscore, or a dollar sign. Cannot start with digit. </li></ul>
8. 8. Identifiers <ul><li>An identifier cannot contain operators, such as +, -, and so on. </li></ul><ul><li>An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). </li></ul><ul><li>An identifier cannot be true , false , or null . </li></ul><ul><li>An identifier can be of any length. </li></ul>
10. 10. Declaring Variables <ul><li>int x; // Declare x to be an </li></ul><ul><li>// integer variable; </li></ul><ul><li>double radius; // Declare radius to </li></ul><ul><li>// be a double variable; </li></ul><ul><li>char a; // Declare a to be a </li></ul><ul><li>// character variable; </li></ul><ul><ul><ul><ul><li>General syntax: </li></ul></ul></ul></ul><ul><ul><ul><li>datatype identifier; </li></ul></ul></ul><ul><ul><ul><li>datatype identifier, identifier; </li></ul></ul></ul>
11. 11. Assignment Statements <ul><li>Assignment operator = </li></ul><ul><li>x = 1; // Assign 1 to x; </li></ul><ul><li>radius = 1.0; // Assign 1.0 to radius; </li></ul><ul><li>a = 'A'; // Assign 'A' to a; </li></ul>
12. 12. Declaring and Initializing in One Step <ul><li>int x = 1; </li></ul><ul><li>double d = 1.4; </li></ul><ul><li>float f = 1.4; // Is this statement correct? </li></ul><ul><ul><ul><ul><li>General syntax: </li></ul></ul></ul></ul><ul><ul><li>datatype identifier = value; </li></ul></ul><ul><ul><li>datatype identifier = value, identifier = value; </li></ul></ul>
13. 13. Constants <ul><li>final datatype CONSTANTNAME = VALUE; </li></ul><ul><li>final double PI = 3.14159; </li></ul><ul><li>final int SIZE = 3; </li></ul>
14. 14. Numerical Data Types <ul><li>byte 8 bits </li></ul><ul><li>short 16 bits </li></ul><ul><li>int 32 bits </li></ul><ul><li>long 64 bits </li></ul><ul><li>float 32 bits </li></ul><ul><li>double 64 bits </li></ul>
15. 15. Number Literals <ul><li>int i = 34; </li></ul><ul><li>long l = 1000000; </li></ul><ul><li>float f = 100.2f; or float f = 100.2F; </li></ul><ul><li>double d = 100.2d or double d = 100.2D; </li></ul>
16. 16. Operators <ul><li>Multiplicative Operators : *, /, and % </li></ul><ul><li>Integer division </li></ul><ul><li>- 5/2 yields an integer 2. </li></ul><ul><li>Real number division </li></ul><ul><li>- 5.0/2 yields a double value 2.5 </li></ul><ul><li>- 5 /2.0 yields a double value 2.5 too </li></ul><ul><li>5 % 2 yields 1 (the remainder of the division) </li></ul><ul><li>Additive Operators: +, - </li></ul>
17. 17. 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
18. 18. Increment and Decrement Operators <ul><li>x = 1; </li></ul><ul><li>y = 1 + x++; </li></ul><ul><li>y = 1 + ++x; </li></ul><ul><li>y = 1 + x--; </li></ul><ul><li>y = 1 + --x; </li></ul>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 .
19. 19. Numeric Type Conversion <ul><li>Consider the following statements: </li></ul><ul><li>byte i = 100; </li></ul><ul><li>long l = i*3+4; </li></ul><ul><li>double d = i*3.1+l/2; </li></ul><ul><li>int x = l; (Wrong) </li></ul><ul><li>long l = x;(fine,implicit casting) </li></ul>
20. 20. Type Casting <ul><li>double </li></ul><ul><li>float </li></ul><ul><li>long </li></ul><ul><li>int </li></ul><ul><li>short </li></ul><ul><li>byte </li></ul>Range increases
21. 21. Type Casting, cont. <ul><li>Implicit casting </li></ul><ul><li>double d = 3; (type widening – from int to double) </li></ul><ul><li>Explicit casting </li></ul><ul><li>int i = (int)3.0; (type narrowing – from double to int) </li></ul><ul><li>What is wrong? int x = 5/2.0; </li></ul>
22. 22. Character Data Type <ul><li>char letter = 'A'; (ASCII) </li></ul><ul><li>char numChar = '4'; (ASCII) </li></ul><ul><li>char letter = 'u0041'; (Unicode) </li></ul><ul><li>// http://www.pccl.demon.co.uk/java/unicode.html </li></ul>
23. 23. Unicode Format Description Escape Sequence Unicode Backspace  u0008 Tab u0009 Linefeed u000a Carriage return u000d Backslash u005C Single Quote ’ u0027 Double Quote ” u0022
24. 24. The boolean Type and boolean Operators <ul><li>boolean lightsOn = true; </li></ul><ul><li>boolean lightsOn = false; </li></ul><ul><li>&& (and) (1 < x) && (x < 100) </li></ul><ul><li>|| (or) (lightsOn) || (isDayTime) </li></ul><ul><li>! (not) !(isStopped) </li></ul>