2011年6月8日 星期三

6/10筆記

sample086 了解資料封裝
1.使用類別時,禁止直接存取該類別變數,而必須透過該類別
   方法,來間接使用這些變數,以提高類別的獨立性和安全行

sample087 建立不能從類別外部存取的成員
1.在程式1中,main方法中使用private變數,所以編譯時會發生錯誤
程式

class airplane {
private double fuel;
private String iro;
void dataset(double num,String str) {
fuel = num;
iro = str;
}
}




public class sample087 {
public static void main(String[] args) {
airplane plane = new airplane();
plane.dataset(100,"紅");


}


}
2.宣告getfuel和gettiro方法來存取private成員
程式

class airplane {
private double fuel;
private String iro;
void dataset(double num,String str) {
fuel = num;
iro = str;
}

double getfuel() {
return fuel;
}

String getiro() {
return iro;
}
 }


public class sample087_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
          airplane plane = new airplane();
          plane.dataset(100, "紅");
          double num = plane.getfuel();
          System.out.println("飛機的燃料有" + num);
          String str = plane.getiro();
          System.out.println("飛機的顏色是"+ str +"色");
}


}

sample088 建立可以從類別外部存取的成員
1.前面加上public
程式
class airplane {
private double fuel;
private String iro;
void dataset(double num,String str) {
fuel = num;
iro = str;
}
public double getfuel() {
return fuel;
}
public String getiro() {
return iro;
}
 }

public class sample088 {
public static void main(String[] args) {
// TODO Auto-generated method stub
          airplane plane = new airplane();
          plane.dataset(100, "紅");
          double num = plane.getfuel();
          System.out.println("飛機的燃料有" + num);
          String str = plane.getiro();
          System.out.println("飛機的顏色是"+ str +"色");
}

}

sample089 多載方法



6/9筆記

sample074 了解物件導向程式設計

sample075 了解類別
sample076 宣告一個類別
[code]/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//建立一個display物件用來顯示文字
class display{
public display() {
System.out.println("Hello World");

}
}

public class claimclass {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
display disp = new display();


}




}
[/code]

sample077 宣告一個物件
sample078 建立一個物件
sample079 了解欄位
sample080 存取欄位
[code]/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//宣告一個類別兩個物件
class airplane {
double fuel;
String iro;
}
public class field {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//使用類別建立物件
airplane plane = new airplane();
//宣告fuel iro 的欄位
plane.fuel = 100;
plane.iro = "紅";
System.out.println("飛機的燃料有" + plane.fuel);
System.out.println("飛機的燃料有" + plane.iro + "色");




}


}
[/code]

sample081 了解方法
sample082 呼叫方法
[code]/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//宣告一個類別兩個物件
class airplane {
double fuel;
String iro;



//宣告方法
void dataset() {
fuel = 100;
iro ="紅";
}
}




 class sample082 {
public static void main(String[] arg) {
// TODO Auto-generated method stub
//使用類別建立物件
airplane plane = new airplane();
//呼叫方法
plane.dataset();


System.out.println("飛機的燃料有" + plane.fuel);
System.out.println("飛機的燃料有" + plane.iro + "色");




}
}




[/code]

sample083 呼叫方法時傳遞參數
[code]/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//宣告一個類別兩個物件
class airplane {
double fuel;
String iro;



//宣告方法
void dataset(double num,String str) {
fuel = num;
iro =str;
}
}




 class sample082 {
public static void main(String[] arg) {
// TODO Auto-generated method stub
//使用類別建立物件
airplane plane = new airplane();
//呼叫方法時傳入參數
plane.dataset(100,"紅");


System.out.println("飛機的燃料有" + plane.fuel);
System.out.println("飛機的燃料有" + plane.iro + "色");




}
}[/code]

sample084 呼叫具有傳回值的方法
[code]class airplane {
double fuel;
String iro;
void dataset(double num,String str) {
   fuel = num;
   iro  = str;
}
 
//宣告傳回double型態的方法
double getfuel() {
return fuel;
}
 
//宣告傳回String型態的方法
String getiro() {
return iro;
}
}


class sample084 {
public static void main(String args[]) {
airplane plane = new airplane();
plane.dataset(100,"紅");

//呼叫傳回double型態的方法
double num = plane.getfuel();
System.out.println("飛機的燃料有" + plane.fuel);

//呼叫傳回String型態的方法
String str = plane.getiro();
System.out.println("飛機的顏色是" + plane.iro + "色");
}
}[/code]

sample085 將每個類別寫成一個檔案
airplane.java
[code]
public class airplane {
double fuel;
String iro;
}[/code]


sample085.java
[code]
public class sample085 {
public static void main(String[] args) {
// TODO Auto-generated method stub
          airplane plane = new airplane();
          plane.fuel = 100;
          plane.iro = "紅";
          System.out.println("飛機的燃料有" + plane.fuel);
          System.out.println("飛機的顏色是" + plane.iro +"色");
}


}[/code]

2011年5月31日 星期二

5/31筆記

sample067-1 複製陣列
1.arraycopy(來源陣列,來源陣列複製起點,目標陣列,目標陣列填入的起點,複製的項數)
2.複製純量陣列:arraycopy方法會複製出一個完全獨立的陣列
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class arraycopy1 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a1[] = {1,2,3};
int a2[] = new int[3];

//複製陣列
System.arraycopy(a1, 0, a2, 0, a1.length);

//更改陣列a2第二個元素
a2[1] = 9;

//顯示a1的內容
System.out.print("陣列a1的元素: ");
for (int i=0;i<a1.length;i++) {
System.out.print(a1[i] + " ");
}

//顯示a2的內容
System.out.print(" 陣列a2的元素 ");
for (int i=0;i<a2.length;i++) {
System.out.print(a2[i] + " ");
}

System.out.println("");

}


}


sample067-2 複製陣列2
1.複製物件陣列:兩個陣列都會改變
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class arraycopy2 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//可以改變使用stringbuffer
    StringBuffer a1[] = new StringBuffer[3];
    StringBuffer a2[] = new StringBuffer[3];
    
    //設定a1陣列的值
    for (int i=0;i<a1.length;i++) {
     a1[i] = new StringBuffer(Integer.toString(i));
     }
    
    //複製陣列
    System.arraycopy(a1, 0, a2, 0, a1.length);
    
    //顯示a1的內容
    System.out.print(" 更改a2元素前陣列a1的元素: ");
    
    for (int i=0;i<a1.length;i++) {
    System.out.print(a1[i] + " ");
    }
    
    System.out.println();
    
    //更改陣列a2第2個元素
    a2[0].replace(0, 1, "9");
    
    //顯示a1的內容
    System.out.print(" 陣列a1的元素: ");
    for (int i=0;i<a1.length;i++) {
    System.out.print(a1[i] + " ");
    }
    
    //顯示a2的內容
    System.out.print(" 陣列a2的元素 ");
    for (int i=0;i<a2.length;i++) {
    System.out.print(a2[i] + " ");
    }
    
    System.out.println("");


}


}


sample068 了解例外
1.在java中,程式執行期間發生的錯誤,稱為例外(exception)
2.會出現arrayindexoutofbounds exception的例外
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//將值存放在超過陣列長度的位置
public class exception {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a[] = new int[3];
System.out.println("將值存入陣列a第5個元素");

a[4] = 10;

System.out.print("陣列a第5個元素是 ");
System.out.println(a[4]);


}


}


sample069 例外處理
1.使用try/catch,可以讓程式在執行時捕捉發生的錯誤,稱為
   ,exception handling(例外處理)
    基本語法:
   try{
        可能發生的動作;
            }catch  (例外類別 變數名) {
                 例外發生時要做的動作;
       }
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class exceptionusetrycatch {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try{
int a[] = new int[3];
System.out.println("將值存入陣列a第5個元素是");

a[4] = 10;

System.out.print("陣列a第5個元素是");
System.out.println(a[4]);


}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("無法將值指定給陣列");


}
}
}




sample070 發生例外也一定執行的動作
1.無論例外有沒有發生,都會執行的動作(finally區塊)
2.無論try區塊裡面有沒有例外發生,finally區塊最後都會執行.即使發生例外,而
   沒有對應的catch區塊,finally區塊也會執行
3.加上finally的例外處理基本語法如下:
   try{
           可能發生例外的動作;
           } catch (例外類別 變數名) {
                例外發生時要做的動作;
           } finally {
                一定要執行的動作;
       }
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class exceptionusefinally {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {
int a[] = new int[3]; 
System.out.println("將值存入陣列a第5個元素");
 
a[4] = 10;
 
System.out.print("陣列a第5個元素是 ");
System.out.println(a[4]);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("無法將值指定給陣列");
}finally {
System.out.println("所有動作都完成了");
}


}


}


sample072 強制拋出例外
1.使用throw
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class exceptionusethrow {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {

throw new IndexOutOfBoundsException();

}catch(IndexOutOfBoundsException e) {
System.out.print("捕捉到 " + e);
System.out.println(" 例外 ");

}


}


}

sample073 使用自己定義的例外
1.繼承Exception類別加以延伸,就可以建立自己定義的例外類別
2.不過這個好像失敗ㄝ
程式
/**
 * 
 */

/**
 * @author vicyuyu311
 *
 */
