29
http://www.thaiall.com/class Page: 1 กกกกกกกกกกกกกกกกกกกกกกกกกกก JAVA ก.กกกกกกกก กกกกกกกกกกก . ปปปปปปปป 18 Introduction กกกกกกกกกกกกกกก กกกกกกกกกกกกก กกกกกกกกกกกกกกกกกกก

Java intro

Embed Size (px)

DESCRIPTION

นี่คือการทดสอบใช้ slide share จัดทำเพื่อการเรียนการสอน

Citation preview

Page 1: Java intro

http://www.thaiall.com/class Page: 1

การโปรแกรมเชิงวั ตถุ�ด้�วัยภาษา JAVA

อ. บุ�รนทร� ร�จจนพั นธุ์�� . ปรั�บปรั�ง 18

ตุ�ลาคม 2551

Introduction

มหาวัทยาลั ยโยนก จ งหวั ด้ลั!าปาง

ศู#นย�กลัางควัามร# �แลัะภ#มป&ญญาแผ่)นด้น

http://www.yonok.ac.th

Page 2: Java intro

http://www.thaiall.com/class Page: 2

ควัามหมายของภาษาจาวัา ภาษาจาวา (Java Language )ค+อ ภาษาโปรแกรมท,-ถุ#กพั ฒนาโด้ยบุรษ ท ซั นไมโครซัสเต2มส� เป3นภาษาส!าหร บุเข,ยนโปรแกรมท,-สน บุสน�นการเข,ยนโปรแกรมเชิงวั ตถุ� (OOP =Object-Oriented Programming ) โปรแกรมท,-เข,ยนข45นถุ�าเป3นคลัาสก2จะเป3นต�นแบุบุของวั ตถุ�สามารถุม,เมทธุ์อด้ (Method ) ท,-ประกอบุด้�วัยพัฤตกรรม (Behavior )ร#ปพัรรณ (Identity ) แลัะสถุานะ (State)

Page 3: Java intro

http://www.thaiall.com/class Page: 3

อะไรต)อมอะไรในจาวัา

ข�อม#ลัจาก http://www.thaiall.com/class/indexr.htm

Page 4: Java intro

http://www.thaiall.com/class Page: 4

ต วัแปลัภาษาจาวัา บรั�ษ�ท Sun Microsystems, Inc.

- J2SE (Java 2 Platform, Standard

Edition :Core/Desktop)

- J2EE (Java 2 Platform, Enterprise

Edition :Enterprise/Server)

- J2ME (Java 2 Platform, Micro

Edition :Mobile/Wireless)

- JRE (Java Runtime Environment)

- JVM (Java Virtual Machine)

http://java.sun.com

Page 5: Java intro

http://www.thaiall.com/class Page: 5

Language ภาษาคอมพัวัเตอร� (Computer Language) ค+อ ภาษาท,-ใชิ� หร+อเก,-ยวัข�องก บุ

คอมพัวัเตอร� ซั4-งม กใชิ�ร)วัมก บุภาษาโปรแกรมแต)ภาษาคอมพัวัเตอร�น 5นม,ควัามหมายท,-กวั�าง

กวั)า โด้ยไม)จ!าเป3นท,-จะต�องเป3นภาษาโปรแกรม ซั4-งเป3นท,-ร# �ก นวั)าภาษาอย)าง HTML หร+อ SQL

ไม)ใชิ)ภาษาโปรแกรม แต)ถุ+อวั)าเป3นภาษาคอมพัวัเตอร� ภาษาโปรแกรม (Program Language) ค+อ วัธุ์,การมาตรฐานในการ

ส+-อสารส!าหร บุแสด้งค!าส -งไปย งคอมพัวัเตอร�ภาษาโปรแกรมก!าหนด้ไวัยากรณ�แลัะการต,ควัามหมายจากโปรแกรมคอมพัวัเตอร�ท,-

เข,ยนข45น ภาษาโปรแกรมท!าให�โปรแกรมเมอร�สามารถุระบุ�อย)างชิ ด้เจนถุ4งข�อม#ลัท,-

