5. 5
การประกาศตัวแปร
[<access_specifier>] [<modifier>] <data_type> <variable_name1>
[, <variable_name2>] ...;
public ใช้นิยามตัวแปรเพื่อให้นาไปใช้กับ Class หรือโปรแกรม
อื่นได้
private ใช้นิยามตัวแปรเพื่อให้ใช้ได้เฉพาะใน Class ที่สร้างตัว
แปรหรือ Method นั้นขึ้นมา
protected ใช้นิยามตัวแปรเพื่อให้ใช้ได้เฉพาะ Class ที่สร้าง
ขึ้นมาด้วยวิธีการสืบทอด (Inheritance) โดยปกติใช้กับ Base
Class
static ใช้นิยามตัวแปรที่ต้องการให้ใช้งานได้กับทุก Method ใน
Class
final ใช้นิยามตัวแปรที่ต้องการใช้เก็บข้อมูลค่าคงที่ (Constant)
ซึ่งไม่สามารถเปลี่ยนค่าได้
6. 6
การกาหนดค่าให้กับตัวแปร
[<access_specifier>] [<modifier>] <data_type>
<variable_name1> = <value> [, <variable_name2> = <value>]...;
หรือ
[<access_specifier>] [<modifier>] <data_type>
<variable_name>; <variable_name> = <value>;
การกาหนดค่าจานวนเต็ม
int r = 1, b = 1, g = 1; long i2 = 300 * 30;
private int employeeid = 0;
int salary; salary = 5000;
int a = 024, b = 036;
int data_c = 0x1D, data_d = 0x36;
12. 12
class test
{ public static void main(String[] args)
{ String fname = "Chutimond";
int n = fname.length();
System.out.println("Length of name = " + n);
String a = fname.substring(0,1);
System.out.println("First character of name = " + a);
String lname = "Bunmark";
String b = lname.substring(2-1,4);
System.out.println("3 char. of lname from position 2 = " + b);
int m = lname.length();
String c = lname.substring(m-1,m);
System.out.println("Last character of surname = " + c); } }
19. 19
// Arithmetic Assignment Operators : Integer
public class IntArithAssOper
{ public static void main(String[ ] args)
{ int x = 1;
System.out.println("Arithmetic Assignment Operators : Integer");
x += 2; System.out.println("x += 2 is " + x);
x -= 2; System.out.println("x -= 2 is " + x);
x *= 2; System.out.println("x *= 2 is " + x);
x /= 2; System.out.println("x /= 2 is " + x);
x %= 2; System.out.println("x %= 2 is " + x); } }
20. 20
// Arithmetic Assignment Operators : Float
public class FloatArithAssOper
{ public static void main(String[ ] args)
{ float y =1f;
System.out.println("Arithmetic Assignment Operators : Float");
y += 2; System.out.println("y += 2 is " + y);
y -= 2; System.out.println("y -= 2 is " + y);
y *= 2; System.out.println("y *= 2 is " + y);
y /= 2; System.out.println("y /= 2 is " + y);
y %= 2; System.out.println("y %= 2 is " + y); } }
21. 21
//Increment and Decrement Operators
public class TestIncDecOper
{ public static void main(String[ ] args)
{ int a, b, x = 1, y = 1;
a = x++; b = ++y;
System.out.println("x = 1 , y = 1");
System.out.println("a = x++ , b = ++y");
System.out.println("x+ = " + x+" , +y = " +y);
System.out.println("a+ = " + a+", +b = " +b); } }
22. 22
//Assignment Operators
public class TestAssOper
{ public static void main(String[ ] args)
{ int x, y;
x = y = 1;
System.out.println("x = y = 1");
System.out.println("x = "+ x + ", y = " + y);
x = (y = y + 1) + 1;
System.out.println("x = (y = y + 1) + 1");
System.out.println("x = "+ x + ", y = " + y); } }
23. 23
Bitwise Operators
Boolean Bitwise Operators
~ (Bitwise Unary Not) & (Bitwise And)
| (Bitwise Or) ^ (Bitwise Exclusive Or)
>> (Shift Right) << (Shift Left)
>>> (Shift Right Zero Fill)
Assignment Bitwise Operators
&= (Bitwise And Assignment)
|= (Bitwise Or Assignment)
^= (Bitwise Exclusive Or Assignment)
>>= (Shift Right Assignment)
<<= (Shift Left Assignment)
>>>= (Shift Right Zero Fill Assignment)
24. 24
//Bitwise Operators
public class TestBitwiseOper
{ public static void main(String[ ] args)
{ int A = 6, B = 7;
System.out.println("A = " + Integer.toBinaryString(A));
System.out.println("B = " + Integer.toBinaryString(B));
System.out.println("~A = " + Integer.toBinaryString(~A));
System.out.println("A & B = " + Integer.toBinaryString(A&B));
System.out.println("A | B = " + Integer.toBinaryString(A|B));
System.out.println("A ^ B = " + Integer.toBinaryString(A^B));
System.out.println("A << 1 = " + Integer.toBinaryString(A<<1));
System.out.println("A >> 1 = " + Integer.toBinaryString(A>>1));
System.out.println("A >>> 1 = " + Integer.toBinaryString(A>>>1));} }
25. 25
Relational Operators
== Equal To != Not Equal To
> Greater Than < Less Than
>= Greater Than or Equal To
<= Less Than or Equal To
Logical Operators
Boolean Logical Operators
& Logical And | Logical Or
^ Logical Exclusive Or ! Logical Unary Not
Short-Circuit Logical Operators
&& Short-Circuit And || Short-Circuit Or
Assignment Logical Operators
&= And Assignment |= Or Assignment
^= Exclusive Or Assignment
26. 26
//Relational Operator
public class TestRelationalOper
{ public static void main(String args[])
{ boolean a = true, b = false;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a & b = " + (a & b));
System.out.println(" a | b = " + (a | b));
System.out.println(" a ^ b = " + (a ^ b));
System.out.println(" !a = " + (!a)); } }
27. 27
// Logical Operator ใช้สำหรับ Operands ที่มีชนิดเป็น boolean เท่ำนั้น
public class TestBooleanLogicalOper
{ public static void main(String args[])
{ boolean a = true, b = false;
System.out.println("a = true : a = " + a);
System.out.println("b = false : b = " + b);
System.out.println();
// กลุ่มที่ 1 Boolean Logical Operators
System.out.println("1. Boolean Logical Operators");
// Logical And
System.out.println("Logical And => a & b = " + (a & b));
// Logical Or
System.out.println("Logical Or => a | b = " + (a | b));
// Logical Exclusive Or
System.out.println("Logical Exclusive Or => a ^ b = " + (a ^ b));
// Logical Unary Not
System.out.println("Logical Unary Not => !a = " + (!a));
System.out.println();
} }
28. 28
public class TestShortandAssLogicalOper
{ public static void main(String args[])
{ boolean a = true, b = false;
System.out.println("a = true : a = " + a);
System.out.println("b = false : b = " + b);
System.out.println();
// กลุ่มที่ 2 Short-Circuit Logical Operators
System.out.println("2. Short-Circuit Logical Operators");
// Short-Circuit And
System.out.println("Short-Circuit And => a && b = " + (a && b));
// Short-Circuit Or
System.out.println("Short-Circuit Or => a || b = " + (a || b));
System.out.println();
// กลุ่มที่ 3 Assignment Logical Operators
System.out.println("3. Assignment Logical Operators");
// And Assignment
System.out.println("And Assignment => a &= b = " + (a &= b));
// Or Assignment
System.out.println("Or Assignment => a |= b = " + (a |= b));
// Exclusive Or Assignment
System.out.println("Exclusive Or Assignment => a ^= b = " + (a ^= b)); } }
29. 29
Conditional Operators
(<condition>) ? <expression 1> : <expression 2>
//Conditional Operator
public class TestCondOper
{ public static void main(String args[])
{ int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
//input x = 5, y = 3
System.out.println((x > y)? x : y); } }
30. 30
ลา้ับในการประมวลผลของ Operator
ลาดับ Operator ประเภท Assoc.
1 ()
2 ++ (Increment), -- (Decrement)
!
~ (Complement)
(type_cast)
การคานวณ
boolean
integer
ทุกรูปแบบ
R
R
R
R
3 *, /, % การคานวณ L
4 +, - การคานวณ L
5 << (Left shift), >> (Right shift),
>>> (Zero fill)
จานวนเต็ม L
6 <, <=, >, >=, Instanceof() การเปรียบเทียบ
object
L
7 ==, != ข ้อมูลพื้นฐาน
และ object
L
31. 31
ลาดับ Operator ประเภท Assoc.
8 & (Bitwise AND) จานวนเต็ม L
9 ^ (Bitwise XOR) จานวนเต็ม L
10 | (Bitwise OR) จานวนเต็ม L
11 && (AND) boolean L
12 || (OR) boolean L
13 ?: boolean R
14 =, *=, /=, %=, +=, -=, <<=, >>=,
>>>=, &=,
^=, !=
อื่น ๆ R
32. 32
ให ้ประมวลผลหาค่าของนิพจน์
a * b – c != a / b – c && --a > b++ || b % --c > 0
1. Increment และ Decrement Operator
2. ตัวกระทาทางคณิตศาสตร์ *, /, %
3. ตัวกระทาทางคณิตศาสตร์ -
4. Relational Operator
5. ไม่เท่ากัน
6. &&
7. ||
1 1 1
2 2 2
3 3
4 4
5
6
7
33. 33
1. จงแส้งผลลัพธ์จากการทางานของโปรแกรมต่อไปนี้ (item1.java)
public class three
{ public static void main(String args[])
{ int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6;
int ans1 = c * c + c % b;
int ans2 = b + e / c - c * d;
int ans3 = b * (a - (d / e) / b) * (b - e % c);
int ans4 = a + b - c / d / e * f;
System.out.println("ans1 is " + ans1 + " และ " + "ans2 is " + ans2);
System.out.println("ans3 is " + ans3 + " และ " + "ans4 is " + ans4);
}
}
การบ ้าน ครั้งที่ 1
34. 34
2. จงแส้งผลลัพธ์จากการทางานของโปรแกรมต่อไปนี้
(item2.java)
public class four
{ public static void main(String args[])
{ boolean a = true, b = true, c = true;
boolean ans1 = !a && b;
boolean ans2 = a && b || c;
boolean ans3 = a || (b && c);
boolean ans4 = a && b || c;
System.out.print("ans1 is " + ans1 + " และ ");
System.out.println("ans2 is " + ans2);
System.out.print("ans3 is " + ans3 + " และ ");
System.out.println("ans4 is " + ans4); } }
35. 35
3. จากข ้อมูลคือ data=Faculty of Liberal Arts and Science
จงเขียนโปรแกรม item3.java เพื่อให ้มีการแส้งผลลัพธ์ว่า
FLAS พร ้อมทั้งให ้แส้งผลตัวอักษร 3 ตัวแรกของชื่อนิสิต
และตัวอักษร 3 ตัวสุ้ท ้ายของนามสกุลนิสิต
ส่งภายในวัน
37. 37
3. การแปลงชนิ้ข ้อมูลโ้ยใช ้Type Wrapper
เป็นการสร ้าง Object ขึ้นมาก่อนที่จะเรียก Method มาใช ้งาน
public double doubleValue()
public float floatValue()
public int intValue()
public long longValue()
public String toString()
38. 38
// Type Conversion : Implicit
public class TestImplicit
{ public static void main(String args[ ])
{ int a = 15/5;
System.out.println("int a = 15/5 => " + a);
// method ชื่อ toBinaryString จำก Class ชื่อ Integer เปลี่ยนข้อมูล Integer
ให้อยู่ในรูป Binary
System.out.println("a = " + Integer.toBinaryString(a));
System.out.println();
// int b = (15/5)*(float)2;
float b = a * (float) 2;
// byte c = c*2;
byte c = 5;
System.out.println("float b = a * (float) 2 => " + b);
System.out.println("byte c = 5");
System.out.println("b/c => " + b/c);
39. 39
/* method ชื่อ floatToIntBits จำก Class ชื่อ Float เปลี่ยนข้อมูล Float
ให้อยู่ในรูป Bit ของจำนวนเต็มแบบ Integer */
System.out.println("Integer.toBinaryString(Float.floatToIntBits(b/c))");
System.out.println(Integer.toBinaryString(Float.floatToIntBits(b/c)));
System.out.println();
double d = 10.0d;
System.out.println("double d = 10.0d");
System.out.println("(b+c)/d => " + (b+c)/d);
/* method ชื่อ doubleToLongBits จำก Class ชื่อ Double เปลี่ยนข้อมูล Double
ให้อยู่ในรูป Bit ของจำนวนเต็มแบบ Long
method ชื่อ toBinaryString จำก Class ชื่อ Long เปลี่ยนข้อมูล Long ให้อยู่ในรูป Binary */
System.out.println("Long.toBinaryString(Double.doubleToLongBits((b+c)/d))");
System.out.println(Long.toBinaryString(Double.doubleToLongBits((b+c)/d))); }}
40. 40
// Type Conversion : Explicit หรือ Type Casting
public class TestExplicit
{ public static void main(String[ ] args)
{ char A = '0';
int B = 2;
System.out.println("char A = '0' => "+(int)A);
System.out.println("int B = 2");
System.out.println("(int) (A/B) => " + (int) (A/B)); } }
59. 59
import java.io.*;
public class TestDataInput
{ public static void main(String args [ ]) throws IOException
{ DataInput input = new DataInputStream(System.in);
String text = " ";
int noOfguest = 0;
double rate = 0;
System.out.print("Amount of guest => : ");
try
{ noOfguest = Integer.parseInt(input.readLine());
// System.out.println("Amount of guest is " + noOfguest);
System.out.print("Rate of rent per person per night is ");
Double x = new Double(input.readLine());
rate = x.doubleValue();}
catch (Exception e)
{ System.out.print(e);
System.exit(1);}
System.out.println("Total rent is " + (rate * noOfguest)); } }