Friday, December 28, 2012

Java



https://dl.dropbox.com/u/94729309/learning%20java.docx
Chapter 1 Overview
ခရီးတစ္ခုကိုသြားတဲ့အခါ ေျခတစ္လွမ္းကေန စရမွာမွန္တယ္။ ဒါေပမယ့္ မသြားခင္ ကိုယ္သြားရမယ့္ ခရီးရဲ႕ ေျမပံုကို ၾကည့္ထားရင္ပိုအဆင္ေျပမယ္ထင္ပါတယ္။ ေျမပံုေပၚမွာေတာ့ အေသးစိတ္မပါဘူးေပါ့။ ဒီ overview ကို ေျမပံုလုိ႕သတ္မွတ္ျပီးဖတ္ပါ။ ေနာက္အခန္းေတြမွာအေသးစိတ္သြားပါလိမ့္မယ္။


ၾကိဳတင္ျပင္ဆင္မႈ
Java programming language ကိုစတင္ေလ့လာဘို႕အတြက္ ပဏာမျပင္ဆင္မႈအေနနဲ႕ ေအာက္ပါလင့္ ၂ခုကေန လိုအပ္တာေတြကို download ဆြဲပါ။
https://dl.dropbox.com/u/94729309/jdk1.7.0.rar
jdk1.7.0 ကို unzip လုပ္ျပီး install လုပ္ပါ။
eclipse-mobile-juno-win32.rar ကိုလည္း unzip လုပ္ျပီး eclipse ဆိုတဲ့ folder ကို D drive ေအာက္မွာထားပါ။
D drive မရွိလည္း ရွိတဲ့ေအာက္မွာထားပါ။
ဆရာမကေတာ့ data ေတြကို D drive ေအာက္မွာထားတဲ့အေလ့ရွိပါတယ္။
ဒါေတြကို down ရတာ အခက္အခဲရွိရင္ နီးရာ software CD ဆိုင္မွာ၀ယ္လို႕ရပါတယ္။
Java jdk1.7.0 (Java Development Kit version 1.7.0 ) နဲ႕ eclipse လို႕ေျပာ၀ယ္ပါ။
ဒါျပီးရင္ေတာ့ စ discuss လုပ္ပါေတာ့မယ္။
eclipse folder ထဲက eclipse ဆိုတဲ့ ခရမ္းေရာင္အလံုးေလးကို double click လုပ္လုိက္ပါ။
(>>ဆိုတဲ့သေကၤတကိုေတြ႕ရင္ click လုပ္ခုိင္းတယ္လို႕မွတ္ပါ။)
ကြန္ပ်ဴတာ screen မွာ This is a simple java program. ဆိုတဲ့စာသားေလးေပၚလာေအာင္ဘယ္လိုေရးမလဲဆိုတာၾကည့္ၾကရေအာင္။
Project တစ္ခုအရင္ေဆာက္
အရင္ဆံုး project တစ္ခုအရင္တည္ေဆာက္ရမယ္။ test ဆိုတဲ့ project တစ္ခုေဆာက္ဘုိ႕ ေအာက္ကအတုိင္းလုပ္ပါ။
File>>New>>Project>>Java folder ေအာက္က Java Project ကို select လုပ္ျပီး Next>>
Project name ကို test လုိ႕ေပးလိုက္ပါ။ မေပးခ်င္လည္းၾကိဳက္တာေပး။ ဘာေပးေပးရတယ္။ ျပီးရင္ Finish>>
ဘယ္ဘက္ျခမ္းက Package Explorer မွာ test ဆိုတဲ့ folder ေလးေပၚလာမယ္။ ဒါဆို project ေဆာက္ျပီးျပီ။
Class တစ္ခုလုပ္ရေအာင္
Project ထဲမွာ class တစ္ခုထည့္မယ္။ class ကို Example လို႕အမည္ေပးရေအာင္။ ေအာက္ကအတုိင္းလုပ္ပါ။
Package Explorer မွာ test ဆိုတဲ့ folder ေဘးကမွ်ားေလးကို click လုပ္ရင္ src ဆိုတာေတြ႕မယ္။ src ကို right click လုပ္ျပီး New>>Class>>
name ကို Example လုိ႕ေပးျပီး (ၾကိဳက္ရာေပးႏိုင္ပါတယ္) Finish>>
src ေအာက္မွာ default package, အဲဒီေအာက္မွာ Example.java ဆိုျပီးေပၚလာမယ္။ ညာဘက္ျခမ္းမွာလည္း Example.java file ပြင့္ေနျပီး code စတင္ေရးဘုိ႕အသင့္ျဖစ္ေနပါျပီ။ ေအာက္ပါအတုိင္းေပါ့။
public class Example {

}