คอมพัวัเตอร�จะท!างาน แลัะวัธุ์,การท,- คอมพัวัเตอร�จะประมวัลัผ่ลัข�อม#ลัเหลั)าน 5น

Page 6: Java intro

http://www.thaiall.com/class Page: 6

เรั��มตุ�นเขี�ยนโปรัแกรัมท�าอย�างไรั• เลั+อกภาษา ส!าหร บุน กเร,ยน น กศู4กษาน 5นง)ายท,-จะเลั+อก เพัราะ

อาจารย�คอยชิ,5แนะ• หาแหลั)งข�อม#ลัอ�างองจากห�องสม�ด้ ถุ�าท)านไม)ม,อะไรอย#)ในม+อ คงน4กโครงสร�างภาษาไม)ออก• หาต วัแปลัภาษา ท�กภาษาต�องม,ต วัแปลัภาษา ม,หลัายภาษาท,-ถุ#ก

สร�างเป3น Free compiler • เข,ยนโปรแกรมต วัแรกท,-ง)าย เชิ)น พัมพั�เลัข 5 เป3นต�น• ศู4กษาการท!าซั!5า แลัะการเลั+อกตามเง+-อนไข เชิ)นพัมพั�ส#ตรค#ณ หร+อ

พัมพั� 1 ถุ4ง 10 เป3นต�น• ตด้ต)อแฟ้;มข�อม#ลั เพั+-ออ)านมาแสด้งผ่ลั หร+อปร บุปร�งข�อม#ลัได้�• เข,ยนเมน# เพั+-อเลั+อกกระท!าโปรแกรมตามต วัเลั+อก• ท!ารายงานจากการเชิ+-อมแฟ้;มหลัายแฟ้;ม โด้ยก!าหนด้ได้�หลัายต วั

เลั+อก• เข,ยนโปรแกรมเพั-มข�อม#ลั เชิ)น ซั+5อ ขาย ย+ม ค+น หร+อโปรแกรมลัง

ทะเบุ,ยนน กศู4กษา• สร�างโปรแกรมข45นมาระบุบุหน4-งให�สมบุ#รณ� ( สมบุ#รณ�ค+อ การสนองควัามต�องการของผ่#�ใชิ�)

Page 7: Java intro

http://www.thaiall.com/class Page: 7

การเข,ยนโปรแกรมโครงสร�าง 1. การัท�างานแบบตุามล�าดั�บ(Sequence) ร#ปแบุบุการเข,ยนโปรแกรมท,-ง)าย

ท,-ส�ด้ค+อ เข,ยนให�ท!างานจากบุนลังลั)าง เข,ยนค!าส -งเป3นบุรรท ด้ แลัะท!าท,ลัะ

บุรรท ด้จากบุรรท ด้บุนส�ด้ลังไปจนถุ4ง บุรรท ด้ลั)างส�ด้ สมมตให�ม,การท!างาน 3 กระบุวันการค+อ อ)านข�อม#ลั ค!านวัณ

แลัะพัมพั� จะเข,ยนเป3นผ่ งงาน(Flowchart) ในแบุบุตามลั!าด้ บุได้�ตามภาพั

Page 8: Java intro

http://www.thaiall.com/class Page: 8

การเข,ยนโปรแกรมโครงสร�าง 2. การัเล"อกกรัะท�าตุามเง"�อนไขี(Decision) การต ด้สนใจ หร+อเลั+อกเง+-อนไขค+อ

เข,ยนโปรแกรมเพั+-อน!าค)าไปเลั+อกกระท!า โด้ยปกตจะม,เหต�การณ�ให�ท!า 2

กระบุวันการ ค+อเง+-อนไขเป3นจรงจะกระท!า กระบุวันการหน4-ง แลัะเป3นเท2จจะกระท!าอ,ก กระบุวันการหน4-ง แต)ถุ�าซั บุซั�อนมากข45น จะ

