cs117 oop chapter 1 - dr. paween · 2019-04-02 · cs. 117 oop chapter 1. introduction to...

35
CS1 17 OOP Chapter 1 Introduction to Programming By Dr. Paween Khoenkaw Computer Science MJU 1

Upload: others

Post on 14-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

CS117 OOPChapter 1

Introduction to Programming

By Dr. Paween KhoenkawComputer Science MJU

1

Page 2: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

17th century computer•The term "computer", in use from the early 17th century , meant "one whocomputes": a person performing mathematical calculations

•Early "computers" at work, summer 1949. In the terminology of that period, computers were employees--typically female--who performed the arduous task of transcribing raw data to standard engineering units.

•Computer Room

Page 3: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

ตามพจนานกรมฉบบราชบณฑตยสถาน พ.ศ. 2525

หมายถง เครองอเลกทรอนกสแบบอตโนมตทาหนาทเปน

เสมอนสมองกล ใชสาหรบแกปญหาตาง ๆ ทงทงายและ

ซบซอนโดยวธทางคณตศาสตร

•“คอมพวเตอร” หรอในภาษาไทยวา “คณตกรณ”

Page 4: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

โปรแกรมคอมพวเตอรคออะไร ?

4

โปรแกรมหรอชดคาสงทเขยนขนเพอบงคบและควบคมฮารดแวรของระบบคอมพวเตอร ใหทางานตามทตองการ

ลาดบขนตอนการทางานทเขยนดวยคาสงของคอมพวเตอร คาสงเหลานเรยงกนเปนโปรแกรมคอมพวเตอร

Page 5: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

คอมพวเตอรในยกตตน

Presenter
Presentation Notes
Rotating napia’s bone
Page 6: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1
Presenter
Presentation Notes
designed to tabulate polynomial functions. The name derives from the method of divided differences, a way to interpolate or tabulate functions by using a small set of polynomial coefficients. Most mathematical functions commonly used by engineers, scientists and navigators, including logarithmic and trigonometric functions, can be approximated by polynomials, so a difference engine can compute many useful tables of numbers.  government was interested in economically produced tables, not the engine itself Babbage had moved on to an analytical engine. By developing something better
Page 7: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1
Presenter
Presentation Notes
Programmable machine
Page 8: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

เครอง Analytical Engine ป ค.ศ.1834 แบบเบจไดพยายามเสนอ

การสรางเครองจกรกลชนดใหมเรยกวา Analytical Engine เพอตองการใหคานวณไดกบงานแทบทกชนดและตองทางานตามคาสงได

(programmable) โดยอาศยแนวคดของแจคการดทเอาบตรเจาะรมาชวยควบคมลวดลายการทอผาใหไดตามแบบ ทตองการนนเอง แบบรางของเครอง

Analytical Engine ทเขานาเสนอนจะอาศยองคประกอบในการทางานแบงออกเปนสวน ๆ ดงน

•Input Device อาศยบตรเจาะรในการนาขอมลเขาสตวเครอง

•Arithmetic Processor เปนสวนททาหนาทคานวณเพอหาผลลพธ

•Control Unit สาหรบคอยควบคมและตรวจสอบงานทจะนาออกวาไดผลลพธทถกตองหรอไม

•Memory เปนสวนสาหรบเกบตวเลขเพอรอการประมวลผล

Presenter
Presentation Notes
designed to tabulate polynomial functions. The name derives from the method of divided differences, a way to interpolate or tabulate functions by using a small set of polynomial coefficients. Most mathematical functions commonly used by engineers, scientists and navigators, including logarithmic and trigonometric functions, can be approximated by polynomials, so a difference engine can compute many useful tables of numbers.  government was interested in economically produced tables, not the engine itself Babbage had moved on to an analytical engine. By developing something better
Page 9: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

9

ววฒนาการของเครองคอมพวเตอร (ตอ)

Lady Ada Augusta Lovelace (1815 – 1852) ได

ทางานรวมงานกบ Babbage และไดเขยนชดคาสง

ลงในบตรเจาะรเพอควบคมการทางานของเครอง

Analytical Engine ไดรบการยกยองวาเปน

โปรแกรมเมอรคนแรกของโลก

Ada Augusta Lovelace

Page 10: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

10

George Boole (1815 – 1864)

นกคณตศาสตรชาวองกฤษ George Boole ไดคดระบบ

พชคณตแบบใหม คอ Boolean Algebraซงใชอธบาย

หลกเหตผลทางตรรกวทยาโดยใชสภาวะเพยง 2 อยาง

