1 lecture 3 program elements instructors: fu-chiung cheng ( 鄭福炯 ) associate professor computer...

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

Upload: timothy-walters

Post on 04-Jan-2016

232 views

Category:

Documents


3 download

TRANSCRIPT

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

1

Lecture 3Program Elements

Instructors: Fu-Chiung Cheng

( 鄭福炯 )Associate Professor

Computer Science & EngineeringTatung Institute of Technology

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

2

Outline

• Data types• Variable declaration and use• Decisions and loops• Input and output

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

3

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 4: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

4

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 5: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

5

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 6: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

6

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 7: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

7

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 8: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

8

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 9: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

9

Primitive Data Types

•Same operations as C/C++• Size of each data type is machine independent

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

10

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 Typeint

doublechar

boolean

Wrapper ClassIntegerDouble

CharacterBoolean

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

11

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 12: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

12

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 13: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

13

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 14: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

14

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 15: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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 16: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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 17: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

17

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 18: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

18

Types of Output Stream

ByteArrayOutputStrem

FileOutputStrem

PipeOutputStrem

Block of Memory

File

Pipe(to another thread)

Writes to

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

19

Types of Input Stream

ByteArrayInputStrem

FileInputStrem

PipeInputStrem

Block of Memory

File

Pipe(to another thread)

Reads from

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

20

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 21: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

21

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 22: 1 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

22

Open File

public class Scanner {

protected static DataInputStream inputStream;

Scanner(String fileName) throws IOException {

inputStream = new DataInputStream

(new FileInputStream (fileName));

}

}

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

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 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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 Lecture 3 Program Elements Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology

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