Download - 02slide
![Page 1: 02slide](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/1.jpg)
Chapter 2 Primitive Data Types and Operations
![Page 2: 02slide](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/13.jpg)
Constants
final datatype CONSTANTNAME = VALUE;
final double PI = 3.14159;
final int SIZE = 3;
![Page 14: 02slide](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/14.jpg)
Numerical Data Types
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
float 32 bits
double 64 bits
![Page 15: 02slide](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/20.jpg)
Type Casting
double float long int short byte
Range increases
![Page 21: 02slide](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062319/55789360d8b42aaf518b47b9/html5/thumbnails/24.jpg)
The boolean Type and boolean Operators
boolean lightsOn = true;
boolean lightsOn = false;
&& (and) (1 < x) && (x < 100) || (or) (lightsOn) ||(isDayTime) ! (not) !(isStopped)