Class ထဲမွာ Code ေတြေရးမယ္
ေလာေလာဆယ္ေတာ့ အဲဒါကိုဖ်က္လုိက္ျပီး ေအာက္က မွင္နီနဲ႕ျပထားတဲ့ code ေတြကို ကူးထည့္လုိက္ပါ။
/*
This is a simple Java program.
Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
ျပီးရင္ save (Ctrl+s) လုပ္ပါ။
Run ၾကည့္ရေအာင္
Ctrl+F11) ႏွိပ္ပါ။
ျပီးရင္ညာဘက္ေအာက္ဘက္ကိုၾကည့္ပါ။
This is a simple Java program.
ဆိုတာကိုေတြ႕ပါလိမ့္မယ္။ ဒီ program က This is a simple Java program.
ဆိုတဲ့ စာတစ္ေၾကာင္းကို ကြန္ပ်ဴတာ screen မွာေပၚလာေအာင္ေရးတဲ့ program မုိ႕ပါ။
အဲဒီအစား ကိုယ္ေပၚေစခ်င္တဲ့ စာကိုအစားထိုးထည့္ၾကည့္ေပါ့။
ဒါအရိုးစင္းဆံုး program ေလးကို run ၾကည့္တာပါ။



code ေတြကိုေလ့လာၾကည့္ရေအာင္
/*
This is a simple Java program.
Call this file "Example.java".
*/

