Upload
usablelabs
View
1.593
Download
2
Embed Size (px)
DESCRIPTION
สื่อการเรียนการสอน เรื่อง โปรแกรมเชิงวัตถุ โดยคุณ Kan http://learners.in.th/file/zniperlll
Citation preview
3. Arithmetic Expression, String class,Wrapper class, Math Class
Faculty of Technology and Environment
Prince of Songkla University, Phuket Campus
2/2551
976-140 Object-Oriented Programming
344-202 Introduction to Object-Oriented Programming
การโปรแกรมเชิงวัตถุ
2Object-Oriented Programming 2/2551
Contents
Java operators
String Class
Wrapper Class
Sending argument to a program
Math Class
Formating Number
3Object-Oriented Programming 2/2551
Arithmetic Expressions
Arithmetic expression คือ นิพจน์ที่เกี่ยวกับการด าเนนิการทาง
คณิตศาสตร์
Arithmetic expression ประกอบด้วย arithmetic operator และ operand
arithmetic operator คือ ตัวด าเนินการทางคณิตศาสตร์ เช่น +, -, *, /
Operand คือ ตัวถูกด าเนินการ อาจเป็น ค่าคงที ่ตัวแปร นิพจน์ทาง
คณิตศาสตรอ์ื่น ๆ หรือเมธอดที่มีการคนืค่ากลับ
a + b * 4 Operator : +, * Operand : a, b, 4
a + (b*c) Operator : + Operand : a, b*c
4Object-Oriented Programming 2/2551
Java operators
น้อย
Pre
ceden
ce
มาก
5Object-Oriented Programming 2/2551
Implicit Type Conversion
ถ้า operand ที่มีชนิดข้อมูลเดียวกัน ผลลัพธ์ที่ได้ก็จะได้ชนิดข้อมลูนั้น
ถ้า operand ต่างชนิดข้อมูลกัน จะมีการแปลงให ้operand มีชนิดเดียวกัน
ก่อนด าเนินการ
double > float > long > int > short > byte
Operation result
23 / 5 4
23 + 5.0 28.0
23 / 5.0 4.6
25.0 / 25.0 1.0
23 % 5 3
23 % 25 23
6Object-Oriented Programming 2/2551
How java compute these expression?
การค านวณค่าทางคณิตศาสตร์ จะเริ่มจาก operator ที่มีล าดับ
ความส าคัญสงูสุดไปต่ าสดุ ถา้ความส าคญัเท่ากันจะท าจากซ้ายไปขวา
-3 * 7 + (20 / 2 – 6 ) / 2
2 * (17 % 3 / 2 + 26 ) * 2 + 4
7Object-Oriented Programming 2/2551
Increment & Decrement
++ และ -– สามารถอยู่หน้าและหลังตัวแปรก็ได้
ในค าสั่งที่มีตวัแปรตัวเดียว ถือว่าเหมือนกัน
x++; หรือ ++x; หมายถึง x = x + 1;
y--; หรือ --y; หมายถึง y = y - 1;
หากมีการด าเนินการอ่ืนด้วย ต าแหน่งของเครื่องหมายจะมีผลต่างกัน
Prefix คือ เครื่องหมายอยู่หน้าตัวแปร
• หมายถึง เพิ่มหรือลดค่าตัวแปรก่อนแล้วน าไปด าเนินการ
Postfix คือ เครื่องหมายอยู่หลังตัวแปร
• หมายถึง ด าเนินการเสร็จแล้วค่อยเพิ่มหรือลดค่าตัวแปรนั้น
8Object-Oriented Programming 2/2551
Assignment Operator
+= x += y; x = x + y;
-= x -= y; x = x - y;
*= x *= y; x = x * y;
/= x /= y; x = x / y;
%= x %= y; x = x % y;
9Object-Oriented Programming 2/2551
Shift Operators
left-shift operator (<<) ตรึง signed bit
-4 << 1: 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 0 (= -8)
Right-shift (>>) – signed แทนที่ signed bit เดิม
-4 >> 1: 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 (= -2)
4 >> 1: 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 (= 2)
Right-shift (>>>) - unsigned
-4 >>> 1: 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 (X)
4 >>> 1: 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 (= 2)
แทนที่ signed bit ด้วย 0
10Object-Oriented Programming 2/2551
Shift Operators
11Object-Oriented Programming 2/2551
Ternary Operators
เป็น operator ที่ประกอบด้วย 3 operands
Short-cut ส าหรับ if/else
รูปแบบ (boolean expression) ? X : Y;
เช่น
มีความหมายเหมือนกับ
12Object-Oriented Programming 2/2551
Try this!
13Object-Oriented Programming 2/2551
Boolean operator
คือ operator ที่ใช้ค านวณทางตรรกศาสตร์ของค่าคงที่ ตัวแปร นิพจน์ทาง
คณิตศาสตรท์ี่อยู่ติดกับ operator ทั้งสองข้าง
& AND ให้ค่า false ถ้าตัวใดตัวหนึ่งเป็น false
| OR ให้ค่า true ถ้าตัวใดตัวหนึ่งเป็น true
^ XOR ให้ค่า true ถ้า operand ทั้งสองข้างมีค่าความจริงไม่ตรงกัน
! NOT เปล่ียนค่าความจริงเป็นตรงข้าม
&& Short-circuit AND เหมือน & แต่ถ้า operator ตัวแรกเป็น false จะ
ให้ค่า false และหยุดตรวจสอบ
|| Short-circuit OR เหมือน | แต่ถ้า operator ตัวแรกเป็น true จะให้
ค่า true และหยุดตรวจสอบ
14Object-Oriented Programming 2/2551
Boolean operator
15Object-Oriented Programming 2/2551
Reference Data Type - ชนดิข้อมูลแบบอ้างอิง
ชนิดข้อมูลในจาวามี 2 แบบคือ
Primitive data type – byte, short, int, long ,char, boolean, double,
float
Reference data type – String, array, class อื่น ๆ
2 แบบนี้มีวิธกีารเก็บข้อมลูในหน่วยความจ าต่างกนั เช่น
Reference คือ address ในหน่วยความจ า
16Object-Oriented Programming 2/2551
String Class
String เป็นคลาสหนึ่งใน Java API ใช้เปน็ชนิดข้อมูลได้
Literal ของตัวแปรที่มีชนดิข้อมูลแบบ String คือ ข้อความที่อยู่ใน
เครื่องหมายค าพูด “…..”
การก าหนดคา่ให้ตัวแปร String ท าได้ 2 แบบ
String s = “Thana”;
String s = new String(“Thana”);
new เป็น keyword ในจาวาส าหรับประกาศตัวแปรที่เป็น reference data
type
17Object-Oriented Programming 2/2551
String Class
การใช้ค าสั่ง new จะมีการสร้าง String ใหม่และจองพื้นทีใ่นหน่วยความจ า
เสมอ
ถ้าไม่ใช้ค าสั่ง new จะตรวจสอบจาก pool หากมีข้อความเดิมก็จะก าหนด
reference ใหช้ี้ไปทีข่้อความนั้น
18Object-Oriented Programming 2/2551
String Class
การก าหนดคา่ใหม่ให้ String เป็นการจองพื้นที่ให้ข้อความใหม ่ และ
ก าหนด reference ไปยังขอ้ความใหม่
19Object-Oriented Programming 2/2551
String Class
ในจาวานั้น ชนิดข้อมูลที่เปน็ primitive data type จะถูกแปลงเป็น String
อัตโนมัติเมื่อท าการต่อกับ String อื่น
ถ้าโปรแกรมเป็นดังนี้
int x = 10;
System.out.println(“x = ” + x);
+ เป็น operator ส าหรับการต่อสตริงเข้าด้วยกัน
ตัวแปร x จะถูกแปลงจากตัวเลขเป็นขอ้ความคือ “10”
ดังนั้นจึงได้ผลลัพธ์เป็นข้อความว่า “x = 10” ส่งให้แก่ค าสั่ง println()
20Object-Oriented Programming 2/2551
String Class
String ที่เป็นตัวเลขไม่สามารถน ามาใช้ในการค านวณได้
การน าตวัเลขต่อกับ String จะได้ผลลัพธเ์ป็น String เท่านั้น
การแปลงจาก primitive data type เป็น String อาจใช้เมธอด
String.valueOf(x)
21Object-Oriented Programming 2/2551
Sending argument to our program
argument คือ ค่าที่เราต้องการส่งเป็น input ใหแ้ก่โปรแกรม เพื่อน าไปใช้
งานในโปรแกรม
การส่ง argument จะส่งผ่าน method โดยระบุช่ือและชนิดของ argument
ไว้ภายในวงเล็บ (………………) หลังชื่อ method
การเขียนเมธอดให้สามารถรับ argument ได้ โดยระบุ argument ภายใน
วงเล็บหลังชื่อเมธอดในรูปแบบ
method_name(data_type argument_name) {}
22Object-Oriented Programming 2/2551
Sending argument to our program
String คือชนิดข้อมูลที่เป็นข้อความ
String[] เป็นการบอกว่า argument นี้เป็นล าดับของ String ที่เรียงต่อกัน
เรียกว่า array โดยอ้างถึง argument ได้ดว้ย ชื่อตัวแปร[ล าดับ]
args คือ ชื่อตวัแปร ซึ่งเป็นชื่อ argument
ล าดับ เริ่มจาก 0,1,2,....
ตัวอย่าง รันโปรแกรมด้วยค าสั่ง
Java MyProgram hello happy 50 PSU
args[0] “hello”
Args[1] “happy”
Args[2] “50”
Args[3] “PSU”
23Object-Oriented Programming 2/2551
Sending argument to our program Greeting
แกไ้ขให้โปรแกรมแสดงข้อความทักทายโดยระบุชื่อผู้ที่พิมพ์ข้อความด้วย
สามารถเขียนโปรแกรมใหม่ได้ดังนี้
24Object-Oriented Programming 2/2551
Run program with argument
method main รับ argument เป็นล าดับของ String ชื่อargument คือ args
เริ่มต้นล าดับที ่0 เช่น argument ตัวแรกคือ args[0] , args[1], args[2],….
เราสามารถเอาค่าที่ส่งมาใช้อา้งถึงในเมธอด main ได้
การคอมไพลโ์ปรแกรม ด้วยค าสั่ง javac Greeting.java
การรันโปรแกรมจาวาใน command prompt นั้น โดยระบุ argument
ต่อท้ายชื่อคลาส ด้วยค าสั่ง
รันโปรแกรม ชื่อโปรแกรม argumentตัวแรก (args[0])
25Object-Oriented Programming 2/2551
Error about argument
What happen when I try to run this program with this command?
java Greeting index ที่เกิด error
หากในโปรแกรมมีการเรียกใช้ argument แต่ไม่ท าการส่งค่าในขณะรัน
โปรแกรม จะเกิดข้อผิดพลาด
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 0 at
Greeting.main(Greeting.java:8)
26Object-Oriented Programming 2/2551
Try this!
How to run this program without error and show this result ?
27Object-Oriented Programming 2/2551
Run Java with argument by EditPlus
28Object-Oriented Programming 2/2551
Get input from console with java.util.Scanner
การรับข้อมูลจากผู้ใช้ผ่านคีย์บอร์ดท าได้ดังนี้
เพิ่มค าสั่ง import java.util.Scanner; ไว้บรรทัดบนสุดของโปรแกรม
ภายในเมธอด ให้ท าการเพิ่มค าสั่ง
Scanner sc = new Scanner(System.in);
ให้เรียกใช้เมธอดการรับค่าตรงกับชนิดข้อมูลที่ต้องการรับ
ตัวอย่าง การรับค่าเลขจ านวนเต็มจากคีย์บอร์ด เก็บในตัวแปร i
int i = sc.nextInt();
29Object-Oriented Programming 2/2551
Get input from console with java.util.Scanner
30Object-Oriented Programming 2/2551
Method in java.util.Scanner
int nextInt()
double nextDouble()
boolean nextBoolean()
byte nextByte()
float nextFloat()
short nextShort()
long nextLong()
String next() //a word
String nextLine() //a line
31Object-Oriented Programming 2/2551
Wrapper Class
แปลง String เป็น primitive data type ที่เป็นตัวเลข จะต้องใช้ Wrapper
class ซึ่งเป็นคลาสใน library มาตรฐานของจาวา
การใช ้Wrapper Class จะใช้คู่กับ Primitive Type แต่ละชนิด Primitive Wrapper Primitive Wrapper
float Float short Short
double Double int Integer
byte Byte long Long
รูปแบบ WrapperClass.Method(String)
ตัวอย่าง แปลง String “5” ให้เป็น int
int num = Integer.parseInt(“5”);
32Object-Oriented Programming 2/2551
Wrapper Class
แปลงจาก String เป็น integer
String input = “5” ;
int i = Integer.parseInt(input); ได้ i ที่เป็น int = 5
// คลาส Integer มีเมธอด parseInt ท าหน้าที่แปลง String เป็นชนิดข้อมูลแบบ int
แปลงจาก String เป็น double
String input = 8.43 ;
double f = Double.parseDouble(input); ได้ f ที่เป็น double = 8.43
// คลาส Double มีเมธอด parseDouble ท าหน้าที่แปลง string เป็นชนิดข้อมูล
แบบ double
33Object-Oriented Programming 2/2551
Using the Math Class
คลาส Math เป็นคลาสใน package java.lang ซึ่งเป็นไลบรารี่มาตรฐานของจาวาที่เรา
สามารถเรียกใช้ได้ในโปรแกรม
การใช้งานเมธอดให้อ้างถึงโดยใช้ ชื่อคลาส.เมธอด() เพราะเป็น static method
static double abs (double)
Returns the absolute value of the argument
static float abs (float)
Returns the absolute value of the argument
static int abs (int)
Returns the absolute value of the argument
static long abs (long)
Returns the absolute value of the argument
34Object-Oriented Programming 2/2551
Using the Math Class
static double exp (double) Returns e raised to the power of the argument
static double log (double) Returns the natural logarithm of the argument
static double pow (double base, double exp) Returns the base raised to the exp power
static double random () Returns a pseudorandom number in the range [0, 1)
static double sqrt (double) Returns the square root of the argument
35Object-Oriented Programming 2/2551
Using the Math Class
Constants of the Math class
PI 3.141592653589793 ( constant Pi (π) )
E 2.718281828459045 ( natural logarithms )
public class CircleAreaApp {
public static void main(String[] args) {
int r = 5;
double area = Math.PI * (r * r);
System.out.println("The area is " + area);
}
}
36Object-Oriented Programming 2/2551
Mathematical Functions
Try this!
37Object-Oriented Programming 2/2551
Rounding functions
Try this!
38Object-Oriented Programming 2/2551
Formating Number