การเขียนโปรแกรมเชิงวัตถุ OOP:Object Oriented...

Preview:

Citation preview

อาจารย ดร. นฐพงศ สงเนยม

http://www.siam2dev.com

xnattapong@hotmail.com

Last updated : 06-01-2561

การเขยนโปรแกรมเชงวตถ(OOP:Object Oriented Programming)

เนอหา• Lec06: Java Array And String

Lec06: Java Array And String

โดย อ. นฐพงศ สงเนยม

http://www.siam2dev.netxnattapong@homtail.comnattapong@siam2dev.com

06-01-2560

Java Array แอรเรย (Array)หมายถง กลม (group) ของขอมลทเปนประเภทเดยวกน

Array ในภาษาจาวา ไมใชเปนเพยงเนอทในหนวยความจาทจองไวสาหรบประเภทขอมล แตเปนวตถ (Object) เปนกลม (group) ของขอมลทเปนประเภทเดยวกน (ทงประเภท Primitive Data Type และ Reference Type)

PDT , ADT

Examples of Java Array

Element Index

Length=3[0][1][2]

xxx ‘a’‘b’‘c’

xxxyyy

Memory Address

zzzMemory Address

aaaDeclare Aas an Array of Character

A

0 1 N-1

Stack

Variable เกบคาไดเพยงคาเดยว

int x = 5;5

5 8 7 9 0

element index

int[] i;

int i[];

i = new int[5];

Index =0 Index =n-1

0 1 2 3 4

Declaring Array in Java• การประกาศ Array ในภาษาจาวา ทาไดโดย

type[ ] variable; หรอ type variable[ ];

ประกาศตวแปร Array เพอเปนตวอางองไปยง Array เปนการจองเนอทสาหรบตวแปรอางอง (Reference Variable)

ไมระบขนาดของ Array

คา Default value เปน null

Array เปน Object???

Examples of Declaring Array

• ตวอยาง การประกาศ Array

int[ ] i; หรอ int i[ ];

int[ ] i,j; หรอ int i[ ], j[ ];

double[ ] d; หรอ double d[ ];

char[ ] A; หรอ char A[ ];

BankAccount[ ] ba; หรอ BankAccount ba[ ];

Point[ ] pt; หรอ Point pt[ ];

Creating Array in Java

1 char[ ] c;2 c = new char[26];3 for (int i=0; i<26; i++) {4 c[i] = (char)('a'+i);5 }

YYYMemory Address

XXX

Declare C as an Array of Character

c

After Line 1

null

YYY

• ตวอยางการสราง Array ของ Primitive Data Type

แบบฝกหด 6.1

จงเขยนโปรแกรมรบ นาหนก นกศกษามา 10 และหา นาหนก เฉลย

สงทตองร

– Array

– AVG

– Loop

– รบคาจากแปนพมพ Scanner

แบบฝกหด 6.1

จงเขยนโปรแกรมรบ นาหนก นกศกษามา 10 และหา นาหนก เฉลย

MAX ,MIN

จงแสดงคาทอยใน Array ออกมา

Creating Array in Java

1 char[ ] c;2 c = new char[26];3 for (int i=0; i<26; i++) {4 c[i] = (char)('a'+i);5 }

YYYMemory Address

XXX

C

Executing Line 2

null

Declare C as an Array of Character

YYY

aaaMemory Address

XXX

CElement Index

Length=26

[0][1][2]

YYYaaabbb

Memory Address

zzz

ccc

[25]

Executing Line 2

null

Declare C as an Array of Character

YYYYYY

YYY

YYYaaaMemory Address

XXX

CElement Index

Length=26

[0][1][2]

YYYaaabbb

Memory Address

zzz

ccc

[25]

After Line 2

null

Declare C as an Array of Character

YYYYYY

YYY

YYY

• ตวอยางการสราง Array ของ Primitive Data Type

Creating Array in Java

