1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Bài giảng điện tử môn tin học: Fundamentals of Programming pptx

164 631 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Fundamentals of Programming
Tác giả Võ Phương Bình
Trường học Dai Hoc Lao Dong - Thanh Pho Ho Chi Minh (DLU)
Chuyên ngành Information Technology
Thể loại electronic lecture
Năm xuất bản 2014
Thành phố Ho Chi Minh City
Định dạng
Số trang 164
Dung lượng 1,57 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

08/13/14 Võ Phương Bình - ITFAC - DLU 24Compiling and Running a Program  Where are the files stored in the directory?. 08/13/14 Võ Phương Bình - ITFAC - DLU 27Package  The second line

Trang 1

08/13/14 Võ Phương Bình - ITFAC - DLU 1

 Chapter 1: Introduction to Java

 Chapter 2: Primitive Data Types and

Trang 2

08/13/14 Võ Phương Bình - ITFAC - DLU 2

Chapter 1:

Introduction to Java

 What Is Java?

 Getting Started With Java Programming

 Create, Compile and Running a Java

Application

Trang 3

08/13/14 Võ Phương Bình - ITFAC - DLU 3

Trang 4

08/13/14 Võ Phương Bình - ITFAC - DLU 4

Trang 5

08/13/14 Võ Phương Bình - ITFAC - DLU 5

Trang 6

08/13/14 Võ Phương Bình - ITFAC - DLU 6

 Java Micro Edition (J2ME). 

 J2ME can be used to develop applications for mobile 

devices such as cell phones. 

Trang 7

Java2 Micro Edition (J2ME™) Java 2 Platform

Standard desktop &

workstation applications Heavy duty server systems constrained devices Small & memory

Trang 8

08/13/14 Võ Phương Bình - ITFAC - DLU 8

Java IDE Tools

 IBM Visual Age for Java

 Forte by Sun Microsystems

Trang 9

08/13/14 Võ Phương Bình - ITFAC - DLU 9

Trang 10

08/13/14 Võ Phương Bình - ITFAC - DLU 10

Console

Trang 11

08/13/14 Võ Phương Bình - ITFAC - DLU 11

GUI (with JFC)

Trang 12

08/13/14 Võ Phương Bình - ITFAC - DLU 12

Applet & Web

Trang 13

08/13/14 Võ Phương Bình - ITFAC - DLU 13

Database & Web

Trang 14

08/13/14 Võ Phương Bình - ITFAC - DLU 14

Mobile

Trang 15

08/13/14 Võ Phương Bình - ITFAC - DLU 15

Compare to C/C++/C#

 Similar to C/C++/C#

 Difference:

 Compiler

Trang 16

08/13/14 Võ Phương Bình - ITFAC - DLU 16

Traditional Compiler (C/C++)

Trang 17

08/13/14 Võ Phương Bình - ITFAC - DLU 17

Trang 18

08/13/14 Võ Phương Bình - ITFAC - DLU 18

Trang 19

08/13/14 Võ Phương Bình - ITFAC - DLU 19

Getting Started with Java

Programming

 A Simple Java Application

 Compiling Programs

 Executing Applications

Trang 20

08/13/14 Võ Phương Bình - ITFAC - DLU 20

public class Welcome {

public static void main(String[] args) { System.out.println("Welcome to

Java!");

}

}

Trang 21

08/13/14 Võ Phương Bình - ITFAC - DLU 21

Creating and Compiling Programs

 On command line

 javac file.java

Source Code Create/Modify Source Code

Compile Source Code i.e javac Welcome.java

Bytecode

Run Byteode i.e java Welcome

Result

If compilation errors

If runtime errors or incorrect result

Trang 22

08/13/14 Võ Phương Bình - ITFAC - DLU 22

on Sun Solaris

Java Interpreter

on Linux

Bytecode

Trang 23

08/13/14 Võ Phương Bình - ITFAC - DLU 23

Example

javac Welcome.java

java Welcome

output:

Trang 24

08/13/14 Võ Phương Bình - ITFAC - DLU 24

Compiling and Running a Program

 Where are the files stored in the directory?

Java source files and class files for Chapter 2

chapter19 Java source files and class files for Chapter 19

Welcome.java~

Trang 25

08/13/14 Võ Phương Bình - ITFAC - DLU 25

Anatomy of a Java Program

Trang 26

08/13/14 Võ Phương Bình - ITFAC - DLU 26

Comments

 Similar to C/C++