public class exceptionusemyself {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
   try {
throw new TestException();
} catch (TestException e) {
System.out.print(" 捕捉到  " + e);
System.out.println(" 例外 ");
}

}

}

   class TestException extends Exception {   

        TestException() {
    System.out.println("自定例外的建構子");
  }
   }

















2011年5月30日 星期一

5/30筆記

sample063 取得陣列的長度
1.陣列名稱.length
2.二維陣列時,指定索引值,可以得到該索引的長度(列數)
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class arraylength {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//宣告一維陣列
int a[] = new int[3];
//取得一維陣列的長度
System.out.print("陣列a的長度是 ");
System.out.println(a.length);

//宣告二維陣列
int b[][] = new int[2][3];
//取得二維陣列的長度
System.out.print("陣列b的長度是(行數)是 ");
System.out.println(b.length);
System.out.print("陣列b的長度是(列數)是 ");
System.out.println(b[0].length);


}


}


sample064 設定陣列初始值
1.初始化一維陣列
   int a[ ] = {10, 20, 30}
2.初始化二維陣列
   int a[ ][ ] = {{10,20,30}, {40,50,60}}
   結果
   10 40
   20 50
   30 60
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class arrayinitial {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//初始化一維陣列
int a[] = {10,20,30};
System.out.println("顯示一維陣列的元素");
for(int i=0;i<a.length;i++) {
System.out.print("陣列第 "+ (i+1) + " 項的值是 ");
System.out.println(a[i]);
}

//初始化二維陣列
int b[][] = {{10,20,30},{40,50,60}};
System.out.println("顯示二維陣列的元素");
for(int i=0;i<b.length;i++) {
for(int j=0;j<b[0].length;j++) {
    System.out.print("陣列第 "+ (i+1) + " 行第 ");
    System.out.println((j+1) +" 列的值是 "+b[i][j]);
    
    }
             }
}
}


sample065 在陣列內搜尋
1.使用for迴圈進行尋序搜尋
2.arraysearch方法,找到想要找的值,傳回該值存放索引值+1的數
   ,如果找不到,則傳回-1
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
public class arraysearch {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a[] = {10,20,30,40,50};

//搜尋陣列中元素10的位置
System.out.print("10在陣列的第");
System.out.println(arraysearch(a,10) + "項");

//搜尋陣列中元素40的位置
System.out.print("40在陣列第");
System.out.println(arraysearch(a,40) + "項");
        
//搜尋陣列中元素60的位置
//因為找不到,故傳回-1
System.out.print("60在陣列的第");
System.out.println(arraysearch(a,60) + "項");
}


private static int arraysearch(int array[],int value) {
for (int i=0;i<array.length;i++) {
if (array[i] == value) {
  //傳回所在的位置
   return(i+1);
}
}

//找不到時,傳回-1
return(-1);
}
}




sample066 排序陣列的內容
1.氣泡排序(bubble sort):氣泡排序會不斷比較相鄰兩個
   元素的值,當大小關係相反時就互相交換,一直到沒有
   值需要交換為止.
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//氣泡排序
public class bubblesort {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a[] = {20,50,10,30,40};
System.out.println("排序前的陣列");
for (int i=0;i<a.length;i++) {
System.out.println(a[i]);
}

//將陣列遞增排序
sort(a);

System.out.println("排序後的陣列");
for (int i=0;i<a.length;i++) {
System.out.println(a[i]);

}



}
    public static void sort(int array[]) {
   
    int i = array.length;
    while (--i >= 0) {
     for (int j=0 ;j<i;j++) {
     if (array[j] > array[j+1]) {
      //交換元素值
     int tmp = array[j];
     array[j] = array[j+1];
     array[j+1] = tmp;
     }
     }
    }
    }
}


2.快速排列(quick sort):快速排列是先決定基準值,再以不斷分割資料
   達到排序陣列的效果,將其他元素與該基準值比較,分為較大和較小
   的兩個集合,並重複進行這些動作
程式

/**
 * 
 */


/**
 * @author vicyuyu311
 *
 */
//quick sort
public class quicksort {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a[] = {20,50,10,30,40};
System.out.println("排序前的陣列");
for (int i=0;i<a.length;i++) {
    System.out.println(a[i]);
    }

//將陣列遞增排序
sort(a, 0, a.length-1);

System.out.println("排序後的陣列");
for (int i=0;i<a.length;i++) {
 System.out.println(a[i]);
}
}

static void sort(int array[], int start, int end) {

int low = start;
int high = end;
if (start >= end) {
 return;
}
int mid = array[(start+end)/2];
do {
while (array[low] > mid) {
low++;
}
while (array[high] < mid) {
high--;
}
if (low <= high) {
int temp = array[low];
array[low++] = array[high];
array[high--] = temp;
}
}while (low <= high);
sort(array,start,high);
sort(array,low,end);
}
}