2014-02-27

Java example code for "For Loop"

編解碼的轉換

公式:
y = a * x + b
m = y % n
r = m + diff


y: a * x + b
x: 字元的原始編碼.
a: 為 unicode 編碼 97 ( Variable a ), 隨機整數 0~9.
b: 依次遞增 1 ( Variable b ), 隨機整數 0~9.

m: 餘數, 由 y 除以 n 的餘數取得, 值應在 0~25 之間的整數.
n: 所要轉換的字元數量, 值為 26 (英文字母數). n 為重複 26次.

r: 結果
diff: unicode 編碼系統的差值 97, 因為英文字由 a 取得開始.

由上, 因此餘數 0 的字元會為 a, 餘數 1 的字元會為 b, 餘數 2 的字元為 c, ...., 依此類推.
到餘數 25 字元應為 z.








Unicode

Unicode 編碼中 "a" 為 97. Why???

bin (Binary): 二進位 / 0.1
oct (Octal): 八進位 / 0.1.2.3.4.5.6.7
dec (Decimal): 十進位 / 0.1.2.3.4.5.6.7.8.9
hex: (Hexadecimal): 十六進位 / 0.1.2.3.4.5.6.7.8.9.A.B.C.D.E.F

依下表得知 unicode 的 "a" 為十進位的  "97"

unicode 和 dec 的轉換

Reference Web:  Unicode Character Table



Java 的二三事 - 迴圈 loop

迴圈要項
  • 設定控制變數 Control Variable
  • 條件 Condition
  • 調整控制變數值 Control Value
Example for While:
//========== -begin- =============
public class WhileTest {
   public static void main (String [ ] args) {
         int i  = 10;                                                 //==> 設定控制變數
         while (i > 0) {                                            //==> 條件
              System.out.println (i);
              i --;                                                      //==> 調整控制變數 (由 i 值遞減)
          }
    }
}
//========== -end- ==============

Example for For:
//========== -begin- =============
public class ForTest {
     public static void main ( String [ ] args ) {
           for ( int i = 10; i > 10; i-- ) {                    //==> 要項全寫在小括弧內.
                System.out.println ( i );
           }
      }
}
//========== -end- ==============

Example output:
10
9
8
7
6
5
4
3
2
1

習慣於 While 與 For 的使用

for 迴圈通常用於具有明確重複次數的迴圈.
      因為全部的控制機制都放在 for 之後的小括弧中.

while 迴圈用法為較不明確的重複次數.
        程式通常被設計成一直等待使用者的輸入, 迴圈才會結束.




2014-02-25

Java 的二三事 - 運算子 operator

07- 等號 ( = ), 屬於指派運算子, 就是把等號右邊的值給左邊的 variable (變數), 如果右邊是 object (物件) , 變數就會得到物件的參考.

  • int a = 11;
  • a += 33;  // 11+33, a 等於 44.
  • a -= 22;  // a 為 44, 故 44-22=22, a 等於 22.
  • a *= 11;  // a 為 22, 故 22 * 11 = 242 , a 等於  242.
  • a /= 44;  // a 為 242, 故 242 / 44 = 5.5, a 取得的整數為 5.
  • a %= 55;  / a 等於 5 (% 是餘數, 但不曉得怎求得 ???)

06- boolean 與 關係運算子的 true & false
  • int a = 11;
  • int b = 22;
  • boolean c = a >= b; // a 非小於或等於 b, 所以 c 等於 false.
  • boolean d = a <= b; // a 是小於或等於 b, 所以 d 等於 true.
  • boolean e = c == d; // c 是 false, 而 d 是 true,  故 c 不等於 d, 所以 e 等於 false.
  • boolean f = a != b; // a 確實是不等於 b, 所以 f 等於 true.
05- ( ! ) 邏輯補數運算子, 會把 true 變成 false, false 變成 true.
  • boolean a = true;
  • boolean b = !a // a is true --> b becomes false
  • boolean c = !b // b becomes false --> c is true
04- 每一行程式皆以分號 ( ; ) 結尾, 以分號結尾的程式碼稱為 statement (陳述).

03- variable 變數 - 同 method 方法.

02-  method 方法 - 以英文小寫開頭, 若有多個英文單字組成, 則採取小寫駝峰型撰寫 (lower camel case). example: hellowWorld.

01- class 類別 - 以英文大寫開頭, 若有多個英文單字組成, 則採取大寫駝峰型撰寫 (upper camel case). example: HellowWorld

Java 中英文術語對照