 In Java, comments are preceded by two

slashes (//) in a line, or enclosed

between /* and */ in one or multiple lines.

 When the compiler sees //, it ignores all text after // in the same line When it sees /*, it scans for the next */ and ignores any text between /* and */  

Trang 27

08/13/14 Võ Phương Bình - ITFAC - DLU 27

Package

  The second line in the program 

(package chapter1;) specifies a package  name, chapter1, for the class Welcome.

Trang 28

08/13/14 Võ Phương Bình - ITFAC - DLU 28

Reserved Words

Trang 29

08/13/14 Võ Phương Bình - ITFAC - DLU 29

Trang 30

08/13/14 Võ Phương Bình - ITFAC - DLU 30

Blocks

 A pair of braces in a program forms a block that groups components of a

program

public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!");

} }

Class block Method block

Trang 31

08/13/14 Võ Phương Bình - ITFAC - DLU 31

Classes

Java construct.

blueprint for objects To

program in Java, you must

understand classes and be

able to write and use them

Trang 32

08/13/14 Võ Phương Bình - ITFAC - DLU 32

Trang 33

08/13/14 Võ Phương Bình - ITFAC - DLU 33

main Method

The main method provides the control of

program flow The Java interpreter executes the application by invoking the main method

The main method looks like this:

public static void main(String[] args) {

 // Statements;

}

Trang 34

08/13/14 Võ Phương Bình - ITFAC - DLU 34

Displaying Text in a Message

Dialog Box

showMessageDialog method in the JOptionPane class.

many predefined classes in

the Java system.

Trang 35

08/13/14 Võ Phương Bình - ITFAC - DLU 35

Trang 36

08/13/14 Võ Phương Bình - ITFAC - DLU 36

The exit Method

and stop all threads.

thread is spawned to run the program When the showMessageDialog is

invoked, a separate thread is spawned

to run this method The thread is not terminated even you close the dialog box To terminate the thread, you

have to invoke the exit method.

Trang 37

08/13/14 Võ Phương Bình - ITFAC - DLU 37

Trang 38

08/13/14 Võ Phương Bình - ITFAC - DLU 38

Chapter 2: Primitive Data Types and

Operations

 Introduce Programming with an Example

 Identifiers, Variables, and Constants

 Primitive Data Types

 byte, short, int, long, float, double, char,

 Getting Input from Input Dialog Boxes

 Case Studies (Computing Mortgage, and Computing

Changes)

 Style and Documentation Guidelines

 Syntax Errors, Runtime Errors, and Logic Errors

Trang 39

08/13/14 Võ Phương Bình - ITFAC - DLU 39

Identifiers

 An identifier is a sequence of characters that consist of letters, digits,

underscores (_), and dollar signs ($)

 An identifier must start with a letter,

an underscore (_), or a dollar sign ($)

It cannot start with a digit

 An identifier cannot be a reserved word

 An identifier cannot be true , false , or

 An identifier can be of any length.

Trang 40

08/13/14 Võ Phương Bình - ITFAC - DLU 40

area + " for radius "+radius);

// Compute the second area

Trang 41

08/13/14 Võ Phương Bình - ITFAC - DLU 41

// character variable;

Trang 42

08/13/14 Võ Phương Bình - ITFAC - DLU 42

Trang 43

08/13/14 Võ Phương Bình - ITFAC - DLU 43

Declaring and Initializing

in One Step

 int x = 1;

 double d = 1.4;

 float f = 1.4;

Trang 44

08/13/14 Võ Phương Bình - ITFAC - DLU 44

Trang 45

08/13/14 Võ Phương Bình - ITFAC - DLU 45

Numerical Data Types

Trang 46

08/13/14 Võ Phương Bình - ITFAC - DLU 46

Operators

+, -, *, /, and %

5/2 yields an integer 2.

5.0/2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)

Trang 47

08/13/14 Võ Phương Bình - ITFAC - DLU 47

NOTE

• Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy.

Trang 48

08/13/14 Võ Phương Bình - ITFAC - DLU 48

Number Literals

A literal is a constant value that

appears directly in the program

• For example, 34, 1,000,000, and 5.0 are literals in the following statements:

int i = 34;

long l = 1000000;

double d = 5.0;

Trang 49

08/13/14 Võ Phương Bình - ITFAC - DLU 49

Integer Literals

integer variable as long as it can fit into the variable A compilation error would occur if the literal were too large for the variable to hold.

would cause a compilation error, because

1000 cannot be stored in a variable of the byte type.

denote an integer literal of the long type, append it with the letter L or l.

Trang 50

08/13/14 Võ Phương Bình - ITFAC - DLU 50