ต�องใชิ�เง+-อนไขหลัายชิ 5น เชิ)นการต ด้เกรด้ น กศู4กษา เป3นต�น ต วัอย)างผ่ งงานน,5 จะ

แสด้งผ่ลัการเลั+อกอย)างง)าย เพั+-อกระท!ากระบุวันการเพั,ยงกระบุวันการเด้,ยวั

Page 9: Java intro

http://www.thaiall.com/class Page: 9

การเข,ยนโปรแกรมโครงสร�าง 3. การัท�าซ้ำ�%า (Repeation or Loop)

การท!ากระบุวันการหน4-งหลัายคร 5ง โด้ยม, เง+-อนไขในการควับุค�ม หมายถุ4งการท!าซั!5าเป3น

หลั กการท,-ท!าควัามเข�าใจได้�ยากกวั)า 2 ร#ป แบุบุแรก เพัราะการเข,ยนโปรแกรมแต)ลัะภาษา

จะไม)แสด้งภาพัอย)างชิ ด้เจนเหม+อนการเข,ยนผ่ งงาน (Flowchart) ผ่#�เข,ยนโปรแกรมต�อง

จนตนาการ ถุ4งร#ปแบุบุการท!างาน แลัะใชิ�ค!าส -ง ควับุค�มด้�วัยตนเอง ต วัอย)างผ่ งงานท,-น!ามา

แสด้งน,5เป3นการแสด้งค!าส -งท!าซั!5า (do while) ซั4-งหมายถุ4งการท!าซั!5าในขณะท,-เป3นจรง แลัะเลัก

การท!าซั!5าเม+-อเง+-อนไขเป3นเท2จ

Page 10: Java intro

http://www.thaiall.com/class Page: 10

แนวค�ดัขีองการัโปรัแกรัมเชิ�งว�ตุถุ�(OOP Concepts)

1. การัปกป(อง (Encapsulation) - การัรัวมกล��มขีองขี�อม*ล และกล��มขีองโปรัแกรัม เพื่"�อการั

ปกป(อง และเล"อกตุอบสนอง

2 . การัส"บทอดั (Inheritance) - ยอมให้�น�าไปใชิ� ห้รั"อเขี�ยนขี/%นมาทดัแทนขีองเดั�ม

3 . การัพื่�องรั*ป (Polymorphism)

มาจากภาษากรั�ก ห้มายถุ/ง Many Shapes

(Overriding) - Overloading ม�ชิ"�อโปรัแกรัมเดั�ยวก�น แตุ�รัายการัตุ�วแปรั

(Parameter List) ตุ�างก�น- Overriding ม�ชิ"�อโปรัแกรัม และตุ�วแปรัเห้ม"อนก�น เพื่"�อ

เขี�ยน behavior ขี/%นมาให้ม�

Page 11: Java intro

http://www.thaiall.com/class Page: 11

Java 2 Platform, Standard Edition (J2SE)http://java.sun.com/j2se/1.4/

download.html ม,ต วัแปรท,-หลัายบุรษ ทสร�างข45น แต)ต วัท,-

เป3นมาตรฐานค+อของ sun แลัะ java ก!าลั ง อย#)ในระหวั)างการพั ฒนาอย)างต)อเน+-อง ผ่มขอ

เลั+อก 2J SE ของ sun เพัราะใชิ�งานใน text mode น!าไปใชิ�ใน linux ก2ม,หลั กการเหม+อน

ก น แต)ถุ�าเป3นของบุรษ ทอ+-น ส)วันใหญ)ต�องใชิ�ใน Windows ไม)อย)างย4ด้ตด้ก บุ O O จ4งเลั+อกท,-

จะใชิ� 2J SE สอนน กศู4กษา แต)บุรษ ทต)าง ๆ พั ฒนาต วัแปลัเป3นระบุบุ

IDE (Integrated development environment) ซั4-งใชิ�ง)ายกวั)า ส)วัน 2J SE

แม�ไม)ม, IDE ในต วั แต)ม,โปรแกรมหลัายต วัท,-น!า มาใชิ�เป3น editor แลั�วัเร,ยก 2J SE มาแปลั

