คณิตศาสตร์ ตัวห่อหุ้ม...

Preview:

DESCRIPTION

คณิตศาสตร์ ตัวห่อหุ้ม และการจัดรูปแบบ ( Math, Wrapper and Formatting). Nerissa Onkhum. Outline. คลาส Math การรับข้อมูลจากผู้ใช้ ตัวห่อหุ้ม ( Wrappers) การแปลงชนิดข้อมูล (Type Conversions) การจัดรูปแบบ. คลาส Math. การคำนวณ. ง่าย + - * / % ซับซ้อน คลาส Math. เมธอดในคลาส Math. - PowerPoint PPT Presentation

Citation preview

LOGO

Nerissa Onkhum20/04/23 1

20/04/23 2

คลาส Mathการร�บข้อมู ลจากผู้ ใช้ตั�วห่�อห่�มู (Wrappers)

การแปลงช้นิ�ดข้อมู ล (Type Conversions)

การจ�ดร ปแบบ

LOGO

20/04/23 3

คลาส Math คลาส Math

20/04/23 4

ง�าย + - * / %

ซั�บซัอนิ คลาส Math

20/04/23 5

sqrt() ใช้ในิการห่าค�ารากที่!"สอง Math.sqrt(9 ) จะมู!ค�าเที่�าก�บ 3

pow() ใช้ในิการห่าค�ายกก%าล�ง Math.pow(2, 4 ) จะเที่�าก�บ 24 ห่ร&อ 16 นิ�"นิเอง

abs() ใช้ในิการห่าค�าส�มูบ รณ์( Math.abs(-4 ) จะเที่�าก�บ 4

20/04/23 6

ceil() ใช้ในิการห่าค�าเลข้จ%านิวนิเตั)มูนิอยที่!"ส�ดที่!"มูากกว�าตั�วเลข้ที่!"ระบ�ไว (การป+ดข้,-นิ) Math.ceil(3.27 ) จะมู!ค�าเที่�าก�บ 4 Math.ceil(-3.27 ) จะมู!ค�าเที่�าก�บ -3

floor() ใช้ในิการห่าค�าเลข้จ%านิวนิเตั)มูที่!"มูากที่!"ส�ดที่!"นิอยกว�าตั�วเลข้ที่!"ระบ�ไว (การป+ดลง) Math.floor(3.27 ) จะมู!ค�าเที่�าก�บ 3 Math.floor(-3.27 ) จะมู!ค�าเที่�าก�บ -4

20/04/23 7

round() ใช้ในิการป+ดเศษที่ศนิ�ยมูโดยจะห่าเลข้จ%านิวนิเตั)มูที่!"ใกลก�บตั�วเลข้ที่!"ระบ�มูากที่!"ส�ด (ว�เคราะห่( ตั�วเลข้ ถ้า

5>= . ป+ดข้,-นิ 5< . ป+ดลง) Math.round(3.27 ) จะมู!ค�าเที่�าก�บ 3 Math.round(-3.27 ) จะมู!ค�าเที่�าก�บ -3

min() ใช้ห่าค�าตั�วเลข้ที่!"นิอยที่!"ส�ดระห่ว�างเลข้สองตั�วตัามูที่!"ระบ�ไว Math.min(3, 5 ) จะไดค�าเที่�าก�บ 3

max() ใช้ห่าค�าตั�วเลข้ที่!"มูากที่!"ส�ดระห่ว�างเลข้สองตั�วตัามูที่!"ระบ�ไว Math.max(3, 5 ) จะไดค�าเที่�าก�บ 5

20/04/23 8

toDegrees() แปลงมู�มู radian เป2นิมู�มู degreeMath.toDegrees(Math.PI) มู!ค�าเที่�าก�บ 180.0

toRadians() แปลงมู�มู degree เป2นิมู�มู radian Math.toRadians(180) มู!ค�าเที่�าก�บ 3.141592653589793

sin() Math.sin(Math.toRadians(90)) มู!ค�าเที่�าก�บ 1

20/04/23 9

ปริ�มาตริ = (4÷3) × × ริ�ศม 3 public class BallVolume {

public static void main(String[] args) {

double radius = 10;

double volume = 4.0 / 3.0 *

Math.PI *

Math.pow(radius, 3);

System.out.println(volume);

}

}

20/04/23 10

เมูธอด random() return ค�าส��มูในิช้�วง 0 เก&อบถ้,ง 1

โดยไมู�รวมูเลข้ 1 ใช้เมูธอด Math.random() ในิคร�-งแรกอาจไดค�าเป2นิ 0.33456876 คร�-งที่!"สองอาจไดค�าเป2นิ 0.805566743 เป2นิตันิ

20/04/23 11

เมูธอด random() ตั�วอย�าง โปรแกรมูสรางเลข้ส��มูที่!"เป2นิ

จ%านิวนิเตั)มูที่!"อย �ในิช้�วง 0 ถ้,ง 99 เป2นิจ%านิวนิส�บตั�ว

for (int i = 0; i < 10; i++) { double r = Math.random() * 100; int rand = (int) r;

System.out.println(rand);} แปลง double

ให่เป2นิ int

ส��มูตั�วเลข้72

98

40

96

37

..

..

72

98

40

96

37

..

..

LOGO

20/04/23 12

20/04/23 13

คลาส java.util.Scanner เร�"มูโดยการส�ง System.in เพื่&"อ

นิ%าไปสรางว�ตัถ้� Scanner ก�อนิ จากนิ�-นิจ,งเร!ยกเมูธอดในิว�ตัถ้� Scanner เพื่&"อรอร�บข้อมู ลตั�าง ๆ