Floating-Point Literals

 Floating-point literals are written with a decimal point By default, a floating-point literal is treated as a double type value.

 For example, 5.0 is considered a double value, not a float value.

 You can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or D.

 For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number

Trang 51

08/13/14 Võ Phương Bình - ITFAC - DLU 51

Scientific Notation

specified in scientific notation.

equivalent to 0.0123456

it can be either in lowercase or uppercase

Trang 52

08/13/14 Võ Phương Bình - ITFAC - DLU 52

Arithmetic Expressions

is translated to

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x +

9*(4/x + (9+x)/y)

Trang 53

08/13/14 Võ Phương Bình - ITFAC - DLU 53

Trang 54

08/13/14 Võ Phương Bình - ITFAC - DLU 54

Increment and Decrement Operators

Trang 55

08/13/14 Võ Phương Bình - ITFAC - DLU 55

Increment and Decrement Operators, cont.

int newNum = 10*i;

Equivalent to

Trang 56

08/13/14 Võ Phương Bình - ITFAC - DLU 56

Increment and Decrement Operators, cont.

 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

Trang 57

08/13/14 Võ Phương Bình - ITFAC - DLU 57

Assignment Expressions and

Assignment Statements

 Prior to Java 2, all the expressions can be used as statements

 Since Java 2, only the following types

of expressions can be statements:

variable op= expression; // Where op is +, -, *, /, or %

++variable;

variable++;

variable;

variable ;

Trang 58

08/13/14 Võ Phương Bình - ITFAC - DLU 58

Numeric Type Conversion

Consider the following statements:

Trang 59

08/13/14 Võ Phương Bình - ITFAC - DLU 59

Trang 60

08/13/14 Võ Phương Bình - ITFAC - DLU 60

Type Casting, cont.

Trang 61

08/13/14 Võ Phương Bình - ITFAC - DLU 61

Character Data Type

• char letter = 'A'; (ASCII)

char numChar = '4'; (ASCII)

char letter = '\u0041'; Unicode) char numChar = '\u0034';(Unicode)

• Special characters

char tab = ‘\t’;

Trang 62

08/13/14 Võ Phương Bình - ITFAC - DLU 62

Trang 63

08/13/14 Võ Phương Bình - ITFAC - DLU 63

Casting between char and

Numeric Types

• int i = ' a ' ; // Same as

int //i = (int) ' a ' ;

• char c = 97; // Same as char //c = (char)97;

Trang 64

08/13/14 Võ Phương Bình - ITFAC - DLU 64

The boolean Type and Operators

• boolean lightsOn = true;

• boolean lightsOn = false;

• boolean b = (1 > 2);

 && (and) (1 < x) && (x < 100)

 || (or) (lightsOn) || (isDayTime)

 ! (not) !(isStopped)

Trang 65

08/13/14 Võ Phương Bình - ITFAC - DLU 65

Trang 66

08/13/14 Võ Phương Bình - ITFAC - DLU 66

Trang 67

08/13/14 Võ Phương Bình - ITFAC - DLU 67

Truth Table for Operator !

Truth Table for Operator !

Operand !Operand

Trang 68

08/13/14 Võ Phương Bình - ITFAC - DLU 68

Truth Table for Operator &&

Operand1 Operand2 Operand1 && Operand2

Trang 69

08/13/14 Võ Phương Bình - ITFAC - DLU 69

Truth Table for Operator ||

Operand1 Operand2 Operand1 ||

Operand2

Trang 70

08/13/14 Võ Phương Bình - ITFAC - DLU 70

Truth Table for Operator ^

Operand1 Operand2 Operand1 ^

Operand2

Trang 71

08/13/14 Võ Phương Bình - ITFAC - DLU 71

The & and | Operators

&&: conditional AND operator

&: unconditional AND operator

Trang 72

08/13/14 Võ Phương Bình - ITFAC - DLU 72

The & and | Operators

If x is 1, what is x after this

Trang 73

08/13/14 Võ Phương Bình - ITFAC - DLU 73

Operator Precedence

How to evaluate

3 + 4 * 4 > 5 * (4 + 3) - ++i

Trang 74

08/13/14 Võ Phương Bình - ITFAC - DLU 74

 * , / , % (Multiplication, division, and modulus)

 + , - (Binary addition and subtraction)

 && (Conditional AND) Short-circuit AND

 || (Conditional OR) Short-circuit OR

 = , += , -= , *= , /= , %= (Assignment operator)

Trang 75

08/13/14 Võ Phương Bình - ITFAC - DLU 75

Operator Associativity

• When two operators with the same precedence

are evaluated, the associativity of the

operators determines the order of

evaluation.

