bai giang java co ban - java cơ bản - bai 2

Post on 26-May-2015

468 views 4 download

Tags:

description

bài giảng java cơ bản, bài 2/5. Nơi giảng: VTC academy Người giảng: Hoàng Văn Hậu

Transcript of bai giang java co ban - java cơ bản - bai 2

Introduction toJava Programming

Y. Daniel LiangEdited by Hoàng Văn Hậu – VTC Academy – THSoft co.,ltd

https://play.google.com/store/apps/developer?id=THSoft+Co.,Ltd

VTC Academy THSoft Co.,Ltd 2

Introduction

Course Objectives Organization of the Book

VTC Academy THSoft Co.,Ltd 3

Course Objectives Upon completing the course, you will understand

– Create, compile, and run Java programs

– Primitive data types

– Java control flow

– Methods– Arrays (for teaching Java in two semesters, this could be the end)

– Object-oriented programming

– Core Java classes (Swing, exception, internationalization, multithreading, multimedia, I/O, networking, Java Collections Framework)

VTC Academy THSoft Co.,Ltd 4

Course Objectives, cont.

You will be able to – Develop programs using Eclipse IDE– Write simple programs using primitive data

types, control statements, methods, and arrays.– Create and use methods– Write interesting projects

VTC Academy THSoft Co.,Ltd 5

Session 02: Control statement

Swith Case statement While, do while statement For statement Continue, break, return Array in Java String in Java Exception and debuging

VTC Academy THSoft Co.,Ltd 6

switch Statementsswitch (year) { case 7: annualInterestRate = 7.25; break; case 15: annualInterestRate = 8.50; break; case 30: annualInterestRate = 9.0; break; default: System.out.println( "Wrong number of years, enter 7, 15, or 30");}

Eclipse shortcut key:S + Ctrl + Space

VTC Academy THSoft Co.,Ltd 7

switch Statement Flow Chart

VTC Academy THSoft Co.,Ltd 8

switch Statement RulesThe switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses.

The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. (The case statements are executed in sequential order.)

The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed.

VTC Academy THSoft Co.,Ltd 9

switch Statement Rules, cont.

The default case, which is optional, can be used to perform actions when none of the specified cases is true. The order of the cases (including the default case) does not matter. However, it is a good programming style to follow the logical sequence of the cases and place the default case at the end.

VTC Academy THSoft Co.,Ltd 10

Actions on Eclipse

Open Eclipse IDE– Create project: Session2Ex– Create java class: Ex2WithSwitch.java

RunRunSourceSource

VTC Academy THSoft Co.,Ltd 11

Repetitions

while Loops

do-while Loops for Loops

break and continue

VTC Academy THSoft Co.,Ltd 12

while Loop Flow Chartwhile (continuation-condition) {

// loop-body;

}

Eclipse shortcut key:w + Ctrl + Space

VTC Academy THSoft Co.,Ltd 13

while Loop Flow Chart, cont.

int i = 0;while (i < 100) { System.out.println( "Welcome to Java!"); i++;}

false

true

System.out.println("Welcoem to Java!"); i++;

Next Statement

(i < 100)

i = 0;

VTC Academy THSoft Co.,Ltd 14

do-while Loop

false

true

Statement(s)

NextStatement

Continue condition?

do {

// Loop body;

} while (continue-condition);

Eclipse shortcut key:d + Ctrl + Space

VTC Academy THSoft Co.,Ltd 15

for Loopsfor (initial-action; loop-continuation-condition;

action-after-each-iteration) { //loop body;}

int i = 0;while (i < 100) { System.out.println("Welcome to Java! ” + i); i++; }

Example:

int i;for (i = 0; i < 100; i++) { System.out.println("Welcome to Java! ” + i); }

VTC Academy THSoft Co.,Ltd 16

for Loop Flow Chartfor (initial-action; loop-continuation-condition; action-after-each-iteration) { //loop body;}

Eclipse shortcut key:f + Ctrl + Space

VTC Academy THSoft Co.,Ltd 17

Actions on Eclipse Open Eclipse IDE

– Create project: Session2Ex– Create java class: Ex2WithWhile.java– Change the rule of games.– Exception input– Example debug project

RunRunSourceSource

VTC Academy THSoft Co.,Ltd 18

Arrays

int[] sourceArray = {2, 3, 1, 5, 10};

int[] targetArray = new int[sourceArray.length];

float[] f = new float[20];

Set data to array:

for (int i = 0; i < sourceArrays.length; i++)

targetArray[i] = sourceArray[i];

VTC Academy THSoft Co.,Ltd 19

Multidimensional ArraysDeclaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays

int[][] matrix = new int[10][10]; orint matrix[][] = new int[10][10];matrix[0][0] = 3;

for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { matrix[i][j] = (int)(Math.random()*1000); }double[][] x;

VTC Academy THSoft Co.,Ltd 20

Multidimensional Array Illustration

0 1 2 3 4

0

7

0 1 2 3 4

1 2 3 4

0 1 2 3 4 matrix[2][1] = 7;

matrix = new int[5][5];

3

7

0 1 2

0 1 2

int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };

1

2

3

4

5

6

8

9

10

11

12

VTC Academy THSoft Co.,Ltd 21

Declaring, Creating, and Initializing Using Shorthand Notations

You can also use a shorthand notation to declare, create and initialize a two-dimensional array. For example,

int[][] array = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9},

{10, 11, 12}

};

This is equivalent to the following statements: int[][] array = new int[4][3];

array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;

array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;

array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;

array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;

VTC Academy THSoft Co.,Ltd 22

Lengths of Multidimensional Arrays

int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};array.lengtharray[0].lengtharray[1].lengtharray[2].length

Illustration on code

VTC Academy THSoft Co.,Ltd 23

Actions on Eclipse

Open Eclipse IDE– Create project: Session2Ex– Create java class: Ex2WithArray.java– Add String type– Change rule of games

RunRunSourceSource

VTC Academy THSoft Co.,Ltd 24

Action on EclipseExample 2.1

Adding and Multiplying Two Matrices

Objective: Use two-dimensional arrays to create two matrices, and then add and multiply the two matrices.

TestMatrixOperationTestMatrixOperation RunRun

55555454535352525151

45454444434342424141

35353434333332323131

25252424232322222121

15151414131312121111

5554535251

4544434241

3534333231

2524232221

1514131211

5554535251

4544434241

3534333231

2524232221

1514131211

bababababa

bababababa

bababababa

bababababa

bababababa

bbbbb

bbbbb

bbbbb

bbbbb

bbbbb

aaaaa

aaaaa

aaaaa

aaaaa

aaaaa

VTC Academy THSoft Co.,Ltd 25

Example 2.2 (cont) Adding and Multiplying Two Matrices

5554535251

4544434241

3534333231

2524232221

1514131211

5554535251

4544434241

3534333231

2524232221

1514131211

5554535251

4544434241

3534333231

2524232221

1514131211

ccccc

ccccc

ccccc

ccccc

ccccc

bbbbb

bbbbb

bbbbb

bbbbb

bbbbb

aaaaa

aaaaa

aaaaa

aaaaa

aaaaa

cij = ai1b1j+ai2b2j+ai3b3j+ai4b4j+ai5b5j

VTC Academy THSoft Co.,Ltd 26

Action on class

Teacher – hauc2@yahoo.com– 0984380003– https://play.google.com/store/search?q=thsoft+co&c=apps

Captions Members