เมธอด การิทำ�างานnextByte() ร�บข้อมู ลเลข้จ%านิวนิเตั)มูช้นิ�ด Byte

ที่างแป6นิพื่�มูพื่(nextDouble()

ร�บข้อมู ลเลข้ที่ศนิ�ยมูช้นิ�ด Double ที่างแป6นิพื่�มูพื่(

nextFloat() ร�บข้อมู ลเลข้ที่ศนิ�ยมูช้นิ�ด Float ที่างแป6นิพื่�มูพื่(

nextInt() ร�บข้อมู ลเลข้จ%านิวนิเตั)มูช้นิ�ด Int ที่างแป6นิพื่�มูพื่(

next() ร�บข้อมู ลแบบ String ที่างแป6นิพื่�มูพื่(

nextLong() ร�บข้อมู ลตั�วเลข้ช้นิ�ด Long ที่างแป6นิพื่�มูพื่(

nextShort() ร�บข้อมู ลตั�วเลข้ช้นิ�ด Short ที่างแป6นิพื่�มูพื่(

20/04/23

15

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

java.util.Scanner sc;sc = new java.util.Scanner(System.in);

System.out.print("Please enter an integer : ");int i = sc.nextInt();

System.out.print("Please enter a double : ");double d = sc.nextDouble();

System.out.print("Please enter a string : ");String s = sc.next();

System.out.println("Your Integer is " + i);System.out.println("Your Double is " + d);System.out.println("Your String is " + s);

}}

ส�ง System.in เพื่��อน�าไปสริ�างวั�ตถุ� Scanner

ส�ง System.in เพื่��อน�าไปสริ�างวั�ตถุ� Scanner

ริ�บจำ�านวันเต"ม ทำ �ผู้$�ใช้�พื่�มพื่'เข้�ามา

ริ�บจำ�านวันเต"ม ทำ �ผู้$�ใช้�พื่�มพื่'เข้�ามาริ�บจำ�านวันทำศน�ยม ทำ �ผู้$�ใช้�พื่�มพื่'เข้�ามา

ริ�บจำ�านวันทำศน�ยม ทำ �ผู้$�ใช้�พื่�มพื่'เข้�ามาริ�บข้�อควัาม ทำ �ผู้$�ใช้�พื่�มพื่'เข้�ามา

ริ�บข้�อควัาม ทำ �ผู้$�ใช้�พื่�มพื่'เข้�ามา

20/04/23 16

17

import java.util.Scanner;

class MyClass {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

...

}

}

class MyClass {

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

...

}

}

import java.util.Scanner; // เร!ยกใช้คลาส Scannerpublic class calnum {

public static void main(String[] args){int x,y; // ประกาศตั�วแปร x และ y ส%าห่ร�บเก)บเลข้จ%านิวนิ

เตั)มูScanner in = new Scanner(System.in); // สรางออบเจ)กตั( in ส%าห่ร�บร�บ

ข้อมู ลSystem.out.print("Input Number 1 : ");x = in.nextInt(); // ร�บตั�วเลข้จ%านิวนิเตั)มูมูาเก)บในิตั�วแปร xSystem.out.print("Input Number 2 : ");y = in.nextInt(); // ร�บตั�วเลข้จ%านิวนิเตั)มูมูาเก)บในิตั�วแปร ySystem.out.println(x + " + " + y + " = " + (x+y) ); //แสดงผู้ลการบวก

} }

import java.util.Scanner; // เร!ยกใช้คลาส Scannerpublic class calnum {

public static void main(String[] args){int x,y; // ประกาศตั�วแปร x และ y ส%าห่ร�บเก)บเลข้จ%านิวนิ

เตั)มูScanner in = new Scanner(System.in); // สรางออบเจ)กตั( in ส%าห่ร�บร�บ

ข้อมู ลSystem.out.print("Input Number 1 : ");x = in.nextInt(); // ร�บตั�วเลข้จ%านิวนิเตั)มูมูาเก)บในิตั�วแปร xSystem.out.print("Input Number 2 : ");y = in.nextInt(); // ร�บตั�วเลข้จ%านิวนิเตั)มูมูาเก)บในิตั�วแปร ySystem.out.println(x + " + " + y + " = " + (x+y) ); //แสดงผู้ลการบวก

} }

import java.util.Scanner;public class InputData {

public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.print("Please enter a number : ");String s = sc.next();System.out.print("Your number plus 10 equals ");System.out.println(s + 10);

} }

import java.util.Scanner;public class InputData {

public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.print("Please enter a number : ");String s = sc.next();System.out.print("Your number plus 10 equals ");System.out.println(s + 10);

} }

200423/ / 20

ข้อมู ลที่!"ร �บเข้ามูาจากไฟล( จากฐานิข้อมู ล จากผู้ ใช้ป+ญห่าค&อไมู�สามูารถ้นิ%าข้อมู ลซั,"งเป2นิ

ช้นิ�ดข้อความูมูาค%านิวณ์ได

มู�กอย �ในิร ปข้องข้อความู

LOGO

20/04/23 21

200423/ / 22

จาวาสรางกล��มูข้องคลาสที่!"ช้�วยในิการแปลงจากข้อความูเป2นิข้อมู ลช้นิ�ดพื่&-นิฐานิแตั�ละแบบ ห่ร&อแปลงจากช้นิ�ดข้อมู ลพื่&-นิฐานิแตั�ละแบบไปเป2นิข้อความูได

