unit1 introduction

16
B-Com : NCU 19/06/56 51-302 Computer Programming 2 1 computer programming 2 computer programming 2 51-302 Unit1-Introduction หลายปีก่อน เชือกันว่า ภาษาสําหรับโปรแกรมเป็นวิชาทีตายแล้ว นั นอาจเป็นเพราะว่าไม่มีอะไรทีภาษา C ทําไม่ได้ และคงต้องอีก หลายปี ทีคนส่วนใหญ่จะเข้าใจคุณค่าอันแท้จริงของภาษา C++ (ทีมา java programming,ดร. วีรศักดิ ซึงถาวร)

Upload: naret-su

Post on 02-Nov-2014

515 views

Category:

Education


1 download

DESCRIPTION

Programming2 , JAVA

TRANSCRIPT

Page 1: Unit1 introduction

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,ดร. วรศกด� ซงถาวร)

Page 2: Unit1 introduction

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) จาวาสามารถทางานไดทกระบบปฎบตการ

Page 3: Unit1 introduction

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

Page 4: Unit1 introduction

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

Page 5: Unit1 introduction

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

Page 6: Unit1 introduction

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 อยแลว

Page 7: Unit1 introduction

B-Com : NCU 19/06/56

51-302 Computer Programming 2 7

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

• jdk 1.5

http://java.sun.com

Page 8: Unit1 introduction

B-Com : NCU 19/06/56

51-302 Computer Programming 2 8

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

• set classpath ใหเปน .;

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

click

Page 9: Unit1 introduction

B-Com : NCU 19/06/56

51-302 Computer Programming 2 9

click

Page 10: Unit1 introduction

B-Com : NCU 19/06/56

51-302 Computer Programming 2 10

หรอ

คนหา Variable Path แลว Click Edit

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

Page 11: Unit1 introduction

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

Page 12: Unit1 introduction

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 บรรทดท�สอง

Page 13: Unit1 introduction

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>}

Page 14: Unit1 introduction

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

Page 15: Unit1 introduction

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

Page 16: Unit1 introduction

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