Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Course Lectures Available on line:

Similar presentations


Presentation on theme: "1 Course Lectures Available on line:"— Presentation transcript:

1

2 1 Course Lectures Available on line: http://www.cse.ttit.edu.tw/fccheng

3 2 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

4 3 Outline Data types Variable declaration and use Decisions and loops Input and output

5 4 Primitive Data Types A data type: define values and the operators Each value stored in memory is associated with a particular data type primitive data types: predefined data types in Java Ex: A: integers: byte, short, int, long. B: float, double. C: boolean (true or false) (on or off) D: char (unicode 16 bits for international languages)

6 5 Storage in Programming Languages Registers Stack (handles & primitives in Java) Heap (all class objects in Java) Static storage Constant storage Non-RAM storage (persistent)

7 6 Primitive Data Types Integers: Type byte short int long Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 10 18 Max Value 127 32,767 2,147,483,647 > 9 x 10 18 Note that Built-in types (primitives) are not object handles handles: call by reference primitives: call by value

8 7 Primitive Data Types Floating point: Type float double Storage 32 bits 64 bits Approximate Min Value -3.4 x 10 38 -1.7 x 10 308 Approximate Max Value 3.4 x 10 38 1.7 x 10 308 SExponentMantisa

9 8 Primitive Data Types char: Unicode character set A character set is an ordered list of characters The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters International character set.

10 9 Primitive Data Types boolean: a true or false condition (two states: on or off) The reserved words true and false are the only valid values for a boolean type

11 10 Primitive Data Types Same operations as C/C++ Size of each data type is machine independent

12 11 Wrappers for primitive Data Types For each primitive data type there is a corresponding wrapper class. Wrapper classes are useful in situations where you need an object instead of a primitive type They also contain some useful methods Primitive Type int double char boolean Wrapper Class Integer Double Character Boolean

13 12 Variables A variable is an identifier that represents a location in memory that holds a particular type of data Variables must be declared before they can be used Syntax of a variable declaration: data-type variable-name; For example: int total; // 4-byte int in stack int total, count, sum; int total = 0, count = 20; float unitPrice = 57.25;

14 13 Scope of Variables Block statements: group of statements delimited by braces { // begin of scope 1 int x=1; System.out.println("x="+x); { // begin of scope 2 // int x=3; // can not redine x int y=2; System.out.println("x="+x); System.out.println(“y=”+y); }

15 14 Assignment Statements An assignment statement takes the following form: variable-name = expression; The expression is evaluated and the result is stored in the variable, overwriting the value currently stored in the variable The expression can be a single value or a more complicated calculation

16 15 Constants A constant is similar to a variable except that they keep the same value throughout their existence They are specified using the reserved word final in the declaration. For example: final double PI = 3.14159; final int STUDENTS_COUNT = 25; All final are static. final static double PI = 3.14159; Better than literal values because: A. make code more readable by giving meaning to a value B. use less memory, easy to modification (one place)

17 Input and Output Java I/O is complicated. 1. Different kind of IO: Files, console, block of memory, network 2. Different kinds of operations: Sequential, random-access, binary, character, integer, by lines, be words,... Java provides a lot of classes to support IO.

18 Input and Output Java I/O is based on input streams and output streams 1. All classes inherit from IS have read methods. 2. All classes inherit from OS have write methods. There are three predefined standard streams: print and println methods write to standard output Stream System.in System.out System.err Purpose reading input writing output writing errors Default Device keyboard monitor

19 18 Input and Output Escape sequences: a special sequence of characters preceded by a backslash (\) Escape Sequence \t \n \" \' \\ Meaning tab new line double quote single quote backslash

20 19 Types of Output Stream ByteArrayOutputStrem FileOutputStrem PipeOutputStrem Block of Memory File Pipe(to another thread) Writes to

21 20 Types of Input Stream ByteArrayInputStrem FileInputStrem PipeInputStrem Block of Memory File Pipe(to another thread) Reads from

22 21 Input From Keyboard The Java API allows you to create many kinds of streams to perform various kinds of I/O To read character strings, we will convert the System.in stream to another kind of stream using: BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); This declaration creates a new stream called stdin

23 22 Echo.java import java.io.*; class Echo { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); String message; System.out.println ("Enter a line of text:"); message = stdin.readLine(); System.out.println ("You entered: \"" + message + "\""); } // method main } // class Echo

24 23 Buffers As you type, the characters are stored in an input buffer When you press enter, the program begins processing the data Output information is temporarily stored in an output buffer The output buffer can be explicitly flushed (sent to the screen) using the flush method See Python.java

25 24 Numeric Input Converting a string into the integer value: value = Integer.parseInt(my_string); A value can be read and converted in one line: num = Integer.parseInt(stdin.readLine()); C: scanf(“%d\n”, num);

26 25 Addition2.java import java.io.*; class Addition2 { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); int num1, num2; System.out.println ("Enter a number: "); num1 = Integer.parseInt (stdin.readLine()); System.out.println ("Enter another number:"); num2 = Integer.parseInt (stdin.readLine()); System.out.println ("The sum is " + (num1+num2)); } // method main } // class Addition2

27 26 Controlling Program Flow Essentially same as C/C++ if statement: if (condition) statement; Relational operators: Operator == != < <= > <= Meaning equal to not equal to less than less than or equal to greater than greater than or equal to

28 27 Controlling Program Flow if-else statement if (condition) statement1; else statement2; while statement: while (condition) statement; Note: Avoid infinite loop (logic error)

29 28 Controlling Program Flow if-else statement if (condition) statement1; else statement2; while statement:for statemetn: while (condition)for (e1;e2;e3) statement; statement; Note: Avoid infinite loop (logic error)

30 29 import java.io.*; class Right_Triangle { // bad bad bad RightTriangle public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); int hypotenuse_sq; // bad hypotenuseSquare System.out.println ("Enter side 1:"); int side1 = Integer.parseInt (stdin.readLine()); System.out.println ("Enter side 2:"); int side2 = Integer.parseInt (stdin.readLine()); System.out.println ("Enter the hypotenuse:"); int side3 = Integer.parseInt (stdin.readLine()); hypotenuse_sq = (side1 * side1) + (side2 * side2); if ((side3*side3) == hypotenuse_sq) System.out.println ("It is a right triangle."); else System.out.println ("It is not a right triangle."); } // method main } // class Right_Triangle

31 30 Conclusion 完成 Lecture 3 休息十分鐘! primitive data types: predefined data types in Java Size of each primitive data type is machine independent Beware of scope(variables can not be redefined in blocks) Java I/O is complicated. IO is based input streams and output streams


Download ppt "1 Course Lectures Available on line:"

Similar presentations


Ads by Google