คอ 0 และ 1รวมกบเครองหมายในเชงตรรกะพนฐาน

ไดแก not, and และ or

ถอเปนกาเนดของระบบเลขฐานสอง และ Boolean Algebra กไดถกดดแปลง

ใหเขากบวงจรไฟฟาซงม 2 สถานะคอ เปดและปด จงนบเปนรากฐานของ

การออกแบบวงจรในระบบคอมพวเตอรปจจบน

ววฒนาการของเครองคอมพวเตอร (ตอ)

Page 11: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

1936 Turing MachineA Turing machine is a hypothetical device that manipulates symbols on a strip of tape according to a table of rules.

•Alan Turing

•Bombe Machine•Finite automata

Presenter
Presentation Notes
HARDWARE PROGRAMMING 0, 1
Page 12: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1
Presenter
Presentation Notes
ENIAC WW2
Page 13: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

ENIAC PROGRAMMER

13

Page 14: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

14

John Von Neumann นกคณตศาสตรไดคนพบวธการเกบโปรแกรมไวในเครองคอมพวเตอรพรอมทงการแปลงชดคาสง(โปรแกรม)ใหเปนรหสตวเลขฐานสอง ซงสามารถปรบเขาไดกบระบบวงจรไฟฟา

ชอวา EDVAC(Electronic Discrete Variable Automatic Computer) ขนมาจนสาเรจในป ค.ศ. 1949 และเรมใชงานจรงในป ค.ศ. 1951

ววฒนาการของเครองคอมพวเตอร (ตอ)

EDVAC

Presenter
Presentation Notes
In the 1940s, Howard Aiken (1900-73), a Harvard University mathematician, created what is usually considered the first digital computer. This machine was constructed from mechanical adding machine parts. The instruction sequence to be used to solve a problem was fed into the machine on a roll of punched paper tape, rather than being stored in the computer. In 1945, however, a computer with program storage was built, based on the concepts of the Hungarian-American mathematician John von Neumann. The instructions were stored within a so-called memory, freeing the computer from the speed limitations of the paper tape reader during execution and permitting problems to be solved without rewiring the computer. The rapidly advancing field of electronics led to construction of the first all-electronic computer in 1946 at the University of Pennsylvania by the American engineer John Presper Eckert, Jr.. (1919- ) and the American physicist John William Mauchly (1907-80). (Another American physicist, John Vincent Atanasoff, later successfully claimed that certain basic techniques he had developed were used in this computer.) Called ENIAC, for Electronic Numerical Integrator And Computer, the device contained 18,000 vacuum tubes and had a speed of several hundred multiplications per minute. Its program was wired into the processor and had to be manually altered.
Page 15: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

15

EDVAC PROGRAMMER

Page 16: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

16

เทคโนโลยทาใหคอมพวเตอรมขนาดเลกลง

Page 17: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

17

ภาษาของเครองคอมพวเตอร

ภาษาทคอมพวเตอรเขาใจ คอเลขฐานสอง

“0” หมายถง ปด, “1” หมายถง เปด

เพอใหเขยนไดงาย เราจงนยมเขยนภาษาเครองเปนเลขฐาน 16

1111 0110 = F6

ตวอยางภาษาเครองของ หนวยประมวลผลเบอร 80386

b4 02 b2 49 cd 21 b2 03 cd 21 b2 55 cd 21 b4 4c cd 21

Presenter
Presentation Notes
BYTE CODE
Page 18: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

18

PROGRAMMER: MARGARET HAMILTON

APOLLO MISSION

Page 19: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

19

ภาษาแอสเซมบล

เพออานวยความสะดวกในการเขยนโปรแกรม

จงมการใชตวอกษรแทนคาสงทเปนเลขฐาน16 เรยกวา Mnemonic

ภาษานเรยกวาภาษาแอสเซมบล (Assembly) โปรแกรมทใชแปลภาษาน

เปนภาษาเครองเรยกวา แอสเซมเบลอร (Assembler)

ตวอยางภาษาแอสเซมบล หนวยประมวลผลเบอร 80386mov ah,02mov dl,49int 21mov dl,03int 21mov dl,55int 21mov ah,4cint 21

Assembler ภาษาเครอง

Presenter
Presentation Notes
ASSEMBLY
Page 20: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

20

ภาษาระดบสง

เปนภาษาทมนษยเขาใจ

ภาษาระดบสงมอยมากมาย

•บางภาษาใชสงงานการคานวณทางคณตศาสตรและวทยาศาสตร