• All binary operators except assignment

operators are left-associative.

a – b + c – d is equivalent to ((a – b)

+ c) – d

Assignment operators are right-associative

Therefore, the expression

a = b += c = 5 is equivalent to a = (b +=

(c = 5))

Trang 76

08/13/14 Võ Phương Bình - ITFAC - DLU 76

Operand Evaluation Order

• The precedence and associativity

rules specify the order of the

operators, but do not specify the

order in which the operands of a

binary operator are evaluated

• Operands are evaluated from left to

right in Java

The left-hand operand of a binary

operator is evaluated before any part

of the right-hand operand is

evaluated

Trang 77

08/13/14 Võ Phương Bình - ITFAC - DLU 77

Operand Evaluation Order,

cont.

If no operands have side effects that

change the value of a variable, the order

of operand evaluation is irrelevant

Interesting cases arise when operands do

have a side effect

For example, x becomes 1 in the following code, because a is evaluated to 0 before +

+a is evaluated to 1

int a = 0;

int x = a + (++a);

But x becomes 2 in the following code,

because ++a is evaluated to 1, then a is

evaluated to 1.

int a = 0;

int x = ++a + a;

Trang 78

08/13/14 Võ Phương Bình - ITFAC - DLU 78

Getting Input from Input Dialog

Boxes

 String string = JOptionPane.showInputDialog(null,

“Prompt Message”, “Dialog Title”,

JOptionPane.QUESTION_MESSAGE));

 Where x is a string for the prompting message and y is

a string for the title of the input dialog box.

Trang 79

08/13/14 Võ Phương Bình - ITFAC - DLU 79

Convertting Strings to Integers

 The input returned from the input dialog box is a string If you enter a numeric

value such as 123, it returns “123” To

obtain the input as a number, you have to convert a string into a number

 To convert a string into an int value, you can use the static parseInt method in the Integer class as follows:

int intValue =

Integer.parseInt(intString);

where intString is a numeric string such

as “123”

Trang 80

08/13/14 Võ Phương Bình - ITFAC - DLU 80

Convertting Strings to Doubles

value, you can use the static

parseDouble method in the Double class

as follows:

double doubleValue = Double.parseDouble

(doubleString);

where doubleString is a numeric string such as “123.45”.

Trang 81

08/13/14 Võ Phương Bình - ITFAC - DLU 81

Programming Style and

Trang 82

08/13/14 Võ Phương Bình - ITFAC - DLU 82

Appropriate Comments

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting

techniques it uses

program

Trang 83

08/13/14 Võ Phương Bình - ITFAC - DLU 83

 If the name consists of several words,

concatenate all in one, use lowercase

for the first word, and capitalize the

first letter of each subsequent word in

the name

 For example, the variables radius and

area , and the method computeArea

Trang 84

08/13/14 Võ Phương Bình - ITFAC - DLU 84

Naming Conventions, cont.

 Class names:

 Capitalize the first letter of

each word in the name For

example, the class name

ComputeArea

 Constants:

 Capitalize all letters in

constants For example, the

constant PI

Trang 85

08/13/14 Võ Phương Bình - ITFAC - DLU 85

Proper Indentation and Spacing

Trang 86

08/13/14 Võ Phương Bình - ITFAC - DLU 86

Block Styles

Use end-of-line style for braces.

public class Test {

public static void main(String[] args) {

System.out.println("Block Styles");

} }

public class Test { public static void main(String[] args) { System.out.println("Block Styles");

} }

End-of-line style

Next-line style

Trang 87

08/13/14 Võ Phương Bình - ITFAC - DLU 87

Trang 88

08/13/14 Võ Phương Bình - ITFAC - DLU 88

Compilation Errors

public class ShowSyntaxErrors {

public static void main(String[] args) {

i = 30;

System.out.println(i+4);

}

}

Trang 89

08/13/14 Võ Phương Bình - ITFAC - DLU 89

Runtime Errors

public class ShowRuntimeErrors {

public static void main(String[] args) {

int i = 1 / 0;

}

}

Trang 90

08/13/14 Võ Phương Bình - ITFAC - DLU 90

Logic Errors

public class ShowLogicErrors {

// Determine if a number is between 1 and 100 inclusively

public static void main(String[] args) {

// Prompt the user to enter a number

String input = JOptionPane.showInputDialog(null,

"Please enter an integer:",

"ShowLogicErrors", JOptionPane.QUESTION_MESSAGE);

int number = Integer.parseInt(input);

// Display the result

System.out.println("The number is between 1 and 100, "

Ngày đăng: 11/08/2014, 22:23

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm