1. 976-140 Object-Oriented Programming
344-202 Introduction to Object-Oriented Programming
การโปรแกรมเชิงวัตถุ
3. Arithmetic Expression, String class,
Wrapper class, Math Class
Faculty of Technology and Environment
Prince of Songkla University, Phuket Campus
2/2551
2. Contents
Java operators
String Class
Wrapper Class
Sending argument to a program
Math Class
Formating Number
Object-Oriented Programming 2/2551 2
8. 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;
Object-Oriented Programming 2/2551 8
25. Error about argument
What happen when I try to run this program with this command?
java Greeting index ที่เกิด error
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 0 at
Greeting.main(Greeting.java:8)
หากในโปรแกรมมีการเรียกใช้ argument แต่ไม่ทาการส่งค่าในขณะรัน
โปรแกรม จะเกิดข้อผิดพลาด
Object-Oriented Programming 2/2551 25
26. Try this!
How to run this program without error and show this result ?
Object-Oriented Programming 2/2551 26
27. Run Java with argument by EditPlus
Object-Oriented Programming 2/2551 27
28. Get input from console with java.util.Scanner
การรับข้อมูลจากผู้ใช้ผ่านคีย์บอร์ดทาได้ดังนี้
เพิ่มคาสั่ง import java.util.Scanner; ไว้บรรทัดบนสุดของโปรแกรม
ภายในเมธอด ให้ทาการเพิ่มคาสั่ง
Scanner sc = new Scanner(System.in);
ให้เรียกใช้เมธอดการรับค่าตรงกับชนิดข้อมูลที่ต้องการรับ
ตัวอย่าง การรับค่าเลขจานวนเต็มจากคีย์บอร์ด เก็บในตัวแปร i
int i = sc.nextInt();
Object-Oriented Programming 2/2551 28
29. Get input from console with java.util.Scanner
Object-Oriented Programming 2/2551 29
30. 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
Object-Oriented Programming 2/2551 30
31. 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”);
Object-Oriented Programming 2/2551 31
32. 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
Object-Oriented Programming 2/2551 32
33. 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
Object-Oriented Programming 2/2551 33
34. 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
Object-Oriented Programming 2/2551 34
35. 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);
}
}Object-Oriented Programming 2/2551 35