แลัะใชิ�งานได้�สะด้วักข45น เชิ)น kawa เป3นต�น

Page 12: Java intro

http://www.thaiall.com/class Page: 12

ร#ปแบุบุของ Classclass declaration { variable constructor method nested class or inner class}class a { int a = 0; a() { } void b () { } class c { }}

Page 13: Java intro

http://www.thaiall.com/class Page: 13

ชิ+-อเร,ยกเก,-ยวัก บุ ObjectObject Model

State Behavior Identity

General, UML

Value of Attribute Behavior/Action Attribute

OOP

Value of Variable Method Property

Coding in Java

Value of Attribute Method Variable/Attr.

Page 14: Java intro

http://www.thaiall.com/class Page: 14

เข,ยนโปรแกรมแรกstart, run, cmdcd\dircd c:\j2sdk1.4.2_03\binc:\j2sdk1.4.2_03\bin>edit x.java

class x { public static void main(String args[]){ System.out.println("hello"); }}

Page 15: Java intro

http://www.thaiall.com/class Page: 15

แปลัโปรแกรม แลัะประมวัลัผ่ลัc:\j2sdk1.4.2_03\bin>javac x.javac:\j2sdk1.4.2_03\bin>java xhello

c:\j2sdk1.4.2_03\bin>

Page 16: Java intro

http://www.thaiall.com/class Page: 16

ค!าท,-ข,ด้เส�นใต� เปลั,-ยนได้�ตามต�องการclass xxx {

public static void main(String args[]){ System.out.println("hello"); }}class aaa { void bbb(String[] ccc){ System.out.println("ddd"); System.out.println(eee("ddd")); } int eee(String fff){ return ("xxx"); }}

Page 17: Java intro

http://www.thaiall.com/class Page: 17

Java Applet//<applet code=x.class width=200 height=200></applet>

//DOS>appletviewer x.java

import java.awt.Graphics;

public class x extends

java.applet.applet {

public void paint(Graphics g) {

g.drawString("test",10,20);

}

}

Page 18: Java intro

http://www.thaiall.com/class Page: 18

Java Application// javac y.java// java yimport java.lang.*;import java.awt.*;import java.awt.event.*;public class y implements ActionListener{Frame fs = new Frame("header");Button bexit = new Button("exit");public static void main(String args[]) { y s = new y(); s.init();}public void init() { fs.setSize(200,100); fs.setLayout(null); bexit.setBounds(10,50,50,20); fs.add(bexit); bexit.addActionListener(this); fs.show();}public void actionPerformed(ActionEvent a) { System.exit(0);}}

Page 19: Java intro

http://www.thaiall.com/class Page: 19

Certified Topics Product ID - - 310035CX

Price 20000$ .

The Sun Certified Programmer for Java 2

14Platform . certificationOOOO OO OOO OOOOOOOOOOO

exper i enced i n usi ng t he basic syntax and st r uct ur e

of the Java programming lan guage. Certification is

2available for the Java OOOOOOOOO http://www.sun.com/training/catalog/

- -courses/CX 3 1 0 0 3 5 .xml

Page 20: Java intro

http://www.thaiall.com/class Page: 20

Certified TopicsTopics

1. Declaration .. 2 . Flow Control 3 . Garbage Collection 4 . Language

Fundamentals 5 . Operators

& Assi gnment s 6 . Overloading,

OOOOOOOOOO 7 . Threads 8 . . ...java lang 9 . Collections Framework

Page 21: Java intro

http://www.thaiall.com/class Page: 21

Certified TopicsSection 1: Declarations and Access Control •Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms both for declaration and for initialization. •Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as public, final, static, abstract, etc.). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers. •For a given class, determine if a default constructor will be created and if so state the prototype of that constructor. •Identify legal return types for any method given the declarations of all related methods in this or parent classes.

Page 22: Java intro

http://www.thaiall.com/class Page: 22