•บางภาษาใชสงงานทางดานการจดการขอมล

Page 21: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

21

ภาษาระดบสงในชวงแรก

- มนษยเขาใจงาย

- มโครงสราง

- แบงการทางานเปนสวนยอย

Fortran, ALGOL, COBOL, BASIC, Pascal, C, Go

Page 22: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

22

ภาษาระดบสงในชวงแรก

Complieror

Interpreterภาษาเครอง

Page 23: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

23

กระบวนทศน: Programming paradigm

Machine code Procedural languages Object-oriented programmingmov ah,02mov dl,49int 21mov dl,03int 21mov dl,55int 21mov ah,4cint 21

?

Page 24: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

24

Procedural languages

A = 4B = 1C = 1L1: B = B * CC = C+1IF C > A THEN GOTO L2GOTO L1L2: PRINT B

DATA

LOGIC

Page 25: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

25

Procedural languages

A = 4B = 1C = 1L1: B = B * CC = C+1IF C > A THEN GOTO L2GOTO L1L2: PRINT B

DATA

LOGIC

mov ah, 02mov dl, 49int 21mov dl, 03int 21mov dl, 55int 21mov ah, 4cint 21

Page 26: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

26

Procedural languages' Attribute 1redvalue(1) = 40greenvalue(1) = 10bluevalue(1) = 0

' Attribute 2redvalue(2) = 50greenvalue(2) = 15bluevalue(2) = 0

' Attribute 3redvalue(3) = 50greenvalue(3) = 20bluevalue(3) = 0

' Attribute 4redvalue(4) = 50greenvalue(4) = 25bluevalue(4) = 0

' Attribute 5redvalue(5) = 60greenvalue(5) = 35bluevalue(5) = 0

' Attribute 6redvalue(6) = 60greenvalue(6) = 40bluevalue(6) = 0

' Attribute 7redvalue(7) = 63greenvalue(7) = 50bluevalue(7) = 0

' Attribute 8redvalue(8) = 30greenvalue(8) = 60bluevalue(8) = 0

' Attribute 9redvalue(9) = 0greenvalue(9) = 40bluevalue(9) = 10

' Attribute 10redvalue(10) = 0greenvalue(10) = 30bluevalue(10) = 15

END SUB ' SetColorValues

' current MandY value to screen' coordinates and store that in' C2.

x = C1 ' Shift the value of C1 to X so that it may be maintained.y = C2 ' Shift the value of C2 to Y so that it may be maintained.

FOR n = 1 TO count ' Begin the iterations of the Mandelbrot' equation.

x1 = x * x - y * y + C1 ' Calculate the X value and store it' in x1.

y1 = 2 * x * y + C2 ' Calculate the Y value and store it' in y1.

r = x1 * x1 + y1 * y1 ' Calculate the distance from the' origin.

IF r > 4 THEN GOTO skipman ' If (c1, c2) tends toward infinity' exit the loop.

x = x1 ' Take the new value for x1 and shift it to x.y = y1 ' Take the new value for y1 and shift it to y.intervalcounter = n ' Set intervalcounter to the current

' interval number for use with' GetColorInterval

NEXT n ' Reiterate the Mandelbrot equation.

PSET (MandX + mandoffsetx, MandY + mandoffsety), 0: IF litemode1 = 1 THEN PaintColorGaugeBlack

' If the point remains within the 4 x 4' boundaries then color the point black' and color the gauge black.

skipman:

IF r > 4 THEN PSET (MandX + mandoffsetx, MandY + mandoffsety), GetColorInterval: IF litemode1 = 1 THEN PaintColorGauge

' If the point tends to infinity color it' based on the number of iterations of the' Mandelbrot equation.

q$ = UCASE$(INKEY$)IF q$ = CHR$(27) THEN EndProgram ' End program when the user presses

' Escape.IF q$ = " " THEN SWAP litemode1, litemode2: PaintLitemodeIndicator

' Turn litemode on or off, then indicate.

IF litemode1 = 1 THEN IF scantype = 1 THEN EraseScanLineIF litemode1 = 1 THEN IF scantype = 2 THEN PaintScanDot

NEXT MandY ' Loop to the next MandY value.NEXT MandX ' Loop to the next MandX value.

END SUB ' PaintMandelbrotSet

END SUB ' PaintIndicatorLights

SUB PaintLitemodeIndicator

IF litemode1 = 0 THEN LINE (6, 304)-(24, 317), 14, B: LIN