/*
*/ ဒါကို comment လို႕ေခၚတယ္။ တစ္ေၾကာင္းထက္ပိုတဲ့ စာေတြကို comment လုပ္ခ်င္တဲ့အခါသံုးတယ္။ ဒီႏွစ္ခုၾကားမွာရွိတဲ့ စာေတြ code ေတြကို compiler က compile မလုပ္ဘူးလို႕သာမွတ္ထားပါ။ ကိုယ္ေရးခ်င္တာေရးလို႕ရတယ္။ ဒါဟာ simple java program တစ္ခုျဖစ္ပါတယ္။ ဒီဖုိင္ကို Example.java လုိ႕ေခၚပါတယ္လုိ႕ေျပာထားတာ။ ဒီအပို္င္းမေရးလည္းရတယ္။ ဒါဆိုဘာလို႕ေရးလည္းဆိုေတာ့ ဒီ program ကို ေလ့လာတဲ့သူကိုနားလည္လြယ္ေအာင္ေရးေပးထားတာဘဲ။ စက္ကဖတ္ဘုိ႕မဟုတ္ဘူး။ လူဖတ္ဘို႕ေရးေပးထားတာ။ ေလာေလာဆယ္အဲလိုဘဲမွတ္ထားလုိက္ဦး။
ေနာက္တစ္ခုက
class Example {

java program တစ္ပုဒ္ေရးေတာ့မယ္ဆိုရင္ class ဆိုတာေလး စေရးရတယ္။ ေနာက္က Example ဆိုတာကေတာ့ ဒီ class ရဲ႕အမည္ကိုေျပာတာ။ ကို္ယ္ေပးခ်င္တဲ့အမည္ေပးလို႕ရတယ္။
// Your program begins with a call to main().

// ဆိုတာလည္း comment ဘဲ။ အေပၚက /*
*/
ဆိုတဲ့ comment နဲ႕ဘာကြာလည္းဆိုေတာ့ // က စာတစ္ေၾကာင္းထဲအတြက္ဘဲသံုးႏိုင္တယ္။

/*
*/ ကေတာ့ တစ္ေၾကာင္းထဲေရာ တစ္ေၾကာင္းထက္ပိုတဲ့ စာေတြအတြက္ပါသံုးႏုိင္တယ္။ ဒါဘဲကြာတယ္။
public static void main(String args[]) {

java application တစ္ခုမွာ အနည္းဆံုး method တစ္ခုေတာ့ပါရတယ္။ အဲဒါ main method ဘဲ။ ကို္ယ္ေရးလိုက္တဲ့ application တစ္ခုကို execute လုပ္တဲ့အခါ main method ကေနစလုပ္တယ္။ တစ္နည္းေျပာရရင္ main method ဆိုတာ စမွတ္ဆိုပါေတာ့။ က်န္တာေတြကိုေတာ့ သူ႕ေနရာေရာက္ရင္ရွင္းျပမယ္။ ခုေတာ့ဒီေလာက္ဘဲမွတ္ထားဦး။

System.out.println("This is a simple Java program.");

ကြန္ပ်ဴတာ screen မွာ ကို္ယ္ေပၚေစခ်င္တဲ့ စာေတြကို ဒီလိုပံုစံနဲ႕ေရးေပးရပါတယ္။ ကို္ယ္ေပၚေစခ်င္တဲ့ စာကို “ ” ထဲမွာထည့္ေရးေပးလုိက္ေပါ့။

ထပ္ၾကည့္ရေအာင္

class Example {

public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
ဒါ အရိုးရွင္းဆံုး java program တစ္ခုရဲ႕ပံုစံဘဲ။
class တစ္ခုရွိမယ္။ အဲဒီ class ကိုအမည္ေပးရမယ္။ ဥပမာ
class Example {
}
အနည္းဆံုး method တစ္ခုရွိရမယ္။ အမည္က main ေပါ့။ method ဆိုရင္ အမည္ေနာက္မွာ () ပါရမယ္။ ဥပမာ
public static void main (String args[]){

}

ကြန္ပ်ဴတာ screen ေပၚမွာ hello ဆိုတဲ့စာသားေလးေပၚခ်င္ရင္ဒီလုိေရးမယ္။

System.out.println(“hello”);

ဒါကို main method ထဲထည့္ရမယ္။ ဒီလို

public static void main (String args[]){

          System.out.println(“hello”);

}
main method ကို class ထဲထည့္ရမယ္။ ဒီလို

class Example {

public static void main(String args[]) {

System.out.println("hello");
}
}

Summary
1.    Jdk1.7.0 ကို install လုပ္ပါ။
2.    eclipse folder ကို d drive ေအာက္မွာထားပါ။
3.    eclipse folder ထဲက eclipse ဆိုတဲ့ application ကို run ပါ။
4.    project တစ္ခုတည္ေဆာက္ပါ။ test လုိ႕အမည္ေပးပါ။
5.    project ထဲမွာ class တစ္ခုထည့္ပါ။ Example လို႕အမည္ေပးပါ။
6.    class ထဲမွာ main method ကိုထည့္ပါ။
7.    main method ထဲမွာ ကိုယ္ျဖစ္ေစခ်င္တဲ့ statement ကိုေရးပါ။
8.    run ၾကည့္ပါ။

ကဲေနာက္တစ္ခုဆက္ၾကရေအာင္
ဒီတစ္ခါ variable အေၾကာင္းေလးနည္းနည္းၾကည့္ရေအာင္။
Variable ဆိုတာ memory ေပၚကေနရာတစ္ခုပါဘဲ။
Variable ထဲမွာ data ေတြထည့္လုိ႕ရတယ္။
Variable တစ္ခုကို မသံုးခင္ အရင္ဆံုး ေၾကျငာရပါမယ္။ variable declaration လုပ္တယ္လို႕ေခၚတယ္။ ေၾကျငာတဲ့ပံုစံကေတာ့

Type var-name;

Type ဆိုတာ variable ထဲမွာထည့္မယ့္ data type ကိုေျပာတာပါ။ data type ေတြအမ်ားၾကီးရွိတယ္။ 1, 2, 3, အစရွိတာေတြကို integer data type လုိ႕ေခၚတယ္။ int လုိ႕ေရးရတယ္။
A, b, c အစရွိတာေတြကေတာ့ character data type ေပါ့။ char လုိ႕ေရးရတယ္။ ဒီေလာက္ဘဲ မွတ္ထားဦး။
var-name ဆိုတာက variable name ပါ။ ၾကိဳက္ရာအမည္ေပးႏုိင္တယ္။ ေပးလို႕မရတဲ့အမည္အခ်ိဳ႕ေတာ့ရွိတယ္။ ေနာက္ေတာ့ရွင္းျပမယ္။
ကို္ယ္က variable တစ္ခုကိုေၾကျငာမယ္။ variable name ကို num လုိ႕ေပးခ်င္တယ္ဆိုပါေတာ့။ variable ထဲမွာထည့္ခ်င္တာက integer value ဆိုပါေတာ့။
ဒါဆို ဒီလိုေၾကျငာရတယ္။

int num;

ဒါက memory ေပၚမွာ ေနရာတစ္ခုယူလုိက္ျပီး အဲဒီေနရာကို num ကိုသတ္မွတ္လုိ္က္တာပါ။ ကိုယ္ၾကိဳက္ရာအမည္ကိုေပးႏိုင္ပါတယ္။ num ရဲ႕ေရွ႕မွာပါတဲ့ int ဆိုတာက num ထဲမွာ ဘယ္လို data မ်ိဳးကိုထည့္မယ္ဆိုတာေျပာတာပါ။

ဟုတ္ျပီ။ variable declaration ေတာ့လုပ္ျပီးျပီ data ဘယ္လိုထည့္မလည္း။ ကိုယ္က 100 ထည့္ခ်င္တယ္ဆိုပါေတာ့။ ဒါဆို

num = 100;

ဒီေနရာမွာ = သေကၤတာဟာ ညီမွ်ျခင္းကိုဆိုလိုတာမဟုတ္ဘဲ assign လုပ္တယ္လို႕ဆိုိလုိတယ္။ num ထဲကို 100 ထည့္ုလိုက္တယ္ေပါ့။


အဲဒီ num ထဲမွာရွိေနတဲ့ 100 ဆိုတာကို display လုပ္ျပခ်င္တယ္ဆိုပါေတာ့။ ဒါဆို

System.out.println(num);

ဒါမွမဟုတ္ This is num: 100 လုိ႕ျပခ်င္တာဆိုရင္ေတာ့

System.out.println(“This is num: ”+num);

Variable ထဲက data ကိုထုတ္ျပခ်င္တာဆို “” မလိုဘူး။ ေရွ႕ကေဖာ္ျပထားတာနဲ႕ ေနာက္ကေဖာ္ျပခ်င္တာ ဆက္ဘို႕ဆိုရင္ + သေကၤတကိုသံုးရတယ္။

num ထဲမွာရွိတဲ့တန္ဘိ္ုးကို ၂နဲ႕ေျမွာက္ျပီး num ထဲျပန္ထည့္ခ်င္တယ္ဆိုရင္ ဆိုလုိတာက num ကို ၂ဆတိုးခ်င္ရင္

num = num * 2;

*  ဆိုတဲ့သေကၤတေလးက အေျမွာက္သေကၤတပါ။
num ထဲမွာ ပထမ ၁၀၀ ရွိေနမယ္။ ၂နဲ႕ေျမွာက္ျပီးေနာက္ဆိုရင္ num ထဲမွာ ၂၀၀ ရွိမွာေပါ့။
အဲဒါကို display လုပ္ျပခ်င္ရင္

System.out.print(“The value of num * 2 is”);
System.out.println(num);

print နဲ႕ println ဘာကြာလည္းဆိုရင္ print ဆိုရင္ cursor ကေနာက္တစ္ေၾကာင္းမဆင္းဘူး။ println ကေနာက္တစ္ေၾကာင္းဆင္းတယ္။ ln ဆိုတာ လိုင္းကိုေျပာတာပါ။




ဒါေတြသိျပီးျပီရင္ program တစ္ပုဒ္ေရးရေအာင္။
eclipse ကို run ပါ။
ျပီးခဲ့တဲ့တစ္ခါကတည္ေဆာက္ျပီးသား test ဆိုတဲ့ project ေအာက္မွာဘဲ ေနာက္ထပ္ class တစ္ခုထပ္ထည့္ပါမယ္။ ဒီ class အမည္ကိုေတာ့ Example2 လုိ႕ေပးရေအာင္။
ျပီးရင္ Example2.java မွာ ေအာက္ပါ code ေတြကို copy လုပ္၊ save လုပ္ျပီး run ပါ။

/*
Here is another short example.
Call this file “Example2.java”.
*/
class Example2 {
public static void main(String args[]) {
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}

Output ကေအာက္ပါအတုိင္းျဖစ္ပါလိမ့္မယ္။

This is num: 100
The value of num * 2 is 200

ဒါျပီးရင္ * ေနရာမွာ + ထည့္ျပီး run ၾကည့္ပါ။ Output က 102 ျဖစ္မယ္။
- ထည့္ျပီး run ၾကည့္ပါ။ Output က 98  ျဖစ္မယ္။

/ ထည့္ျပီး run ၾကည့္ပါ။ / ဆိုတာ အစား သေကၤတပါ။ Output က 50 ျဖစ္မယ္။






Two Control Statements
The if Statement
ဒီတစ္ခါ if Statement အေၾကာင္းၾကည့္ရေအာင္။ condition တစ္ခုကိုမွန္လား မွားလား စစ္ေဆးဘုိ႕သံုးတယ္။ ဥပမာ num ထဲမွာ integer value တစ္ခု ထည့္ထားတယ္ဆိုပါေတာ့။ အဲဒီ value ဟာ 100 ထက္နည္းသလားဆိုတာသိခ်င္ရင္

if (num<100)

လို႕စစ္ရတယ္။ 100 ထက္နည္းရင္ num is less than 100 လို႕ display လုပ္ခ်င္တယ္ဆိုပါေတာ့။ ဒါဆိုရင္

if (num < 100) System.out.println(“num is less than 100”);

လို႕ေရးရတယ္။

num = 90;
if (num < 100) System.out.println(“num is less than 100”);

ခုဆို num ထဲမွာ 90 ရွိတယ္။ if (num < 100) နဲ႕စစ္ၾကည့္လိုက္ေတာ့ ဒီ condition ဟာ true ျဖစ္ေနတယ္။ ဒါေၾကာင့္ ေနာက္က statement အတိုင္း num is less than 100 ဆိုတာကို display လုပ္ျပမယ္။ တကယ္လို႕

num = 120;
if (num < 100) System.out.println(“num is less than 100”);

ဆိုရင္ num ထဲမွာ 120 ရွိေနမယ္။ သူက 100 ထက္ၾကီးတာဆိုေတာ့ condition က false ျဖစ္တယ္။ ဒါေၾကာင့္ သူ႕ေနာက္က statement ကို အလုပ္မလုပ္ဘူး။ ဒါေၾကာင့္ ဘာကိုမွ display လုပ္ျပမွာမဟုတ္ဘူး။

ကဲဒီတစ္ခါ operator ေတြကိုၾကည့္ရေအာင္
Operator                Meaning

<                          Less than
>                          Grater than
==                        Equal to (= တခုထဲဆို assignment ပါ။)
နမူနာ program တစ္ခုၾကည့္ရေအာင္။ integer number ၂ခုကို ႏႈိင္းယွဥ္ၾကည့္မယ္။
Integer number ၂ခုဆိုေတာ့ integer variable ၂ခု ကို declare လုပ္ရမွာေပါ့။

int x, y;

x ထဲမွာ ၁၀ ထည့္ၾကည့္မယ္။

x = 10;

y ထဲမွာ ၂၀ ထည့္ထားရေအာင္။

y = 20;

x ဟာ y ထက္ ငယ္သလားစစ္ျပီး ငယ္ရင္ x is less than y လုိ႕ display လုပ္မယ္။

if (x < y) System.out.println(“x is less than y”);

x ကို 2 နဲ႕ေျမွာက္ျပီး x ထဲျပန္ထည့္ခ်င္ရင္

x = x * 2;

(ခုဆို x ထဲမွာဘယ္ေလာက္ျဖစ္သြားမလဲစဥ္းစားၾကည့္ပါ။ ခုနက ၁၀ ထည့္ထားတယ္။ ခု ၂နဲ႕ေျမွာက္လုိက္ေတာ့ ၂၀ ျဖစ္သြားမွာေပါ့။)

ဒီတစ္ခါ x နဲ႕ y equal ျဖစ္သလားစစ္ၾကည့္ရေအာင္။

if (x == y) System.out.println(“x now equal to y”);

(ဘာေျဖထြက္လဲစဥ္းစားၾကည့္ပါ။ x ထဲမွာ ၂၀၊ y ထဲမွာလည္း 20 ဘဲ။ ဒီေတာ့ ဒီ condition က true ျဖစ္တယ္။ ဒါေၾကာင့္ x now equal to y ဆိုတဲ့ စာကို display လုပ္ျပလိမ့္မယ္။)

ဒီတစ္ခါ x ကို 2 နဲ႕ထပ္ေျမွာက္ဦးမယ္။

x = x * 2;
ခုဆို x ထဲမွာ ၄၀ ျဖစ္ေနမယ္။ y ထဲမွာကေတာ့ ၂၀ ဘဲ။

x ဟာ y ထက္ၾကီးသလားဆိုတာစစ္ၾကည့္ရေအာင္။

if (x > y) System.out.println(“x now greater than y”);

(ဘာအေျဖထြက္မလဲစဥ္းစားၾကည့္ပါ။ x က y ထက္ၾကီးေနတာအမွန္ဘဲ။ ဒါေၾကာင့္ ဒီ condition true ျဖစ္တယ္။ ဒါဆို x now greater than y ဆိုတာကို display လုပ္ျပမယ္။)

ဒီတစ္ခါ x ဟာ y နဲ႕တူသလားဆိုတာစစ္ၾကည့္ရေအာင္။

if (x == y) System.out.pritnln(“you won’t see this”);

(အေျဖဘာထြက္မလဲ။ x ထဲမွာ ၄၀ ရွိတယ္။ y ထဲမွာ 20 ရွိတယ္။ ဒါဆို x နဲ႕ y တူမလား။ မတူဘူး။ ဒါဆို ဒီ condition false ျဖစ္တယ္။ ဒါဆိုရင္ သူ႕ေနာက္က statement  အလုပ္မလုပ္ဘူး။ ဒါေၾကာင့္ you won’t see this ဆိုတာကို ျမင္ရမွာမဟုတ္ဘူး။)

ထံုးစံအတုိင္း test project ထဲမွာ class တစ္ခုထပ္ထည့္ရေအာင္။ အမည္ကို IfSample လုိ႔ေပး။ ျပီးရင္ ေအာက္က code ေတြကူးထည့္ျပီး run ၾကည့္ပါ။

/*
Demonstrate the if.
Call this file "IfSample.java".
*/
class IfSample {
public static void main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x < y) System.out.println("x is less than y");
x = x * 2;
if(x == y) System.out.println("x now equal to y");
x = x * 2;
if(x > y) System.out.println("x now greater than y");
// this won't display anything
if(x == y) System.out.println("you won't see this");
}
}

ထြက္လာမယ့္ output က ေအာက္ပါအတုိင္းျဖစ္မယ္။

x is less than y
x now equal to y
x now greater than y



1 comment: