120 likes | 400 Views
附錄單元: JAVA 複習. 王豐緒 銘傳大學資工系. 單元目標. 能夠安裝 JAVA 能夠 編 譯與 執行簡單的 JAVA 程式 了解 JAVA 基本概念 類別與物件 簡單的輸出入 陣列與 Vector 錯誤處理. 事前的準備工作. 安裝與設定 JAVA 環境 首先安裝 JAVA 執行環境,可以到 http://java.sun.com/j2se/1.4.2/download.html 下载安裝 設定 Window 環境變數,以便執行 javac 和 java
E N D
附錄單元:JAVA複習 王豐緒 銘傳大學資工系
單元目標 • 能夠安裝JAVA • 能夠編譯與執行簡單的JAVA程式 • 了解JAVA基本概念 • 類別與物件 • 簡單的輸出入 • 陣列與Vector • 錯誤處理
事前的準備工作 • 安裝與設定JAVA環境 • 首先安裝 JAVA 執行環境,可以到http://java.sun.com/j2se/1.4.2/download.html 下载安裝 • 設定Window環境變數,以便執行javac和java • 將java 安裝目錄下的 bin 子目錄加到 PATH 環境變數 :PATH = java安裝目錄/bin • Java程式的編譯與執行 • 1.啟動DOS • 2. 進入程式所在目錄 • 3. 下達 javac程式名稱.java 進行編譯 • 4. 下達 java程式名稱 進行執行
範例:主程式 類別名稱 原始碼 public class Welcome1 { public static void main (String args[] ) { //顯示訊息到螢幕 System.out.println( “Welcome to Java programming!” ) ; } } 呼叫System類別的輸出串流out下的println方法 (屬於java.lang類別套件) 編譯與執行 javac Welcome1.java java Welcome1
範例:簡單的人機介面 匯入類別套件 原始碼 importjavax.swing.JOptionPane; public class Welcome2 { public static void main (String args[] ) { JOptionPane.showMessageDialog (null, “Welcome\n to\n Java\n World!”) ; //顯示訊息到螢幕 System.exit( 0 ) ; } } 呼叫JOptionPane類別的showMessageDialog方法 呼叫System類別的exit方法,終止程式執行
範例:輸入資料 匯入類別套件 原始碼 importjavax.swing.JOptionPane ; publicclass Addition{ publicstaticvoidmain (String args[] ) { StringfirstNumber, secondNumber; int number1, number2, sum; firstNumber=JOptionPane.showInputDialog(“輸入第一個數字”) ; secondNumber=JOptionPane.showInputDialog(“輸入第二個數字”) ; number1 = Integer.parseInt(firstNumber); number2 = Integer.parseInt(secondNumber); sum = number1 + number2 ; JOptionPane.showMessageDialog (null, “The sum is: ”+ sum) ; System.exit( 0 ) ; } } 呼叫JOptionPane類別的showInputDialog方法 呼叫Integer類別的parseInt方法將字串轉成整數
陣列 • 宣告 • int c[] = new int [12] ; • String b[]=new String[100]; • int c[] ; c = new int [12]; • String b[]; b=new String[100]; • int d[] = {1, 2, 3, 4, 5} ; • 使用 • c[2] = c[1] + 100;
範例:陣列 匯入類別所有套件 原始碼 importjavax.swing.* ; publicclassInitArray{ publicstaticvoidmain (String args[] ) { String output = “”; int n[] = newint[ 10] ; output += “Subscript\tValue\n”; for (inti=0; i< n.length; i++) output += i + “\t” + n[i] + “\n” ; JTextAreaoutputArea = newJTextArea(11, 10) ; outputArea.setText(output); JOptionPane.showMessageDialog(null, outputArea, “Results”, JOptionPane.INFORMATION_MESSAGE); System.exit( 0 ) ; } } 宣告以及初始化陣列 陣列長度 產生物件 呼叫JTextArea物件的setText方法設定內容
動態陣列:Vector類別 原始碼 importjava.util.*; //Vector類別所再套件 publicclassInitArray2 { publicstaticvoidmain (String args[] ) { Vectorv= new Vector(); Object o = “hello” ; String s = “good bye”; v.addElement( o) ; v.addElement( s) ; v.addElement( new Boolean( true) ) ; v.addElement( new Character (‘z’) ) ; v.addElement( new Integer( 7) ) ; for (int j=0; j < v.size() ; j++) System.out.println(v.elementAt(j) ) ; } } 產生Vector物件 新增內容 顯示內容
錯誤處理:try-catch 指令 (1/2) 原始碼 publicclass Try{ public void test(int x, int y) { System.out.print( x/y ) ; } publicstaticvoidmain (String args[] ) { Try t = new Try() ; t.test(10, 0) ; } } 產生Try物件
錯誤處理:try-catch 指令(2/2) 原始碼 設定捕抓錯誤範圍 publicclass Try2{ public void test(int x, int y) { try { System.out.print( x/y ) ; } catch (Exception e) { System.out.println(“Error Catched”); e. printStackTrace() ; } } publicstaticvoidmain (String args[] ) { Try2 t = new Try2() ; t.test(10, 0) ; } } 捕抓錯誤物件
單元複習 • 安裝JAVA • 編譯與執行簡單的JAVA程式 • 運用JAVA的基本物件 • 類別與物件 • 簡單的輸出入 • 陣列與Vector • 錯誤處理