control variable - 控制變數
condition - 條件

infinite loop - 無窮迴圈
for loop - for 迴圈
while loop - while 迴圈

expression -  運算式
operand - 運算元
operator - 運算子

declare - 宣告
identifier - 識別字
statement - 陳述
local variable - 區域變數
method invocation - 方法呼叫

class - 類別
field - 屬性
constructor - 建構子
method - 方法
string - 字串

variable - 變數
object - 物件
literal - 字面常數
subclass - 子類別
superclass - 父類別

boolean value - 布林值
integer - 整數
inherit - 繼承
inheritance - 繼承
thread - 執行緒

Java basic data types, objects

資料型態 Data Type (8)

關鍵字 (keyword) ----- 型態類型 (type) ----- 位元數 (bits) ==> Example

byte ----- 整數 ----- (8bits)
short ----- 整數 ----- (16bits)
int ----- 整數 ----- (32bits) ==> int a = 2;
long ----- 整數 ----- (64bits)

float ----- 浮點數 ----- (32bits)
double ----- 浮點數 ----- (64bits) ==> double a = 2.0;
boolean ----- 布林值 ---- (1bit) ==> boolean a = trun;
char ----- 字元 ---- (16bits) ==> char a = '2';


物件 Object (11)

回傳型態 (return types) ---- 名稱 (name) ----- 功能 (feature)

Object ----- o.clone() ----- 複製回傳 o 本身的拷貝
boolean ----- o.equals(Object obj) ------ 判斷 obj 是否與 o 相同
void ------ o.finalize() ----- 呼叫資源回收者檢查此物件是否應該被回收
Class<?> ----- o.getClass() ----- 回傳類別名稱
int ----- o.hashCode() ----- 回傳物件的雜湊碼
void ----- o.notify() ----- 喚醒等待中的單一執行緒 (thread)
void ----- o.notifyAll() ----- 喚醒等待中的所有執行緒
String ----- o.toString() ----- 回傳物件的字串表達形式
void ----- o.wait() ----- 使 o 的執行緒進行等待
void ----- o.wait(long timeout) ----- 使 o 的執行緒進行等待
void ----- o.wait(long timeout, int nanos) ----- 使 o 的執行緒進行等待


2014-02-17

JAVA 的存取修飾元


[修飾元] - [權限]

public - 皆可存取
private - 在相同的 class 才可以存取
default - 在相同的 package 的 class 才可以存取
protected - 在相同的 package 的 class 才可以存取. 不同的 package, 需有繼承關係才可.

2014-02-13

Eclipse Text Searching

Search the text what you would like to show on workspace.

On menu bar, choose Search --> File to open the Search window.


Unassociate File Types in Windows 7

Purpose: 在 Win7 取消特定附檔名被預設程式開啟

Tool filename: Unassoc (v1.4)
Download from here: https://www.dropbox.com/s/z2pxitiru4md5uf/unassoc_1_4.zip
  • This is a green software, do not need to to the installation.
  • Just run "Unassoc.exe", and that operation UI shows up.

  • Select the file type from the list
  • Click " Delete file type ".
  • The user-specific association for the chosen file type is now removed from the registry. The system will now use the global file association settings.


2014-02-12

Eclipse Basically

Establish your first JAVA project within IDE Eclipse.
  • Choose " File --> New --> Java project ".
  • Fill " lu.ide.first " in Project name field.
  • Make sure a option of " Create separate folders for sources and class file "is checked on Project layout.
  • Press " Finish " button to create your first JAVA project.

2014-02-11

Installing RXTX for Serial Communication with Java in MS platform.



01- Get RXTX from here.

  • Filename: rxtx-2.1-7-bins-r2.zip
  • Link: https://www.dropbox.com/s/gt061ayzxe4kaoi/rxtx-2.1-7-bins-r2.zip
02- Unzip that file, then you will see 3 files on following each path.
Path1: rxtx-2.1-7-bins-r2/Windows/i368-mingw32/
  • rxtxParallel.dll 
  • rxtxSerial.dll

Path2: rxtx-2.1-7-bins-r2/
  • RXTXComm.jar
03- Copy there
2 files rxtxParallel.dll & rxtxSerial.dll to [ C:\Program Files\Java\jdk1.7.0_45\jre\bin ]
1 file RXTXComm.jar to [ C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\ ]



2014-02-06

JAVA Programming Reference from Web


Serial Communication in JAVA with Example Program / Link

Ken Yang 筆記 / Link

Eclipse 簡介與教學 / Link