IF litemode1 = 1 THEN LINE (38, 304)-(63, 317), 14, B: LIN

END SUB ' PaintLitemodeIndicator

SUB PaintMainGraphic

'Picture Plus'by Chris Howie'1998,1999 Crazy Computers

SHELL "lppdgraf.exe" ' This is the program that loads the

END SUB ' PaintMainGraphic

SUB PaintMainMenu

SCREEN 12

PaintMainGraphic

LINE (9, 9)-(629, 169), , B

LOCATE 15PRINT " Create Image Load Set Edit Set

LINE (10, 220)-(630, 240), , B ' Line around menu choices

ch1x% = 67ch2x% = 255ch3x% = 435ch4x% = 610

CIRCLE (ch1x%, 280), 25 ' Choice indicator "lights".CIRCLE (ch1x%, 280), 24, 8CIRCLE (ch1x%, 280), 23CIRCLE (ch1x%, 280), 18, , 1.5, 3.7

CIRCLE (ch2x%, 280), 25CIRCLE (ch2x%, 280), 24, 8CIRCLE (ch2x%, 280), 23CIRCLE (ch2x%, 280), 18, , 1.5, 3.7

CIRCLE (ch3x%, 280), 25CIRCLE (ch3x%, 280), 24, 8CIRCLE ( h3 % 280) 23

DATA และ LOGIC จะปะปนกนอยางไมเปนระเบยบ

Page 27: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

27

กระบวนทศน: Programming paradigm

Machine code Procedural languages Object-oriented programmingmov ah,02mov dl,49int 21mov dl,03int 21mov dl,55int 21mov ah,4cint 21

class Point {

public int x = 0;

public int y = 0;}

class Test {

public static void main(String[] args) {

Point p = new Point();

System.out.println( " Value of x = " + p.x + “ Value of y = " + p.y);

}

}

Page 28: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

28

Object-oriented programming

DATA……………………………………………………………….

LOGIC……………………………………………………………….

OBJECT

OBJECT

OBJECT

OBJECT

OBJECT

OBJECT

Computer Program

Page 29: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

29

แงมม Procedural languages Object-oriented

programming

โปรแกรมขนาดเลก เขยนสน เขยนยาว

โปรแกรมขนาดใหญ เขยนยาว เขยนสน

การนา code กลบมาใชใหม นา code ทงหมดมาใส เรยกใชไดเลย

การนา code เกามาปรบใช ตองแก code เกา นามาใชไดเลย

อายของภาษา สวนมากเกากวา 20 ป สวนมากไมถง 20 ป

ความเรวของการทางาน เรว ชากวาเลกนอย

Page 30: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

30

Object-oriented programming

DATA……………………………………………………………….

LOGIC……………………………………………………………….

OBJECT

โปรแกรมแนว

Proceduralแยกสวน

จดรปแบบ

Page 31: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

31

Object-oriented programming

DATA……………………………………………………………….

LOGIC……………………………………………………………….

OBJECT

ในชวตจรง

เราจะแบงอยางไรวา อะไรคอ DATA และอะไรคอ LOGIC ?

Page 32: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

32

Attributes

Behavior

Things

ลกษณะจาเพาะ เชน ส กลน ความเรว อณหภม

พฤตกรรม เชน หลบ สงเสยง วง เลยว

Page 33: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

33

2

Attributes and Behavior

Car

Cat

Attributes Behavior

License plateMakeModelYearColor

StartStopTurnAccelerateFlash lights

NameBreedColorAgeWeight

SleepEatPurrClimbScratch

Page 34: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

34

34

Attributes and Behavior

Car

Cat

Attributes Behavior

License plateMakeModelYearColor

StartStopTurnAccelerateFlash lights

NameBreedColorAgeWeight

SleepEatPurrClimbScratch

Properties MethodsDATA LOGIC

JAVA

Page 35: CS117 OOP Chapter 1 - Dr. Paween · 2019-04-02 · CS. 117 OOP Chapter 1. Introduction to Programming. By Dr.Paween Khoenkaw. Computer Science MJU. 1

การบานท 1

35

แยก Attribute และ Behavior ของ “Smartphone”

มาอยางละ 5 ขอ

SmartPhone

Attributes Behavior

1….2….3….4….5….

1….2….3….4….5….

เขยนคาตอบใสกระดาษ A4 ตดครงสงทางกลองรบการบาน หนาแลป 2เขยนชอ รหส กลมเรยน ทมมบนขวา

ภายในวนศกรท 23 มถนายน 2560