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
Wednesday, October 10, 2012
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;
}
}
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)
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
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
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囉!
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!"
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!"
Wednesday, July 4, 2012
Java How to Program Ch3-3
Java How to Program Ch3-3
(1)
一個java檔案只能有一個public class, 在一個檔案定義兩個以上的public class會產生syntax error
(2)
A static method is special because it can be called without first creating an object of the class in
which the method is declared.
Static method 不需要創建Object就可以使用囉! 例如要執行主要工作的method "main", 在定義的時候就要用:
public static void main(String arg[]) { }
(3)
class instance creation expression 就是 new, e.x. Random r = new Random();
(4)
Random r = new Random();
以這個例子來說, Random後面的"( )"叫做constructor(建構子), 用在Object剛創造出來時, 去初始化該Object的變數(如果此class需要初始化變數的話...像這個例子就沒有初始化任何參數)
(5)
在java檔案被compile且execute的時候, main這個method會被自動執行, 不需要programmer去寫程式call它
(6)
By convention, method names begin with a lowercase first letter and all subsequent words in the name begin with a capital letter.
ex. methodNumberOne
(7)
javac *.java -> 將該folder下所有的.java檔進行compile!!
javac Alice.java Bob.java Candy.java ->complie這三個java檔案
(8)
What is UML
UML is a graphical language used by programmers to represent object-oriented systems in a standardized manner. 在思考一個龐大程式架構時十分有用
in UML, + displayMessage( ) ->+號的意思是該method是public!
Monday, March 19, 2012
正規表示法
資料來源:
參考http://caterpillar.onlyfun.net/Gossip/JavaGossip-V1/RegularExpression.htm
當使用 String裡面的matches()、replaceAll()等方法可以使用"正規表示法"進行比較或是替換
以下例子
import java.util.Scanner;
public class UseRegularExpression {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
String str = "abcdefgabcabc";
System.out.println(str.replaceAll(".bc", "###"));
System.out.print("輸入手機號碼: ");
str = scanner.next();
// 簡單格式驗證
if(str.matches("[0-9]{4}-[0-9]{6}"))
System.out.println("格式正確");
else
System.out.println("格式錯誤");
System.out.print("輸入href標籤: ");
// Scanner的next()方法是以空白為區隔
// 我們的輸入有空白,所以要執行兩次
str = scanner.next() + " " + scanner.next();
// 驗證href標籤
if(str.matches("<a.+href*=*['\"]?.*?['\"]?.*?>"))
System.out.println("格式正確");
else
System.out.println("格式錯誤");
System.out.print("輸入電子郵件: ");
str = scanner.next();
// 驗證電子郵件格式
if(str.matches(
"^[_a-z0-9-]+([.][_a-z0-9-]+)*@[a-z0-9-]+([.][a-z0-9-]+)*$"))
System.out.println("格式正確");
else
System.out.println("格式錯誤");
}
}
執行結果
| ###defg###### 輸入手機號碼: 0988-100432 格式正確 輸入href標籤: <a href="http://caterpillar.onlyfun.net"> 格式正確 輸入電子郵件: justin@caterpillar.onlyfun.net 格式正確 |
Garbage Collection
Java程式在跑的時候,Garbage Collecter會視情況(一般是看Java heap size)釋放掉沒有再用的memory,以增加效率
另外可以用System.gc()來傳送Garbage Collection的請求,但程式並不會馬上動作
Applet裡面的method
init() : 很重要,可以把它看成是一般程式裡的main
paint() : Applet從java.awt.Container繼承來的method,可以在網頁上畫界面圖
Overrideing & Overloading
Overloading
多載,在相同類別中,定義名稱相同,但引數個數不同或引數型態不同的函式
例如:
public static int parseInt(String s, int radix)
public static int parseInt(String s)
Overriding
覆寫,子類別繼承父類別,但是改寫父類別的方法,方法的名稱、引數和型態都必須相同
多載,在相同類別中,定義名稱相同,但引數個數不同或引數型態不同的函式
例如:
public static int parseInt(String s, int radix)
public static int parseInt(String s)
Overriding
覆寫,子類別繼承父類別,但是改寫父類別的方法,方法的名稱、引數和型態都必須相同
Java is "Architecture Neutral"
Java是被設計來給網路的app使用的,可是網路的世界是由一對雜七雜八的電腦和作業系統串連起來的,所以為了讓Java的app能夠在蘋果的電腦、Android的手機、Asus的筆電都能夠跑得如魚得水 ,我們的compiler(編譯器)會產生出一種architecture-neutral的code。
這compile出來的code有個名字 - bytecode,這個bytecode基本上只是instructions,它並不針對於任何計算機結構,只要有Java的runtime system,在面對不同的機器時,我們就可以輕鬆地將bytecode翻譯成只有那台機器看得懂的語言。
Reference:
Thursday, March 15, 2012
命名實施要點
class命名規則:
1. class的名稱第一個字母一定要大寫,如果有兩個單字,第二的單字的第一個字母也要大寫
1. class的名稱第一個字母一定要大寫,如果有兩個單字,第二的單字的第一個字母也要大寫
ex. public class WhoIsTheImortal
2. 一個.java檔的檔名一定要跟檔案裡面最主要的那個class的名字一模一樣,例如你主要的class叫做"HelloKitty",那你寫的java code檔案就要叫做"HelloKitty.java"
methods命名規則:
習慣第一個小寫, 接下來單字大寫
ex. superCoolFuntion()
methods命名規則:
習慣第一個小寫, 接下來單字大寫
ex. superCoolFuntion()
static(靜態)可以吃嗎?
當一個function被宣告成static時,你要用它的時候不需要先new一個obiect,只要用class name宣告一個reference 就可以直接拿來用
例如:
class ILoveJava(){
static fun(){...};
}
要用的時候
ILoveJava test; //宣告reference, 但不用new
test.fun(); //可以直接用囉
就可以直接使用fun()這個函式
靜態方法(static method)
我們定義了一個靜態的方法,這就意味著告訴Java編譯器,我這個方法不需要創建一個此類的對象即可使用。
一般來說,靜態方法常常為應用程序中的其它類提供一些實用工具所用,在Java的類庫中大量的靜態方法正是出於此目的而定義的。
靜態變量(static variable)
靜態變量與靜態方法類似。所有此類實例共享此靜態變量,也就是說在這類裝載時,只分配一塊存儲空間,所有此類的對象都可以操控此塊存儲空間。
要成為java大神?從class出發!
class中文名字叫"類別",你可把它想像成是一個家
家裡面當然要有成員囉!也就是class member, 在class裡的member有兩類
一類叫做variable(變數)
variable,就是要被拿來操作的東西,數學喜歡把變數叫做x,y,z,也可以把它想像成資料,我們有了資料,當然要有"方法"去處理這些資料囉!
一類叫做method(方法)
最簡單的例子就是加加減減、算平均,都是方法做的事情,如果你曾經寫過c code,method就是function啦!沒甚麼大不了的東西
舉個簡單例子:
家裡面當然要有成員囉!也就是class member, 在class裡的member有兩類
一類叫做variable(變數)
variable,就是要被拿來操作的東西,數學喜歡把變數叫做x,y,z,也可以把它想像成資料,我們有了資料,當然要有"方法"去處理這些資料囉!
一類叫做method(方法)
最簡單的例子就是加加減減、算平均,都是方法做的事情,如果你曾經寫過c code,method就是function啦!沒甚麼大不了的東西
舉個簡單例子:
public class Dog {
String name;
String breed;
String breed;
int age;
public void bark();
public void run();
public void sit();
public void shakeHand();
}
簡單來說,這個class叫做Dog,狗的variable有甚麼呢?有它的名字、品種、還有年齡,至於method當然就是定義它可以做甚麼動作啦!你可以叫他bark()去吠小偷,或是run()追擊搶匪,也可以叫它做一隻訓練有素的狗會做的動作:sit()坐!和shakeHand()握手!
*Java Controlling Access to Members of a Class
有四種:public, private, protected, or package-private (no explicit modifier).
如果不打就是 package-private
Subscribe to:
Posts (Atom)