1 char[ ] c;2 c = new char[26];3 for (int i=0; i<26; i++) {4 c[i] = (char)('a'+i);5 }

aaaMemory Address

XXX

CElement Index

Length=26

[0][1][2]

‘b’aaabbb

Memory Address

zzz

ccc

[25]

After Line 3 and 4

null

Declare C as an Array of Character

YYY‘z’

‘c’

‘a’

Memory Address

aaaMemory Address

XXX

CElement Index

Length=26

[0][1][2]

YYYaaabbb

zzz

ccc

[25]

Executing Line 3 and 4

null

Declare C as an Array of Character

YYYYYY

YYY

‘a’

• ตวอยางการสราง Array ของ Primitive Data Type

Creating Array in Java

1 char[ ] c;2 c = new char[26];3 for (int i=0; i<26; i++) {4 c[i] = (char)('a'+i);5 }

aaaMemory Address

XXX

CElement Index

Length=26

[0][1][2]

‘b’aaabbb

Memory Address

zzz

ccc

[25]

After Line 5

null

Declare C as an Array of Character

YYY‘z’

‘c’

‘a’

• ตวอยางการสราง Array ของ Primitive Data Type

Initializing an Array

• การสราง Array โดยใหคาเรมตน• BankAccount account[ ];

account = new BankAccount[3]; account[0] = new BankAccount(1000);account[1] = new BankAccount(0);account[2] = new BankAccount (23052);

Array Bounds

• int data[ ];data = new int[5];for (int i=10; I < data.length; i--)

data[i] = i % 2;

• int a[ ] = { 2, 4, 6, 8, 10 };

MyDate.java

A Point Class

Class Diagram

Point

- xPos : int

- yPos : int

+ Point(initX : int, initY: int)

+ getX( ) : int

+ getY( ) : int

+ setPostion(X : int, Y: int)

Class Diagram

Point

setPostion(x, y)

getX()

yPos

xPos

Object Diagram

getY()

1 Point[ ] pt;2 pt = new Point[10];3 for (int i=0; i<pt.length; i++) {4 pt[i] = (Point)('a'+i);5 }

YYYMemory Address

XXX

Declare pt as an Array of Point Object

pt

After Line 1

null

YYY

• ตวอยางการสราง Array ของ Primitive Data Type

Creating Array in Java

1 Point[ ] pt;2 pt = new Point[10];3 for (int i=0; i<pt.length; i++) {4 pt[i] = new Point(i, i+1);5 }

aaaMemory Address

XXX

ptElement Index

Length=10

[0][1][2]

YYYaaabbb

Memory Address

zzz

ccc

[9]

After Line 2

null

Declare pt as an Array of Character

YYYYYY

YYY

YYYaaaMemory Address

XXX

ptElement Index

Length=10

[0][1][2]

YYYaaabbb

Memory Address

zzz

ccc

[9]

Executing Line 2

null

Declare pt as an Array of Character

YYYYYY

YYY

YYY

• ตวอยางการสราง Array ของ Primitive Data Type

Creating Array in Java

1 Point[ ] pt;2 pt = new Point[10];3 for (int i=0; i<pt.length; i++) {4 pt[i] = new Point(i, i+1); 5 }

aaa

Memory Address

XXX

pt

Element Index

Length= 10

[0][1][2]

(1,2)aaabbb

Memory Address

zzz

ccc

[9]

After Line 5

null

Declare pt as an Array of Character

YYY(9,10)

(2,3)

(0,1)aaa

Memory Address

XXX

pt

Element Index

Length= 10

[0][1][2]

(1,2)aaabbb

Memory Address

zzz

ccc

[9]

After Line 5

null

Declare pt as an Array of Character

YYY(9,10)

(2,3)

(0,1)

01

XY

Point

• ตวอยางการสราง Array ของ Primitive Data Type

Creating Array in Java

MyDate.java

A Line class• ตวอยางการสราง Array ของคลาส Line

Line

- startPt : Point

- endPt : Point

+ Line(start :Point, start : Point)

+ getStartPoint ( ) : Point

+ getEndPoint( ) : Point

+ setStartPoint (pt :Point)

+ setEndPoint (pt :Point)

Class Diagram

1 Line[ ] line;2 line = new Line[3];3 for (int i=0; i<line.length; i++) {4 Point pt1 = new Point(i);5 Point pt2 = new Point(i+1);6 line[i] = new Line(pt1,pt2); 7 }

Array Resizing in Java• ในภาษาจาวา

• ไมสามารถ resize Array ได• แตสามารถตวแปรอางองเดมเกบคาอางองไปยง Array ใหมได

• ตวอยาง

1 int myArray [ ] = new int[4];2 myArray = new int[6];

aaaXXX

myArray

nullaaabbb

dddccc

[0][1][2][3]

After Line 1

nullnull

null

1 int myArray [ ] = new int[4];2 myArray = new int[6];

aaaXXX

myArray

nullaaabbb

dddccc

[0][1][2][3]

Executing Line 2

nullnull

null null [0][1][2]

nulljjjkkk

nnnmmm

[3]nullnullnullnull

oooppp

[4][5]

1 int myArray [ ] = new int[4];2 myArray = new int[6];

jjjXXX

myArray

nullaaabbb

dddccc

[0][1][2][3]

After Line 2

nullnull

null null [0][1][2]

nulljjjkkk

nnnmmm

[3]nullnullnullnull

oooppp

[4][5]

จงสม ตว เลข 1- 100 แลวเกบคาใน array

Copying Array in Java

• ในภาษาจาวา ทาไดโดย•เรยกใชเมทธอด System.arrraycopy( );

• ตวอยาง

1 int elements [ ] = { 1,2,3,4 }; // original array2 int hold [ ] = { 10,9,8,7,6,5 }; // new larger array3 System.arraycopy(elements, 0, hold, 0, elements.length);

aaaXXX

elements[0][1][2]

2aaabbb

dddccc

[3]

After Line 2

43

1 jjjXXX

hold[0][1][2]

9jjjkkk

nnnmmm

[3]8

10

765

oooppp

[4][5]

Length of copy

Origin Target

Copying Array in Java

• ตวอยาง

1 int elements [ ] = { 1,2,3,4 }; // original array2 int hold [ ] = { 10,9,8,7,6,5 }; // new larger array3 System.arraycopy(elements, 0, hold, 0, elements.length);

aaaXXX

elements[0][1][2]

2aaabbb

dddccc

[3]

Executing Line 3

43

1 jjjXXX

hold[0][1][2]

9jjjkkk

nnnmmm

[3]8

10

765

oooppp

[4][5]

aaaXXX

elements[0][1][2]

2aaabbb

dddccc

[3]

After Line 3

43

1 jjjXXX

hold[0][1][2]

2jjjkkk

nnnmmm

[3]3

1

465

oooppp

[4][5]

Multidimensional Array in Java

• Multidimensional Array ในภาษาจาวา

Array ทมจานวนแถว และจานวนคอลมน ไมคงทเทากน

Array ทมจานวนแถว และ จานวนคอลมน คงทเทากน

int elements [ ][ ] = new int[4][5]; // 4 rows, 5 cols each

int[][] twoDim;twoDim [ 0] = new int[2]; // row#0, 2 cols twoDim [ 1] = new int[4]; // row#1, 4 colstwoDim [ 2] = new int[6]; // row#2, 6 colstwoDim [ 3] = new int[8]; // row#3, 8 cols

Metric

2 x 2

2 3

4 5

A =

2A = 4 6

8 10Metric Manipulation

A + B

A[1,1] = ?

Initializing Multidimensional Array

• การสราง Multidimensional Array โดยใหคาเรมตน• int[ ][ ] array_2d = {

{ 2, 4, 6, 8, 10 },{ 0, 1},{20, 21, 22}

};• long elements[ ][ ];

elements = new long[2][3]; for (int i=0; i<elements.length; i++)

for (int j=0; j<elements[i].length; j++) elements[i][j] = 0L;

Java String

Java String• สายอกษร (String) ในภาษาจาวา

ไมใชเปนเพยงเนอทในหนวยความจาทจองไวสาหรบประเภท

ขอมล แตเปนวตถ (Object) • ตวแปร String ในภาษาจาวา

เกบคาอางองของ String Object

เมอมการอางองคาไปยง String Object แลว จะไมสามารถ

เปลยนแปลงคาใน String Object ไดโดยตรง แตสามารถใหคาอางอง

ใหมไดString message = new String("Hello");

String message = "Hello";

หรอ

• การประกาศตวแปร String ในภาษาจาวา

String Declaration (1)

1 String empty = new String("");2 String message = "Hello";3 String repeat = message;

""

xxx

yyy

zzz

aaa

bbbString

""

emptystored at xxx

stored at aaa

AfterLine 1

aaaString""

stored at aaa

Conceptual View Memory Address View

Presenter
Presentation Notes
Programming is about solving problems. Compare this to a program for a concert, for example. The program contains a list of what is to happen, in the order in which it will occur. Give an example of a small recipe and point out the data and the processing (ingredients and method). Point out the data and processing in instructions to change a car tyre. A knitting pattern specifies how many balls of wool of what thickness you need, and what size knitting needles. I wonder how many students know how to knit these days :-). There may not be many students who have filled in a tax form yet, either.

String Declaration (2)

1 String empty = new String("");2 String message = "Hello";3 String repeat = message;

" Hello"

messagestored at yyy bbb

String”Hello"stored at bbb

stored at bbb

""

xxx

yyy

zzz

aaa

bbbString

""

emptystored at xxx

stored at aaa

AfterLine 2 aaa

String""

stored at aaa

Conceptual ViewMemory Address

View

String”Hello"

""

xxx

yyy

zzz

aaa

bbbString

""

emptystored at xxx

stored at aaa

AfterLine 1

aaaString""

stored at aaa

Conceptual View Memory Address View

• การประกาศตวแปร String ในภาษาจาวา

Presenter
Presentation Notes
Programming is about solving problems. Compare this to a program for a concert, for example. The program contains a list of what is to happen, in the order in which it will occur. Give an example of a small recipe and point out the data and the processing (ingredients and method). Point out the data and processing in instructions to change a car tyre. A knitting pattern specifies how many balls of wool of what thickness you need, and what size knitting needles. I wonder how many students know how to knit these days :-). There may not be many students who have filled in a tax form yet, either.

String Declaration (3)

1 String empty = new String("");2 String message = "Hello";3 String repeat = message;

Repeatstored at zzz

" Hello"message

stored at yyybbb

String”Hello"stored at bbb

stored at bbb

""

xxx

yyy

zzz

aaa

bbbString

""

emptystored at xxx

stored at aaa

AfterLine 3 aaa

String""

stored at aaa

Conceptual ViewMemory Address

View

String”Hello" bbb

" Hello"

messagestored at yyy bbb

String”Hello"stored at bbb

stored at bbb

""

xxx

yyy

zzz

aaa

bbbString

""

emptystored at xxx

stored at aaa

AfterLine 2 aaa

String""

stored at aaa

Conceptual ViewMemory Address

View

String”Hello"

• การประกาศตวแปร String ในภาษาจาวา

Presenter
Presentation Notes
Programming is about solving problems. Compare this to a program for a concert, for example. The program contains a list of what is to happen, in the order in which it will occur. Give an example of a small recipe and point out the data and the processing (ingredients and method). Point out the data and processing in instructions to change a car tyre. A knitting pattern specifies how many balls of wool of what thickness you need, and what size knitting needles. I wonder how many students know how to knit these days :-). There may not be many students who have filled in a tax form yet, either.

The “String” Class• String Class

คาใน String Object ไมสามารถเปลยนแปลงได แต

สามารถใหคาอางองจากตวแปร String ใหม ได• Methods ของ String Class Create New String

concat, replace, subString, toLowerCase, toUpperCase Search

endswith, startswith, indexOf, lastIndexOf Comparison

equals, equalsIgnoreCase, CompareTo others

charAt, length

String Comparison• การเปรยบเทยบ String ในภาษาจาวา เปรยบเทยบคาใน String Object วาเทากนหรอไม ใชเมทธอด boolean equals(String s)

เปรยบเทยบวาตวแปร String อางองถง String Object เดยวกนหรอไม ใชโอเปอรเรเตอร (Operator) “==”

เปรยบเทยบคาใน String Object เรยงตามลาดบตวอกษร ใชเมทธอด int compareTo(String s) return คา 0 เมอเปนคาเดยวกน, คาจานวนเตมบวกเมอ String Object นมากกวา s และ คาจานวนเตมลบ เมอ String Object นนอยกวา s

String greeting = new String("Hello" );String message = "Hello";if (greeting.equals(message)) { System.out.println("equal content!"); }else { System.out.println("content NOT equal!"); }

Comparing String Contents• ตวอยาง

"Hello"

messagestored at yyy bbb

String"Hello"stored at bbb

stored at bbb

"Hello"

xxx

yyy

zzz

aaa

bbbString"Hello"

greetingstored at xxx

stored at aaa

aaa

String"Hello"

stored at aaa

Conceptual ViewMemory Address

View

String"Hello"

ตองการ จะเปรยบเทยบ

A = “adfasdfeFEFadfasdfEddfhdfgfhjdd”

B = “adsdfewerrassaFEDadsfaSASDFE”

Comparing String Reference

String greeting = new String("Hello" );String message = "Hello";if (greeting == message) { System.out.println("equal reference!"); }else { System.out.println(”reference NOT equal!"); }

"Hello"

messagestored at yyy bbb

String"Hello"stored at bbb

stored at bbb

"Hello"

xxx

yyy

zzz

aaa

bbbString"Hello"

greetingstored at xxx

stored at aaa

aaa

String"Hello"

stored at aaa

Conceptual ViewMemory Address

View

String"Hello"

• ตวอยาง

Comparing String Lexicographical Order

String greeting = new String("Hello" );String message = ”World";System.out.println(greeting.compareTo(message));

• การเปรยบเทยบคา String ตามลาดบตวอกษรใชเมทธอด

int compareTo(String s)

return คา 0 เมอเปนคาเดยวกน

return คาจานวนเตมบวกเมอ String Object นมากกวา s

return คาจานวนเตมลบ เมอ String Object นนอยกวา s

String Concatenation

String greetings = new String("Hello”);int ThisYear = 2001;String name = " World " + 2001;String message = greetings + name;

• การนา String มาเชอมตอกน

ใชโอเปอรเรเตอร Concatenate (+)

แตไมสามารถเปรยบเทยบ String โดยใชโอเปอรเรเตอรแบบ

Relational (>, <, >=, <=) และโอเปอรเรเตอรแบบ Equality ( ==, !=)

ได

• หมายเหต

ใชโอเปอรเตอร (+) กบ String ไดเมอมพจนใดพจนหนงเปน String

• “HELLO WORLD” , “Hello World”

• “HELLO WORLD” >> “DLROW OLLEH”

• “HELLO WORLD” , “Hello World”

• “HELLO WORLD” >> “ OLLEH”

• “HELLO WORLD” …………….. ให นบ L มกตว

• “HELLO WORLD” …………….. ให ตาแหนง ทเจอ ตว O ตว

แรก ?

• “HELLO WORLD” …………….. ม คาวา OR ในขอความน

Summary

• รจก Java Array

• รจก Java String

Recommended