unit1 introduction

Post on 02-Nov-2014

515 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Programming2 , JAVA

TRANSCRIPT

B-Com : NCU 19/06/56

51-302 Computer Programming 2 1

computer programming 2computer programming 2

51-302

Unit1-Introductionหลายปกอน เช�อกนวา ภาษาสาหรบโปรแกรมเปนวชาท�ตายแลว น�นอาจเปนเพราะวาไมมอะไรท�ภาษา C ทาไมได และคงตองอกหลายป ท�คนสวนใหญจะเขาใจคณคาอนแทจรงของภาษา C++

(ท�มา java programming,ดร. วรศกด� ซงถาวร)

B-Com : NCU 19/06/56

51-302 Computer Programming 2 2

Introduction• ชวงป 1990s ตลาดเคร�องใชไฟฟามมลคาสง ซ�งอปกรณเหลาน1ตองการ

คอมพวเตอรขนาดเลกมาควบคม• Sun Microsystems จดต1งทม Green Group สราง

เคร�องตนแบบเรยกวา Star7 โดยใช c++• c++ ปญหาในการพฒนา อกท1งมหนวยประมวลผลหลายรนซ�งใช

โปรแกรมรวมกนไมได• Oak จงถกพฒนาข1นมา• ป 1995 มการเปล�ยนช�อเปน JAVA เพราะ Oak มคนใชแลว

คณสมบตของจาวา• เปนภาษางาย (simple) จาวานารปแบบจาก c และ c++ มาพฒนาตอ

นอกจากน1ยงเปนภาษาเชงวตถ• ความคงทน (Robust) ถกสรางข1นโดยพยายามลดความผดพลาดให

นอยท�สด โดยเปนภาษาท�ใชกลไก execption handling เพ�อจดการกบขอผดพลาด

• ความปลอดภย (security) มการทางานท�ปองกนการทาอนตรายใหกบระบบงานท�ใชงาน

• ความเปนอสระ (independent) จาวาสามารถทางานไดทกระบบปฎบตการ

B-Com : NCU 19/06/56

51-302 Computer Programming 2 3

java

java รนลาสดคอ java 2 ไดถกพฒนามาออกเปน 3 รนเพ�อความเหมาะสม ไดแก

• J2SE (Java 2 Platform, Standard Edition) ใชสาหรบการเขยนโปรแกรมบนคอมพวเตอรท�วๆไป

• J2EE (Java 2 Platform, Enterprise Edition) ใชสาหรบการเขยนโปรแกรมบนระบบงานใหญๆ โดยเพ�มศกยภาพของ J2SE ใหสามารถรองรบการทางานแบบ server side

• J2ME (Java 2 Platform, Micro Edition) ใชสาหรบเขยนโปรแกรมบนอปกรณขนาดเลกท�มทรพยากรจากด เชน โทรศพทมอถอ

ตวแปลภาษาประเภทของตวแปลภาษาแบงไดเปน

1. Interpreter

2. Compiler

VB 6 translator11011010

B-Com : NCU 19/06/56

51-302 Computer Programming 2 4

Interpreter• จะแปลคาส�งทละบรรทดแลวนาไปประมวลผลทนท

• object code ท�ไดในแตละบรรทดจะไมถกจดเกบ ทาใหเม�อเรยกการใชงานอกคร1 งจะตองทาการแปลคาส�งใหม

Basic Interpreter11011010

Compiler• จะแปลโปรแกรมท1งหมดเปน object code จากน1นทาการจดเกบ

object code ลงเปนแฟมขอมล

• จากน1นจงจะนา object code ท�จดเกบสงไปทางาน

• ขอดคอไมตองการแปลโปรแกรมทกคร1 งท�มการเรยกใชงาน

C,C++compiler

11011010

storage

B-Com : NCU 19/06/56

51-302 Computer Programming 2 5

java application

textpad javac.exe java.exe

x.javax.class

compilation Interpretation

java applet

textpad javac.exe

web browser

x.javax.class

x.html

B-Com : NCU 19/06/56

51-302 Computer Programming 2 6

ส�งท�ใชในการเขยนจาวา• Text Editor เชน notepad,editplus เปนตน

• JDK (java developer kit) ปจจบนอยท� version 1.5 (http://java.sun.com)

• JVM (java virtual machine) ปกต JVM จะตดต1งพรอมกน JDK อยแลว

B-Com : NCU 19/06/56

51-302 Computer Programming 2 7

tools ท�ใชในการเรยน• TextPad

• jdk 1.5

http://java.sun.com

B-Com : NCU 19/06/56

51-302 Computer Programming 2 8

การ config• set path ใหเปน java_directory\bin

• set classpath ใหเปน .;

• คลก mouse ขวา แลวเลอก propotries

click

B-Com : NCU 19/06/56

51-302 Computer Programming 2 9

click

B-Com : NCU 19/06/56

51-302 Computer Programming 2 10

หรอ

คนหา Variable Path แลว Click Edit

พมพ ;C:\j2sdk1.4.2_02\bin ตอทายจากสวนท�มอย

B-Com : NCU 19/06/56

51-302 Computer Programming 2 11

ทดสอบหลงการตดต1ง• ไปท� dos promt แลวพมพคาส�ง java -version

OK

first program

/*This is a first program.

*/class Lab1 // ช�อ class จะเปนช�อเดยวกนไฟล{

public static void main(String[] args) {

System.out.println("Hello World!");}

}

บนทกใหช�อไฟลเปน d:\j2sdk1.4.2_02\code\Lab1.java

B-Com : NCU 19/06/56

51-302 Computer Programming 2 12

comment

/*This is a first program.

*/class Lab1 {

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

System.out.println("Hello World!");}

}

comment

comment• /*

comment มากกวาหน�งบรรทด..........................................................................................................................................................................................*/

• // comment ทละบรรทด// comment บรรทดท�สอง

B-Com : NCU 19/06/56

51-302 Computer Programming 2 13

class

/*This is a first program.

*/class Lab1 {

public static void main(String[] args) {

System.out.println("Hello World!");}

}

class

class• ในไฟลหน�งไฟลจะตองม class อยางนอย 1 class

• รายละเอยดใน class จะประกอบดวย data member และ method

รปแบบclass ช�อ class{

<class member declaration>}

B-Com : NCU 19/06/56

51-302 Computer Programming 2 14

method

/*This is a first program.

*/class Lab1 {

public static void main(String[] args) {

System.out.println("Hello World!");}

}

method

method

public static void main(String[] args) {

System.out.println("Hello World!");}

modifier modifier return type method name parameter

method body

B-Com : NCU 19/06/56

51-302 Computer Programming 2 15

program template

class

{

}

method and attribute

class name

import statement

comment

ทดลองสรางโปรแกรมresume.java

ประกอบดวย

code

name

major

address

B-Com : NCU 19/06/56

51-302 Computer Programming 2 16

Homework เขยนโปรแกรม คร1 งท� 1Homework เขยนโปรแกรม คร1 งท� 11.computer.javaอธบาย space คอมพวเตอร

2.comp_applet.java อธบาย space คอมพวเตอรshowcomp.html เรยกใชงาน comp_applet.class

top related