Wednesday, October 10, 2012

What is an Algorithm

Algorithm is...

1. action(s)

2. order(s)

, so give an order for action(s) to decide which action happens after another is Algorithm!

for simplicity, algorithm can be presented by pseudo code, 不是可執行, 可compile的程式, 但是可以清楚的表達該algorithm

Tuesday, October 9, 2012

Constructor 建構子

Constructor 要取得跟該 class一樣的名字, 主要是在用 class 產生 object 時, initialize一些變數用

ex.

public class MyCat {
    private catName;

    public MyCat (String name) // constructor
    {
        catName = name;
    }

}

of course multiple inputs are acceptable


public class MyCat {
    private catName;
    int catAge;

    public MyCat (String name, int age) // constructor
    {
        catName = name;
        catAge = age;
    }

}

Primitive Types vs. Reference Types

boolean, byte, char, short, int, long, float and double - Primitive Types


A primitive-type variable can store exactly one value of its declared type at a time. For
example, an int variable can store one whole number (such as 7) at a time. When another
value is assigned to that variable, its initial value is replaced.


其他的都是 Reference Types (String, 所有其他的class)

Variables vs. Local Variable

variables 如果沒有先initialize的話, 會被令為0 (int, float, long), 或是null (String), 或是false (boolean)

local variable 是在method裡面的variable, 不能被其他的class使用
local variable 一定要initialize不然會有compile error

UML

UML表示法


ClassName
--------------------------------------------
+ variableName : data type // + means a public variable
- variableName : data type // - means a private variable
--------------------------------------------
«constructor» ClassName ( inputName : String )
+ mehodName( attributeName : data type ) // + means a public method
+ mehodName( ) // the method might not need input
+ mehodName( ) : String // the method might return a type such as String, if not written, then it returns void
- mehodName( attributeName : data type ) // - means a private method

import小常識

記得要用JavaSE內建的class之前, 要先進行import的動作

ex.
import java.util.Scanner;

不然每次要用到Scanner的時候都需要另外在前面列出是哪個package的
java.util.Scanner input = new java.util.Scanner( System.in ); //麻煩

但是只有一個package是自動幫你import的, 那就是java.lang

裡面有包含System, String等等class, 所以不用另外再import囉!

Java Index

J
JOptionPane.showMessageDialog( null, "Hello World" ); // static method, 跳出對話視窗
JOptionPane.showInputDialog( "What is your name?" ); // static method, 跳出輸入視窗, 回傳輸入字串

S
Scanner
|| import java.util.Scanner;
|| Sacnner input= new Scanner(System.in); //input是抓鍵盤輸入的值
|| int number1 = input.nextInt() //nextInt(): 取得下一個input的Integer
|| String name = input.nextLine(); //nextLine(): 取得下一個input的字串

String.format( "%s, %d", name, number ); // static method, 回傳一個經過format的字串

System.out.printf( "%s\n%s\n", "Welcome to", "Java Programming!" ); //  static method 印出字, 數字等等

System.out.println( "Hello World!" ); //  static method 印出一行字"Hello World!"