1 Course Lectures Available on line: .
-
Upload
erik-french -
Category
Documents
-
view
216 -
download
0
Transcript of 1 Course Lectures Available on line: .
![Page 1: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/1.jpg)
1
Course Lectures
Available on line: http://www.cse.ttit.edu.tw/fccheng
![Page 2: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/2.jpg)
2
Lecture 3Program Elements
Instructors: Fu-Chiung Cheng
( 鄭福炯 )Associate Professor
Computer Science & EngineeringTatung Institute of Technology
![Page 3: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/3.jpg)
3
Outline
• Data types• Variable declaration and use• Decisions and loops• Input and output
![Page 4: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/4.jpg)
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)
![Page 5: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/5.jpg)
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)
![Page 6: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/6.jpg)
6
Primitive Data Types
• Integers:
Type
byteshortintlong
Storage
8 bits16 bits32 bits64 bits
Min Value
-128-32,768-2,147,483,648< -9 x 1018
Max Value
12732,7672,147,483,647> 9 x 1018
Note that Built-in types (primitives) are not object handles handles: call by reference primitives: call by value
![Page 7: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/7.jpg)
7
Primitive Data Types
• Floating point:
Type
floatdouble
Storage
32 bits64 bits
ApproximateMin Value
-3.4 x 1038
-1.7 x 10308
ApproximateMax Value
3.4 x 1038
1.7 x 10308
S Exponent Mantisa
![Page 8: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/8.jpg)
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.
![Page 9: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/9.jpg)
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
![Page 10: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/10.jpg)
10
Primitive Data Types
•Same operations as C/C++• Size of each data type is machine independent
![Page 11: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/11.jpg)
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
intdoublechar
boolean
Wrapper Class
IntegerDouble
CharacterBoolean
![Page 12: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/12.jpg)
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;
![Page 13: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/13.jpg)
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); }}
![Page 14: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/14.jpg)
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
![Page 15: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/15.jpg)
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)
![Page 16: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/16.jpg)
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.
![Page 17: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/17.jpg)
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.inSystem.outSystem.err
Purpose
reading inputwriting outputwriting errors
Default Device
keyboardmonitormonitor
![Page 18: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/18.jpg)
18
Input and Output
• Escape sequences: a special sequence of characters preceded by a backslash (\)
Escape Sequence
\t\n\"\'\\
Meaning
tabnew line
double quotesingle quotebackslash
![Page 19: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/19.jpg)
19
Types of Output Stream
ByteArrayOutputStrem
FileOutputStrem
PipeOutputStrem
Block of Memory
File
Pipe(to another thread)
Writes to
![Page 20: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/20.jpg)
20
Types of Input Stream
ByteArrayInputStrem
FileInputStrem
PipeInputStrem
Block of Memory
File
Pipe(to another thread)
Reads from
![Page 21: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/21.jpg)
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
![Page 22: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/22.jpg)
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
![Page 23: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/23.jpg)
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
![Page 24: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/24.jpg)
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);
![Page 25: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/25.jpg)
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
![Page 26: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/26.jpg)
26
Controlling Program Flow
• Essentially same as C/C++• if statement: if (condition)
statement;• Relational operators:
Operator
==!=<<=><=
Meaning
equal tonot equal to
less thanless than or equal to
greater thangreater than or equal to
![Page 27: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/27.jpg)
27
Controlling Program Flow
• if-else statementif (condition) statement1;
else statement2;
• while statement: while (condition)
statement;
Note: Avoid infinite loop (logic error)
![Page 28: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/28.jpg)
28
Controlling Program Flow
• if-else statementif (condition) statement1;
else statement2;
• while statement: for statemetn: while (condition) for (e1;e2;e3)
statement; statement;
Note: Avoid infinite loop (logic error)
![Page 29: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/29.jpg)
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
![Page 30: 1 Course Lectures Available on line: .](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649e1b5503460f94b0a1eb/html5/thumbnails/30.jpg)
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