กล��มูข้องคลาสที่!"ช้�วยในิการแปลงช้นิ�ดข้อมู ลเห่ล�านิ!-ว�า Wrapper แปลง String เป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ แปลงจากช้นิ�ดข้อมู ลพื่&-นิฐานิเป2นิ String

มู!ค�าคงที่!"ข้องค�าที่!"มูากส�ดและนิอยข้องช้นิ�ดข้อมู ลพื่&-นิฐานิ

20/04/23 23

ช้นิ�ดข้อมู ลพื่&-นิฐานิ คลาสในิกล��มู Wrapper

boolean Booleanchar Characterbyte Byteshort Shortint Integerlong Longfloat Floatdouble Double

20/04/23 เข้!ยนิโปรแกรมู Java เบ&-องตันิ 24

Boolean bo = new Boolean(true);

Boolean bo = new Boolean("true");

Character c = new Character('c');

Byte by = new Byte((byte)20);

Byte by = new Byte("20");

Short s = new Short((short)20);

Short s = new Short("20");

20/04/23 25

การิแปลงช้น�ดข้�อม$ล ม วั�ธ การิแปลงด�งน + 1. การแปลง wrapper เป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ 2. การแปลง wrapper เป2นิช้นิ�ดข้อมู ลอางอ�ง (wrapper

String) 3. การแปลงสตัร�งเป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ 4. การแปลงช้นิ�ดข้อมู ลพื่&-นิฐานิเป2นิสตัร�ง 5. การแปลงช้นิ�ดข้อมู ลพื่&-นิฐานิ จากข้อมู ลข้นิาดเล)กไปห่าข้อมู ลข้นิาดให่ญ�

6. การแปลงช้นิ�ดข้อมู ลพื่&-นิฐานิ จากข้อมู ลข้นิาดให่ญ�ไปห่าข้อมู ลข้นิาดเล)ก

Wrapper byte Wrapper shortWrapper int Wrapper floatWrapper double

Wrapper byte Wrapper shortWrapper int Wrapper floatWrapper double

เล)ก

ให่ญ�

ให่ญ�

เล)ก

20/04/23

26

Convert Integer to numeric primitive data

types

Convert Integer to numeric primitive data

typesbyteValueshortValue

intValuelongValuefloatValue

doubleValue

byteValueshortValue

intValuelongValuefloatValue

doubleValue

20/04/23

27

 public class IntegerToNumericPrimitiveTypesExample {   public static void main(String[] args) {    Integer intObj = new Integer("10");    //use byteValue method of Integer class to convert it into byte type.    byte b = intObj.byteValue();    System.out.println(b);    //use shortValue method of Integer class to convert it into short type.    short s = intObj.shortValue();    System.out.println(s);     //use intValue method of Integer class to convert it into int type.    int i = intObj.intValue();    System.out.println(i); //use intValue method of Integer class to convert it into long type. long l = intObj.longValue(); System.out.println(l);    //use floatValue method of Integer class to convert it into float type.    float f = intObj.floatValue();    System.out.println(f);     //use doubleValue method of Integer class to convert it into double type.    double d = intObj.doubleValue();    System.out.println(d);  }}

แปลง wrapper เป,น byte

แปลง wrapper เป,น byte

แปลง wrapper เป,น short

แปลง wrapper เป,น short

แปลง wrapper เป,น intแปลง wrapper เป,น int

แปลง wrapper เป,น float

แปลง wrapper เป,น float

แปลง wrapper เป,น double

แปลง wrapper เป,น double

แปลง wrapper เป,น longแปลง wrapper เป,น long

20/04/23 เข้!ยนิโปรแกรมู Java เบ&-องตันิ 28

Scanner sc = new Scanner(System.in);

System.out.print("Please enter a number : ");

String s = sc.next();

Integer i = new Integer(s);

int number = i.intValue();

System.out.print("Your number plus 10 equals ");

System.out.println(number + 10);

20/04/23

29

Convert Integer to numeric primitive data

types

Convert Integer to numeric primitive data

typesการนิ%าว�ตัถ้� Wrapper มูา

แปลงเป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิโดยอ�ตัโนิมู�ตั�

ดวยกลไล autounboxing

การนิ%าว�ตัถ้� Wrapper มูาแปลงเป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ

โดยอ�ตัโนิมู�ตั� ดวยกลไล autounboxing

20/04/23 เข้!ยนิโปรแกรมู Java เบ&-องตันิ 30

Scanner sc = new Scanner(System.in);

System.out.print("Please enter a number : ");

String s = sc.next();

Integer i = new Integer(s);

int number = i;

System.out.print("Your number plus 10 equals ");

System.out.println(number + 10);

การนิ%าว�ตัถ้� Wrapper มูาแปลงเป2นิช้นิ�ดข้อมู ล

พื่&-นิฐานิโดยอ�ตัโนิมู�ตั� ดวยกลไล

autounboxing

การนิ%าว�ตัถ้� Wrapper มูาแปลงเป2นิช้นิ�ดข้อมู ล

พื่&-นิฐานิโดยอ�ตัโนิมู�ตั� ดวยกลไล

autounboxing

20/04/23 เข้!ยนิโปรแกรมู Java เบ&-องตันิ

31

การนิ%าว�ตัถ้� Wrapper มูาแปลงเป2นิช้นิ�ดข้อมู ลเรฟเฟอร(

เรนิซั( นิ�"นิค&อแปลงจาก Wrapper เป2นิ

คลาส Stringโดยการใช้ toString()

การนิ%าว�ตัถ้� Wrapper มูาแปลงเป2นิช้นิ�ดข้อมู ลเรฟเฟอร(

เรนิซั( นิ�"นิค&อแปลงจาก Wrapper เป2นิ

คลาส Stringโดยการใช้ toString()

เข้!ยนิโปรแกรมู Java เบ&-องตันิ

32

byte number = Byte.parseByte(str); //การแปลง String เป2นิ byte

short number = Short.parseShort(str); //การแปลง

String เป2นิ shortlong number = Long.parseLong(str);

// การแปลง String เป2นิ longfloat number =

Float.parseFloat(str); // การแปลง String เป2นิ float

double number = Double.parseDouble(str); //การแปลง

String เป2นิ double

byte number = Byte.parseByte(str); //การแปลง String เป2นิ byte

short number = Short.parseShort(str); //การแปลง

String เป2นิ shortlong number = Long.parseLong(str);

// การแปลง String เป2นิ longfloat number =

Float.parseFloat(str); // การแปลง String เป2นิ float

double number = Double.parseDouble(str); //การแปลง

String เป2นิ double

20/04/23 เข้!ยนิโปรแกรมู Java เบ&-องตันิ 33

ตั�วอย�าง Scanner sc = new Scanner(System.in);

System.out.print("Please enter a number : ");

int number = Integer.parseInt(sc.next());

เข้!ยนิโปรแกรมู Java เบ&-องตันิ

34

การแปลงจาก byte เป2นิ Stringbyte i = 10;String s = Byte.toString(i);

การแปลงจาก short เป2นิ Stringshort i = 10;String s =

Short.toString(i);การแปลงจาก long เป2นิ String

long i = 10L;String s = Long.toString(i);

การแปลงจาก float เป2นิ Stringfloat i = 10.0f;String s = Float.toString(i);

การแปลงจาก double เป2นิ Stringdouble i = 10.0;String s =

Double.toString(i);

การแปลงจาก byte เป2นิ Stringbyte i = 10;String s = Byte.toString(i);

การแปลงจาก short เป2นิ Stringshort i = 10;String s =

Short.toString(i);การแปลงจาก long เป2นิ String

long i = 10L;String s = Long.toString(i);

การแปลงจาก float เป2นิ Stringfloat i = 10.0f;String s = Float.toString(i);

การแปลงจาก double เป2นิ Stringdouble i = 10.0;String s =

Double.toString(i);

20/04/23 เข้!ยนิโปรแกรมู Java เบ&-องตันิ 35

ตั�วอย�าง Scanner sc = new Scanner(System.in);

System.out.print("Please enter a number : ");

String s = Integer.toString(sc.nextInt());

20/04/23 เข้!ยนิโปรแกรมู Java เบ&-องตันิ 36

Integer Integer.MIN_VALUE = -2147483648 Integer.MAX_VALUE = 2147483647

Double Double.MIN_VALUE = 4.9E-324 Double.MAX_VALUE = 1.7976931348623157E308

หล�กการิการิแปลงช้น�ดข้�อม$ล

ช้นิ�ดข้อมู ลตั�วเลข้จ%านิวนิเตั)มูสามูารถ้แปลงให่เป2นิช้นิ�ดข้อมู ลตั�วเลข้ที่ศนิ�ยมูได

ช้นิ�ดข้อมู ล float สามูารถ้แปลงให่เป2นิช้นิ�ดข้อมู ล double ได

ช้นิ�ดข้อมู ลตั�วเลข้จ%านิวนิเตั)มูมู!ข้นิาดเร!ยงก�นิจากนิอยไปมูากด�งนิ!-

byte short int long ช้นิ�ดข้อมู ล char สามูารถ้แปลงให่เป2นิช้นิ�ดข้อมู ล int ได ช้นิ�ดข้อมู ล boolean จะไมู�มู!ความูส�มูพื่�นิธ(ก�บช้นิ�ดข้อมู ล

แบบพื่&-นิฐานิอ&"นิๆ

ในิกรณ์!ที่!"ตั�วถ้ กด%าเนิ�นิการที่�-งสองมู!ช้นิ�ดข้อมู ลที่!"ตั�างก�นิ ภาษาจาวาจะ มู!ห่ล�กการแปลงช้นิ�ดข้อมู ล (type conversion) ด�งนิ!-

ถ้าตั�วถ้ กด%าเนิ�นิการตั�วห่นิ,"งมู!ช้นิ�ดข้อมู ลเป2นิ double ตั�วถ้ ก ด%าเนิ�นิการอ!กตั�วห่นิ,"งจะถ้ กแปลงให่มู!ช้นิ�ดข้อมู ลเป2นิ double

โดยอ�ตัโนิมู�ตั� ถ้าตั�วถ้ กด%าเนิ�นิการที่�-งสองไมู�ไดมู!ช้นิ�ดข้อมู ลเป2นิ double แตั�มู!

ตั�วถ้ กด%าเนิ�นิการตั�วห่นิ,"งที่!"มู!ช้นิ�ดข้อมู ลเป2นิ float ตั�วถ้ กด%าเนิ�นิ การอ!กตั�วห่นิ,"งจะถ้ กแปลงให่มู!ช้นิ�ดข้อมู ลเป2นิ float โดยอ�ตัโนิมู�ตั�

ถ้าตั�วถ้ กด%าเนิ�นิการที่�-งสองไมู�ไดมู!ช้นิ�ดข้อมู ลเป2นิ double ห่ร&อfloat แตั�มู!ตั�วถ้ กด%าเนิ�นิการตั�วห่นิ,"งที่!"มู!ช้นิ�ดข้อมู ลเป2นิ long ตั�ว

ถ้ กด%าเนิ�นิการอ!กตั�วห่นิ,"งจะถ้ กแปลงให่มู!ช้นิ�ดข้อมู ลเป2นิ long โดยอ�ตัโนิมู�ตั�

เข้!ยนิโปรแกรมู Java เบ&-องตันิ

39

5. การิแปลงช้น�ดข้�อม$ลพื่�+นฐานจำากข้�อม$ลข้นาดเล"กไปหาข้�อม$ลข้นาดใหญ่�

- การแปลงข้อมู ลที่!"กวางข้,-นิ (widening conversion) - ภาษาจาวาจะปร�บช้นิ�ดข้อมู ลให่อ�ตัโนิมู�ตั�

- สามูารถ้ก%าห่นิดค�าช้นิ�ดข้อมู ลข้นิาดเล)ก ให่ก�บช้นิ�ดข้อมู ลข้นิาดให่ญ�ไดเลยตั�วอย�าง

1. int i = 4; 2. long l = i;

นิ�พื่จนิ( i จะถ้ กปร�บช้นิ�ดข้อมู ลจากint ให่เป2นิ long โดยอ�ตัโนิมู�ตั�

บรรที่�ดที่!" 1 ประกาศและก%าห่นิดค�า ตั�วแปร i เป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ int มู!ค�าเที่�าก�บ 4บรรที่�ดที่!" 2 การแปลงจาก intไปเป2นิ long โดยการก%าห่นิดค�าไดเลย

เล)ก

ให่ญ�

ต�วัอย�างข้องการิแปลงช้น�ดข้�อม$ล ภาษาจาวาจะปร�บช้นิ�ดข้อมู ลให่อ�ตัโนิมู�ตั� ในิกรณ์!ตั�อไปนิ!-

ก%าห่นิดค�าช้นิ�ดข้อมู ลที่!"เล)กกว�าให่ก�บตั�วแปรช้นิ�ดข้อมู ลที่!"ให่ญ�กว�าอาที่�เช้�นิint i = 4;long l = i;

นิ�พื่จนิ( i จะถ้ กปร�บช้นิ�ดข้อมู ลจาก int ให่เป2นิ long โดยอ�ตัโนิมู�ตั�ก%าห่นิดค�าช้นิ�ดข้อมู ลจ%านิวนิเตั)มูให่ก�บจ%านิวนิเลข้ที่ศนิ�ยมูอาที่�เช้�นิ

double x = 3; นิ�พื่จนิ( 3 จะถ้ กปร�บช้นิ�ดข้อมู ลจาก int ให่เป2นิ double โดย

อ�ตัโนิมู�ตั�

เข้!ยนิโปรแกรมู Java เบ&-องตันิ

41

6. การิแปลงช้น�ดข้�อม$ลพื่�+นฐานจำากข้�อม$ลข้นาดใหญ่�ไปหาข้�อม$ลข้นาดเล"ก

การแปลงข้อมู ลที่!"แคบลง (narrowing conversion) ซั,"งอาจมู!ผู้ลให่เส!ยความูละเอ!ยดข้องข้อมู ลบางส�วนิไป

ช้นิ�ดข้อมู ล ตั�วแปร = (ช้นิ�ดข้อมู ลที่!"ตัองการเปล!"ยนิ)expression;

ตั�วอย�างint amount = (int) 3.0;

แปลงนิ�พื่จนิ(ที่!"มู!ค�า 3.0 ข้อมู ลช้นิ�ด double ให่เป2นิช้นิ�ด int เล)ก

ให่ญ�

ต�วัอย�างข้องการิแปลงช้น�ดข้�อม$ล

ตั�วอย�างที่!"ไมู�ถ้ กตัองint amount = 123L; //illegal

float f = 4.0; //illegal

ต�วัอย�างโปริแกริม public class PromotionDemo {

public static void main(String args[]) { int i;

long l; float fl = 4.2f; i = 4; l = i; fl = i; double x = fl; fl = 4.2; //illegal}

}

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

int i; long l; float fl = 4.2f; i = 4; l = i; fl = i; double x = fl; fl = 4.2; //illegal}

}

ต�วัอย�างข้องTypecasting ตั�วอย�าง

int amount = (int) 3.0; แปลงนิ�พื่จนิ(ที่!"มู!ค�า 3.0 ข้อมู ลช้นิ�ด double ให่เป2นิ

ช้นิ�ด int

ตั�วอย�างที่!"มู!การตั�ดข้อมู ลออกint x;double y = 1.25;x = (int) y;

ต�วัอย�างโปริแกริม public class TypecastingDemo {

public static void main(String args[]) { int b1 = 4; int b2 = 3; byte b3; b3 = (byte)(b1+b2); float fl; fl = (float)3.2;}

}

public class TypecastingDemo {public static void main(String args[]) { int b1 = 4; int b2 = 3; byte b3; b3 = (byte)(b1+b2); float fl; fl = (float)3.2;}

}

LOGO

20/04/23 46

20/04/2347

ริ$ปแบบ

ต�วัแสดงผู้ล.println(ข้�อม$ลทำ �ต�องการิแสดง);

20/04/2348

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

boolean b = true;int i = 2137865423;double d = 7.0/3.0;String s = “Hello”;System.out.println(b);System.out.println(i);System.out.println(d);System.out.println(s);

}}

true21378654

232.33333333333335

Hello

true21378654

232.33333333333335

Hello

LOGO

20/04/23 49

20/04/23 50

ริ$ปแบบ

ต�วัแสดงผู้ล.printf(“ริ$ปแบบ”, ต�วัแปริ1 , ต�วัแปริ2 , …);

ริ$ปแบบ จะเป2นิร ปแบบที่!"ตัองการแสดงผู้ล ซั,"งประกอบไปดวยข้อความู

ปกตั�และร ปแบบการแสดงผู้ล จะนิ%าห่นิาดวยเคร&"องห่มูาย % เสมูอ

ต�วัแปริ เป2นิตั�วแปรห่ร&อว�ตัถ้�ที่!"สอดคลองก�บร ปแบบการแสดงผู้ลที่!"ก%าห่นิด ไวในิ Format

20/04/23

51

ต�วัอย�าง

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

int i = 3277; double d = 132324.25; System.out.printf("%d\n", i); System.out.printf("%f\n", d); System.out.printf("%16f\n", d); System.out.printf("%16.2f\n", d);

System.out.printf("%,16.2f\n", d);}

}

20/04/23 52

ริ$ปแบบ ตั�วแสดงผู้ล.printf("ร ปแบบ", ตั�วแปร1 ,

ตั�วแปร2 , ...); \n ค&อ การข้,-นิบรรที่�ดให่มู� %d แสดงเลข้จ%านิวนิเตั)มู ถ้าตัองการช้�ดข้วาเรา

ตัองระบ�จ%านิวนิช้�องลงไป เช้�นิ %20d ค&อ การส�"งให่พื่�มูพื่(ตั�วเลข้ช้�ดข้วาในิพื่&-นิที่!" 20 ช้�อง

%x แสดงเลข้จ%านิวนิเตั)มูฐานิ 16 %f แสดงเลข้ที่ศนิ�ยมู %,20.2f แสดงเลข้มู!จ�ลภาคและจ�ดที่ศนิ�ยมู

สองตั%าแห่นิ�ง ให่ช้�ดข้วาความูกวางรวมู 20 ช้�อง

20/04/23 53

คลาส NumberFormat getCurrencyInstance() ริ$ปแบบค�า

เง�น getIntegerInstance() ริ$ปแบบ

เลข้จำ�านวันเต"ม getNumberInstance() ริ$ปแบบ

เลข้ทำศน�ยม getPercentInstance() ริ$ปแบบ

เปอริ'เซ็"นต'

20/04/23

54

import java.text.NumberFormat;

public class NumberFormatting{

public static void main (String[] args){

double d = 37625.72558;

NumberFormat nf;

nf = NumberFormat.getCurrencyInstance();

System.out.println(nf.format(d));

nf = NumberFormat.getIntegerInstance();

System.out.println(nf.format(d));

nf = NumberFormat.getNumberInstance();

System.out.println(nf.format(d));

nf = NumberFormat.getPercentInstance();

System.out.println(nf.format(d));

}

}

ß37,625.7337,62

6

37,62

57.26

37,62,57

3

20/04/23

55

ริ$ปแบบการิแสดงผู้ล วั�ถุต�ทำ �ได�จำากเมธอด getCurrencyInstance() จำะ

ได�ข้�อควัามทำ �ข้3+นต�นด�วัยหน�วัยเง�น ม จำ�ลภาคและจำ�ดทำศน�ยม วั�ถุต�ทำ �ได�จำากเมธอด getIntegerInstance() จำะได�

ข้�อควัามทำ �ม จำ�ลภาค และเป,นเลข้จำ�านวันเต"ม วั�ตถุ�ทำ �ได�จำากเมธอด getNumberInstance() จำะได�

ข้�อควัามทำ �ม จำ�ลภาคและจำ�ดทำศน�ยมสามต�าแหน�ง วั�ตถุ�ทำ �ได�จำากเมธอด getPercentInstance() จำะได�

ข้�อควัามทำ �เป,นต�วัเลข้ค$ณด�วัย 100 ม จำ�ลภาคและ % ต�อทำ�าย

20/04/23

56

คลาส DecimalFormat จำ�ดริ$ปแบบการิแสดงผู้ลข้องต�วัเลข้

ต�วัอย�างริ$ปแบบการิแสดงผู้ลค�า ริ$ปแบบ ผู้ลการิจำ�ดริ$ปแบบ

072.5

000. 073.

072.5

#.00 .73

37625.725

#,###.00 37,625.73

37625.725

(#,##0.00) (37,625.73)

37625.725

-#,##0.00 -37,625.73

20/04/23

57

import java.text.DecimalFormat;

public class DecimalFormatting1{

public static void main (String[] args){

double d = 37625.72558;

DecimalFormat df;

df = new DecimalFormat("0.00");

System.out.println(df.format(d));

df = new DecimalFormat("#,##0.00");

System.out.println(df.format(d));

df = new DecimalFormat("-#,##0.00");

System.out.println(df.format(d));

df = new DecimalFormat("(#,##0.00)");

System.out.println(df.format(d));

}

}

37625.7337,62

57.3

-37,62

57.3

(3 7 ,62

57.3

20/04/23

58

ต�วัอย�างควัามละเอ ยดการิแสดงผู้ลวั�นทำ �ค�า ริ$ปแบบ ผู้ลการิจำ�ดริ$ป

แบบวั�นวัาเลนไทำน' ป7 2555

DateFormat.SHORT

14/2/2555

วั�นวัาเลนไทำน' ป7 2555

DateFormat.MEDIUM

14 ก.พื่ .2555

วั�นวัาเลนไทำน' ป7 2555

DateFormat.LONG

14 ก�มภาพื่�นธ' 2555

วั�นวัาเลนไทำน' ป7 2555

DateFormat.FULL

วั�นอ�งคาริทำ � 14

ก�มภาพื่�นธ' 2555

20/04/23

59

import java.text.DateFormat;

import java.util.Date;

public class DateFormatting{

public static void main (String[] args){

Date d = new Date();

DateFormat df;

df = DateFormat.getDateInstance();

System.out.println(df.format(d));

df = DateFormat.getDateInstance(DateFormat.SHORT);

System.out.println(df.format(d));

df = DateFormat.getDateInstance(DateFormat.MEDIUM);

System.out.println(df.format(d));

df = DateFormat.getDateInstance(DateFormat.LONG);

System.out.println(df.format(d));

df = DateFormat.getDateInstance(DateFormat.FULL);

System.out.println(df.format(d));

}

}

2 ก.ค. 2554

2/07/25542 ก.ค. 2554

2 กริกฎาคม 2554

วั�นอ�งคาริทำ � 2 กริกฎาคม 2554

2 ก.ค. 2554

2/07/25542 ก.ค. 2554

2 กริกฎาคม 2554

วั�นอ�งคาริทำ � 2 กริกฎาคม 2554

20/04/23 60

คลาส Math มู!เมูธอดส%าห่ร�บการค%านิวณ์ที่างดานิคณ์�ตัศาสตัร(ช้�-นิส ง

เมูธอด println() ใช้ส%าห่ร�บแสดงผู้ลล�พื่ธ(ออกที่างห่นิาจอ

เมูธอด printf() ใช้แสดงผู้ลล�พื่ธ(เช้�นิเด!ยวก�บเมูธอด println() แตั�สามูารถ้จ�ดร ปแบบการแสดงผู้ลไดห่ลากห่ลาย

คลาส NumberFormat ช้�วยจ�ดร ปแบบการแสดงผู้ลได

20/04/23 61

การิแปลงช้น�ดข้�อม$ล ม วั�ธ การิแปลงด�งน + 1. การแปลง wrapper เป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ 2. การแปลง wrapper เป2นิช้นิ�ดข้อมู ลอางอ�ง (wrapper

String) 3. การแปลงสตัร�งเป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ 4. การแปลงช้นิ�ดข้อมู ลพื่&-นิฐานิเป2นิสตัร�ง 5. การแปลงช้นิ�ดข้อมู ลพื่&-นิฐานิ จากข้อมู ลข้นิาดเล)กไปห่าข้อมู ลข้นิาดให่ญ�

6. การแปลงช้นิ�ดข้อมู ลพื่&-นิฐานิ จากข้อมู ลข้นิาดให่ญ�ไปห่าข้อมู ลข้นิาดเล)ก

Wrapper byte Wrapper shortWrapper int Wrapper floatWrapper double

Wrapper byte Wrapper shortWrapper int Wrapper floatWrapper double

เล)ก

ให่ญ�

ให่ญ�

เล)ก

20/04/23

62

Convert Integer to numeric primitive data

types

Convert Integer to numeric primitive data

typesbyteValueshortValue

intValuelongValuefloatValue

doubleValue

byteValueshortValue

intValuelongValuefloatValue

doubleValue

20/04/23

63

 public class IntegerToNumericPrimitiveTypesExample {   public static void main(String[] args) {    Integer intObj = new Integer("10");    //use byteValue method of Integer class to convert it into byte type.    byte b = intObj.byteValue();    System.out.println(b);    //use shortValue method of Integer class to convert it into short type.    short s = intObj.shortValue();    System.out.println(s);     //use intValue method of Integer class to convert it into int type.    int i = intObj.intValue();    System.out.println(i); //use intValue method of Integer class to convert it into long type. long l = intObj.longValue(); System.out.println(l);    //use floatValue method of Integer class to convert it into float type.    float f = intObj.floatValue();    System.out.println(f);     //use doubleValue method of Integer class to convert it into double type.    double d = intObj.doubleValue();    System.out.println(d);  }}

แปลง wrapper เป,น byte

แปลง wrapper เป,น byte

แปลง wrapper เป,น short

แปลง wrapper เป,น short

แปลง wrapper เป,น intแปลง wrapper เป,น int

แปลง wrapper เป,น float

แปลง wrapper เป,น float

แปลง wrapper เป,น double

แปลง wrapper เป,น double

แปลง wrapper เป,น longแปลง wrapper เป,น long

20/04/23

64

Convert Integer to numeric primitive data

types

Convert Integer to numeric primitive data

typesการนิ%าว�ตัถ้� Wrapper มูา

แปลงเป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิโดยอ�ตัโนิมู�ตั�

ดวยกลไล autounboxing

การนิ%าว�ตัถ้� Wrapper มูาแปลงเป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ

โดยอ�ตัโนิมู�ตั� ดวยกลไล autounboxing

20/04/23 เข้!ยนิโปรแกรมู Java เบ&-องตันิ

65

การนิ%าว�ตัถ้� Wrapper มูาแปลงเป2นิช้นิ�ดข้อมู ลเรฟเฟอร(

เรนิซั( นิ�"นิค&อแปลงจาก Wrapper เป2นิ

คลาส Stringโดยการใช้ toString()

การนิ%าว�ตัถ้� Wrapper มูาแปลงเป2นิช้นิ�ดข้อมู ลเรฟเฟอร(

เรนิซั( นิ�"นิค&อแปลงจาก Wrapper เป2นิ

คลาส Stringโดยการใช้ toString()

เข้!ยนิโปรแกรมู Java เบ&-องตันิ

66

byte number = Byte.parseByte(str); //การแปลง String เป2นิ byte

short number = Short.parseShort(str); //การแปลง

String เป2นิ shortlong number = Long.parseLong(str);

// การแปลง String เป2นิ longfloat number =

Float.parseFloat(str); // การแปลง String เป2นิ float

double number = Double.parseDouble(str); //การแปลง

String เป2นิ double

byte number = Byte.parseByte(str); //การแปลง String เป2นิ byte

short number = Short.parseShort(str); //การแปลง

String เป2นิ shortlong number = Long.parseLong(str);

// การแปลง String เป2นิ longfloat number =

Float.parseFloat(str); // การแปลง String เป2นิ float

double number = Double.parseDouble(str); //การแปลง

String เป2นิ double

เข้!ยนิโปรแกรมู Java เบ&-องตันิ

67

การแปลงจาก byte เป2นิ Stringbyte i = 10;String s = Byte.toString(i);

การแปลงจาก short เป2นิ Stringshort i = 10;String s =

Short.toString(i);การแปลงจาก long เป2นิ String

long i = 10L;String s = Long.toString(i);

การแปลงจาก float เป2นิ Stringfloat i = 10.0f;String s = Float.toString(i);

การแปลงจาก double เป2นิ Stringdouble i = 10.0;String s =

Double.toString(i);

การแปลงจาก byte เป2นิ Stringbyte i = 10;String s = Byte.toString(i);

การแปลงจาก short เป2นิ Stringshort i = 10;String s =

Short.toString(i);การแปลงจาก long เป2นิ String

long i = 10L;String s = Long.toString(i);

การแปลงจาก float เป2นิ Stringfloat i = 10.0f;String s = Float.toString(i);

การแปลงจาก double เป2นิ Stringdouble i = 10.0;String s =

Double.toString(i);

เข้!ยนิโปรแกรมู Java เบ&-องตันิ

68

5. การิแปลงช้น�ดข้�อม$ลพื่�+นฐานจำากข้�อม$ลข้นาดเล"กไปหาข้�อม$ลข้นาดใหญ่�

- ภาษาจาวาจะปร�บช้นิ�ดข้อมู ลให่อ�ตัโนิมู�ตั� - สามูารถ้ก%าห่นิดค�าช้นิ�ดข้อมู ลข้นิาดเล)ก ให่ก�บช้นิ�ดข้อมู ลข้นิาดให่ญ�ไดเลย

ตั�วอย�าง 1. int i = 4;

2. long l = i; นิ�พื่จนิ( i จะถ้ กปร�บช้นิ�ดข้อมู ลจาก

int ให่เป2นิ long โดยอ�ตัโนิมู�ตั� บรรที่�ดที่!" 1 ประกาศและก%าห่นิดค�า ตั�วแปร i เป2นิช้นิ�ดข้อมู ลพื่&-นิฐานิ int มู!ค�าเที่�าก�บ 4บรรที่�ดที่!" 2 การแปลงจาก intไปเป2นิ long โดยการก%าห่นิดค�าไดเลย

เล)ก

ให่ญ�

เข้!ยนิโปรแกรมู Java เบ&-องตันิ

69

6. การิแปลงช้น�ดข้�อม$ลพื่�+นฐานจำากข้�อม$ลข้นาดใหญ่�ไปหาข้�อม$ลข้นาดเล"ก

ช้นิ�ดข้อมู ล ตั�วแปร = (ช้นิ�ดข้อมู ลที่!"ตัองการเปล!"ยนิ)expression;

ตั�วอย�างint amount = (int) 3.0;

แปลงนิ�พื่จนิ(ที่!"มู!ค�า 3.0 ข้อมู ลช้นิ�ด double ให่เป2นิช้นิ�ด int

เล)ก

ให่ญ�

20/04/23 70

ก%าห่นิดตั�วเลข้ด�งนิ!- 42,20,35,44,48,23 ให่เข้!ยนิโปรแกรมูห่าตั�วเลข้มูากที่!"ส�ดในิตั�วเลข้ห่กตั�วนิ!-

ก%าห่นิดตั�วเลข้ด�งนิ!- 4,2,3,5,1,0 ให่เข้!ยนิโปรแกรมูห่าตั�วเลข้นิอยที่!"ส�ดในิตั�วเลข้ห่กตั�วนิ!-

เข้!ยนิโปรแกรมูที่อยล กเตั<า โดยผู้ลล�พื่ธ(จะเป2นิตั�วเลข้จ%านิวนิเตั)มูห่นิ,"งตั�วที่!"มู!ค�าอย �ระห่ว�าง 1 ถ้,ง 6

เข้!ยนิโปรแกรมูส��มูตั�วเลข้ ให่ไดผู้ลล�พื่ธ(เป2นิเลข้ค!"ที่!"มู!ค�าในิช้�วง -010

20/04/23 71

จงเข้!ยนิโปรแกรมู yearDate.java ที่!"อ�านิข้อมู ลจาก keyboard ที่!"เป2นิอาย�ข้องผู้ ใช้ในิร ปแบบข้องป= ให่โปรแกรมูเปล!"ยนิเป2นิจ%านิวนิว�นิ ส�งผู้ลล�พื่ธ(ออกที่างห่นิาจอ (ก%าห่นิดให่ 1 ป=เที่�าก�บ 365 ว�นิ)

จงเข้!ยนิโปรแกรมูค%านิวณ์ห่าพื่&-นิที่!"สามูเห่ล!"ยมู โดยให่ร�บค�าความูส ง และฐานิ จากผู้ ใช้ส ตัรพื่&-นิที่!"สามูเห่ล!"ยมู = ½ × ความูส ง × ฐานิ

LOGO

20/04/23 72