Java Programming - Ttime.in.th - java programming...ต วแปร ภำษำจำวำตอ...

Preview:

Citation preview

Java Programmingธระยทธ ทองเครอ

Java Version

3

ทมา: JRebel

Java ใชสรางอะไร

Android applicationsBack-end SystemWeb applicationsDesktop applicationsBig data manipulationCloud computingRobotics

4

เครองมอส าหรบพฒนา Java

5

BlueJ

การ Download Eclipse

6

1

2

3

ส ำหรบพฒนำ Java Enterprise Edition (JEE)

ส ำหรบพฒนำ Java Standard Edition (JSE)

โครงสราง Java Applicationค ำสงตำง ๆ จะตองเขยนภำยในคลำสเทำนน

ภำยในคลำสมไดทงตวแปร (หรอ attribute) และฟงกชน (หรอ method)

คลำสทรนเปน Application ไดจะตอง เปนคลำสชนด public มเมธอด main เปนสมำชก ตงชอไฟลชอเดยวกบคลำส

7

รปแบบคลาสทรนเปน Application ได

8

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

System.out.println("สวสด");

}

}

Welcome to Java!สวสด

ชอคลาส(ควรขนตนดวยตวพมพใหญ )

ส วนญวเมธอด

ปกกาขอบเขตคลาส

แตละค าสงจะจบดวย semi-colon

ตวอยางผลลพธ

ปกกาขอบเขตเมธอด

ส วนการท างาน

Comment

9

// This is a simple application

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

/* System.out.println("Welcome to C!");

System.out.println("Welcome to C++!");

*/

}

}

Welcome to Java!ตวอยางผลลพธ

PackagePackage เปรยบเสมอนโฟลเดอรในกำรจดหมวดหมไฟลโคดโปรแกรมภำษำจำวำแนะน ำใหเกบไฟลคลำสตำงๆไวใน package เสมอ

10

package foo.test;

import java.util.Scanner;import java.util.*;

public class InputTest {public static void main(String args[]){

Scanner scan = new Scanner(System.in);System.out.print("Please enter number: ");int num = scan.nextInt();System.out.print("You enter " + num);

}}

package ภายนอกทประกาศใหช กรณไม ระบชอคลาส ใหช * ได

package ของคลาสน ซงจะถกเกบใหนโฟลเดอร foo/test

ตวแปรภำษำจำวำตองระบชนดตวแปร และประกำศตวแปรกอนใชทกครงตวแปรพนฐำน (Primitive Types)

ชนดตรรกะ (logical) – boolean ชนดอกขระ (textual) – char ชนดตวเลขจ ำนวนเตม (integer) – byte, short, int, long ชนดตวเลขทศนยม (floating point) – float, double

ชอทกอยำงเปน case-sensitiveไมใช reserve word มำตงชอ

11

แนวปฏบตในการตงชอ (Naming conventions)

ใชรปแบบ Camel case ส ำหรบค ำทมตงแต 2 ค ำขนไป (Compound Words) ชอคลำส – ค ำนำม, ค ำแรกขนตนดวยตวพมพใหญ

Circle EuropeCar GraduateStudent

ชอ Attribute/ตวแปร – ค ำนำม, ค ำแรกขนตนดวยตวพมพเลก firstName yearOfBirth studentName

ชอ Method – เปนค ำกรยำ ค ำแรกขนตนดวยตวพมพเลกgetName() setName() showDetails()

ชอ Package – ตวพมพเลก อำจน ำชอโดเมนขององคกรมำเขยนกลบcom.pantip.controller com.pantip.model

12

Operator เครองหมำยทำงคณตศำสตร:

+ - * / % ++ --

เครองหมำยทำงตรรกะ == != <= >= > < && || !

13

ค าสงตรวจสอบเงอนไข

14

if (expression) {

statement1;

} else {

statement2;

}

if (condition 1) {statement 1;

} else if (condition 2) {statement 2;

} else if (condition n) {statement n;

} else {other condition;

}

กจกรรมจงเขยนโปรแกรมค ำนวณตวเลข 2 ตว โดยรบคำตวด ำเนนกำร (Operator) ได 5 ตว

ไดแก +, -, *, / หรอ %

15

Enter the first number : 9

Enter the second number: 2

Please select operator (+, -, *, /, %): /

9.0/2.0 = 4.5

ค าสงท าซ า (loop)

16

while( ){

}

for( ; ; ) {

}

ก ำหนดคำเรมตนใหตวแปรควบคม เงอนไขกำรเขำลป ปรบปรงคำในตวแปรควบคม

ชดค ำสงท ำซ ำ

ก ำหนดคำเรมตนใหตวแปรควบคม

เงอนไขกำรเขำลป

ชดค ำสงท ำซ ำปรบปรงคำในตวแปรควบคม

do {

} while( );

ก ำหนดคำเรมตนใหตวแปรควบคม

ชดค ำสงท ำซ ำปรบปรงคำในตวแปรควบคม

เงอนไขกำรเขำลป

ค าสงท าซ า (loop)

17

public class WhileLoopTest {

public static void main(String args[]) {

int i = 1;

while (i <= 5) {

System.out.println("I love you");

i++;

}

System.out.println("Jub Jub (>_<)");

}

}

public class ForLoopTest {

public static void main(String args[]) {

for (int i=1; i<=5; i++) {

System.out.println("I love you");

}

System.out.println("Jub Jub (>_<)");

}

}

กจกรรม เขยนโปรแกรมหำคำผลรวมของ

1/1 + 1/2 + 1/3 + 1/4 + ... + 1/100ซงมผลลพธเปน 5.19

ใชทงลป while และ for

18

อารเรยใชเครองหมำย [ ] เพอประกำศตวแปรใหเปนอำรเรย

int score[] = new int[5];

หรอ

int[] score = new int[5];

ประกำศพรอมก ำหนดคำเรมตนchar vowels[] = { 'A', 'E', 'I', 'O', 'U' };

19

ลป for/eachใชส ำหรบลปทตองกำรเขำถงทกคำในอำรเรย ชวยใหค ำสงสนลง

20

for( : ) {

}

ตวแปรเกบสมาชกในแตละรอบ

ชอตวแปรอารเรยทจะน ามาวนลป

ชดค ำสงท ำซ ำ

public class ForEachLoopTest {public static void main(String args[]) {

String[] words = {"This", "is", "a", "test"};for (String w : words) {

System.out.println(w);}

}}

AnnotationsAnnotation ใชส ำหรบเพมขอมลใหกบโคดโปรแกรม (แตไมใช comment)ตวแปลภำษำจะใช Annotation ตรวจสอบขอผดพลำดบำงครงใชในกำร generate code ตำงๆ เชน ใน Spring Boot ใชก ำหนดคำ auto

configuration รปแบบกำรเขยน Annotation จะใช @ น ำหนำ

21

class Student extends Person {private String studentId;

@Override

public void print() {…

}

}

การตรวจจบขอผดพลาด (Exception)ภำษำ Java มประโยค try-catch ส ำหรบจดกำรกบขอผดพลำด ซงมรปแบบดงน

try {<statements>;

} catch(Exception e) {<statements>;

} เมอตองกำรดกจบขอผดพลำดค ำสงบรรทดใดจะน ำค ำสงไวในสวน try { } เมอตองกำรใหท ำงำนใดๆ เมอเกดขอผดพลำดจะเขยนค ำสงไวในสวน catch { }

22

ตวอยาง

23

class Test {

public static void main(String[] args) {

try {

int x = 0;

System.out.println(1 / x);

} catch (Exception ex) {

System.out.println(ex);

}

}

}

ตรวจจบวำตวหำรจะมคำเปน 0 หรอไม

ถำมคำเปน 0 ใหแสดงขอควำมเตอน

ค าสงมาตรฐานคลำสทบรรจเมธอดพรอมใชงำนส ำหรบนกพฒนำสำมำรถอำงถงคลำส และเมธอดนนได โดยไมตองประกำศกอน เลอกใชค ำสงเหลำนจำก คมอค ำสงมำตรฐำนภำษำจำวำ (java api document)

24

คนใน google ดวย "java api document

Java API Document

25

รายชอคลาส

ค าอธบาย ตวอยางและรายการเมธอดพรอมใช

รายชอ package

ตวอยางเมธอดในคลาส Math

26

Math.round( )

27

import java.util.Scanner;

public class RoundTest {

public static void main(String args[]) {Scanner scan = new Scanner(System.in);

System.out.println("Enter number:");float value = scan.nextFloat();

long roundValue = Math.round(value);System.out.println(roundValue);

}

}

รบคาชนด float

เรยกค าสงปดคาทศนยม

Enter number: 5.56 6

ตวอยางหนาจอ

Math.random( )Math.random() สงคำชนด double ระหวำง 0 ถง 1 (แตไมรวมคำ 1)

28

public class RandomTest {

public static void main(String args[]) {

double ranFrom0to1 = Math.random();

double ranFrom2to12 = 2.0 + (Math.random() * 10);

System.out.println(ranFrom0to1);

System.out.println(ranFrom2to12);

}

} 0.2554302519901010511.63515229203088ตวอยางหนาจอ

Math.sqrt( )

29

import java.util.Scanner;

public class SqrtTest {

public static void main(String[] args) {Scanner scan = new Scanner(System.in);

System.out.println("Enter number:");float value = scan.nextFloat();

double squareRoot = Math.sqrt(value);System.out.println("คารากทสอง คอ " + squareRoot);

}

} ตวอยางหนาจอ

Enter number:2

คารากทสอง คอ 1.4142135623730951

กจกรรมจงเขยนโปรแกรมรบคำต ำแหนงของ จด 2 จด (x1, y1) และ (x2, y2) และ ค ำนวณหำ

ระยะหำงระหวำงจด 2 จด โดย ใชสตรระยะหำง distance = x2 − x1 2 + (y2 − y1)2

30

ตวอยางหนาจอ

x1 : -7

y1 : -4

x2 : 17

y2 : 6.5

Distance from (-7, -4) to (17, 6.5) is 26.196374

คลาสมาตรฐาน String

31

ตวอยางเมธอดในคลาส Stringequals( ) เปรยบเทยบสตรง length( ) ควำมยำวของสตรง charAt( ) ตวอกษรในต ำแหนงทก ำหนด

ตวอกษรตวแรกคอต ำแหนงท 0 ตวอกษรสดทำยคอ length() - 1

indexOf( ) ต ำแหนงของสำยอกขระในสตรงsubstring( ) สตรงทอยในชวงทก ำหนดsplit( ) ตดขอควำมในสตรงตำมอกขระทก ำหนด replace( ) แทนทขอควำมในสตรงตำมทก ำหนด

32

equals( )

33

public class CompareString {

public static void main(String[] args) {

String gender = "male";

if ( gender.equals("male") )

System.out.println("เพศชาย");

else if ( gender.equals("female") )

System.out.println("เพศหญง");

}

}

เพศชายตวอยางผลลพธ

chatAt( )

34

public class StringTest {

public static void main(String args[]) {

String str = "My name is John";

System.out.println(str.charAt(0));

System.out.println(str.charAt(4));

}

}

Maตวอยางผลลพธ

length( ) และ replace( )

35

public class StringTest {

public static void main(String args[]) {

String str = "My name is John";

System.out.println( str.length() );

System.out.println( str.replace("J","Y") );

}

}

15My name is Yohnตวอยางผลลพธ

กจกรรมประกำศตวแปร String ทบรรจขอควำมทก ำหนด ใชเมธอดของคลำส String ใดๆก

ได เพอน ำขอควำมออกมำแสดงผลลพธตำมตวอยำง

String htmlContent = "<h1>บรส เอกเซล</h1><p>79 บาท</p>";

36

บรส เอกเซล ราคา 79 บาท

ตวอยางผลลพธ

กจกรรมจงเขยนโปรแกรมนบจ ำนวนค ำทเกบใน String โดยใช method ใดๆ ชวยกได จำก

คลำส String

37

This is a cat = 4 words

ตวอยำงหนำจอโปรแกรม

public class TestApp {public static void main(String arg[]) {

String sentence = "This is a cat";

// เพมโคดตรงน}

}

คลาสมาตรฐาน LocalDateTime

38

import java.time.*;import java.time.format.DateTimeFormatter;import java.time.temporal.ChronoUnit;

public class DateTimeDemo {

public static void main(String[] args) {// ขอวนและเวลาปจจบน

System.out.println(LocalDateTime.now());System.out.println(LocalDate.now());System.out.println(LocalTime.now());System.out.println(LocalDate.now().getYear());

// หาความตางของวนLocalDateTime dateTime1 = LocalDateTime.now();LocalDateTime dateTime2 = LocalDateTime.of(2020, 1, 8, 17, 30);System.out.println(ChronoUnit.DAYS.between(dateTime1, dateTime2));

// จดรปแบบวนเวลาSystem.out.println(dateTime1.format(DateTimeFormatter.BASIC_ISO_DATE));System.out.println(dateTime1.format(DateTimeFormatter.ofPattern("dd MMMM yyyy")));

}

}

2020-02-08T17:37:10.8392020-02-0817:37:10.8402020-312020020808 February 2020

ตวอยางผลลพธ

คลาสมาตรฐาน DecimalFormatใชวธก ำหนดรปแบบดวยสญลกษณ

0 – พมพตำมจ ำนวนหลกทก ำหนด หำกคำไมครบจะเตม 0 # – พมพตำมจ ำนวนหลกทก ำหนด หำกคำไมครบจะไมแสดง . – ระบกำรแสดงทศนยม , – ระบกำรจดกลมตวเลข

39

ตวอยาง

40

import java.text.DecimalFormat;

public class FormatNumberDemo {

public static void main(String[] args) {double number = 1234567.89; DecimalFormat df;

df = new DecimalFormat("#########.###");System.out.println(df.format(number));

df = new DecimalFormat("000000000.000");System.out.println(df.format(number));

df = new DecimalFormat("#.#");System.out.println(df.format(number));

df = new DecimalFormat("#");System.out.println(df.format(number));

df = new DecimalFormat("#,###.#");System.out.println(df.format(number));

}

}

1234567.89001234567.8901234567.912345681,234,567.9

ตวอยางผลลพธ

การแปลง String ไปเปนตวเลข

41

public class ConvertStringDemo {

public static void main(String[] args) {String numberString = "19";

// แปลง String เปน integerint intNumber = Integer.parseInt(numberString);System.out.println( intNumber + 5 );

numberString = "19.59";// แปลง String เปน doubledouble doubleNumber = Double.parseDouble(numberString);System.out.println( doubleNumber - 3 );

}

}

2416.59

ตวอยางผลลพธ

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

สงเกตกำรเปลยนแปลงของโปรแกรมในบรรทดทสงสย

ขนตอนกำร Debug ก ำหนด Break Point ซงหมำยถง ต ำแหนงบรรทดทจะเรมตนตรวจสอบ สงให Debugger เรมท ำงำน ดคำในตวแปรทมกำรเปลยนแปลงในมมมองของกำร Debug

42

การ Debug ใน Eclipse

43

1. ก าหนด break point ทบรรทดใดๆ โดย Double click ทหนาหมายเลขบรรทด

2. เรม Debug โดยใชปมรปแมลง รนโปรแกรม แทนการใชปมรนปกต

4. ใชปมควบคมการ Debug

3. Debugger จะมายง break point ทก าหนด

รนตอจนจบโปรแกรม

หยด Debug Step Into – รนทละบรรทด โดยเขำไปในฟงกชนดวย

Step Over - รนทละบรรทด โดยไมเขำไปในฟงกชนดวย

สำมำรถดตวแปรปจจบนและคำทเกบในตวแปรทหนำตำง Variables

สรำง break point