Certified TopicsSection 2: Flow control, Assertions, and Exception Handling •Write code using if and switch statements and identify legal argument types for these statements. •Write code using all forms of loops including labeled and unlabeled, use of break and continue, and state the values taken by loop counter variables during and after loop execution. •Write code that makes proper use of exceptions and exception handling clauses (try, catch, finally) and declares methods and overriding methods that throw exceptions. •Recognize the effect of an exception arising at a specified point in a code fragment. Note: The exception may be a runtime exception, a checked exception, or an error (the code may include try, catch, or finally clauses in any legitimate combination). •Write code that makes proper use of assertions, and distinguish appropriate from inappropriate uses of assertions. •Identify correct statements about the assertion mechanism.

Page 23: Java intro

http://www.thaiall.com/class Page: 23

Certified TopicsSection 3: Garbage Collection •State the behavior that is guaranteed by the garbage collection system. •Write code that explicitly makes objects eligible for garbage collection. •Recognize the point in a piece of source code at which an object becomes eligible for garbage collection.

Page 24: Java intro

http://www.thaiall.com/class Page: 24

Certified TopicsSection 4: Language Fundamentals •Identify correctly constructed package declarations, import statements, class declarations (of all forms including inner classes) interface declarations, method declarations (including the main method that is used to start execution of a class), variable declarations, and identifiers. •Identify classes that correctly implement an interface where that interface is either java.lang.Runnable or a fully specified interface in the question. •State the correspondence between index values in the argument array passed to a main method and command line arguments. •Identify all Java programming language keywords. Note: There will not be any questions regarding esoteric distinctions between keywords and manifest constants. •State the effect of using a variable or array element of any kind when no explicit assignment has been made to it. •State the range of all primitive formats, data types and declare literal values for String and all primitive types using all permitted formats bases and representations.

Page 25: Java intro

http://www.thaiall.com/class Page: 25

Certified TopicsSection 5: Operators and Assignments •Determine the result of applying any operator (including assignment operators and instance of) to operands of any type class scope or accessibility or any combination of these. •Determine the result of applying the boolean equals (Object) method to objects of any combination of the classes java.lang.String, java.lang.Boolean and java.lang.Object. •In an expression involving the operators &, |, &&, || and variables of known values state which operands are evaluated and the value of the expression. •Determine the effect upon objects and primitive values of passing variables into methods and performing assignments or other modifying operations in that method.

Page 26: Java intro

http://www.thaiall.com/class Page: 26

Certified TopicsSection 6: Overloading, Overriding, Runtime Type and Object Orientation •State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and "has a". •Write code to invoke overridden or overloaded methods and parental or overloaded constructors; and describe the effect of invoking these methods. •Write code to construct instances of any concrete class including normal top level classes and nested classes.

Page 27: Java intro

http://www.thaiall.com/class Page: 27

Certified TopicsSection 7: Threads •Write code to define, instantiate and start new threads using both java.lang.Thread and java.lang.Runnable. •Recognize conditions that might prevent a thread from executing. •Write code using synchronized wait, notify and notifyAll to protect against concurrent access problems and to communicate between threads. •Define the interaction among threads and object locks when executing synchronized wait, notify or notifyAll.

Page 28: Java intro

http://www.thaiall.com/class Page: 28

Certified TopicsSection 8: Fundamental Classes in the java.lang Package •Write code using the following methods of the java.lang.Math class: abs, ceil, floor, max, min, random, round, sin, cos, tan, sqrt. •Describe the significance of the immutability of String objects. •Describe the significance of wrapper classes, including making appropriate selections in the wrapper classes to suit specified behavior requirements, stating the result of executing a fragment of code that includes an instance of one of the wrapper classes, and writing code using the following methods of the wrapper classes (e.g., Integer, Double, etc.): doubleValue o floatValue o intValue o longValue o parseXxx o getXxx o toString o toHexString

Page 29: Java intro

http://www.thaiall.com/class Page: 29

Certified TopicsSection 9: The Collections Framework •Make appropriate selection of collection classes/interfaces to suit specified behavior requirements. •Distinguish between correct and incorrect implementations of hashcode methods.