Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.

Similar presentations


Presentation on theme: "Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009."— Presentation transcript:

1

2 Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009

3 2 Strings 宣告一個 String 物件可以用來儲存一個字串 (a sequence of characters) 宣告 String 物件 : String team = "New York Yankees"; String player = new String(“Derek Jeter”); 字串中的每一個字元 (character) 都有一個 index, 第一個字元的 index 是 0, 第二個字元的 index 是 1, 依此類推

4 3 Concatenation 我們可以使用 + 這個運算 (operator) 把兩個字串前後串 起來 (concatenation). Examples: String team = "New York Yankees"; String player = new String(“Derek Jeter”); System.out.println(player + " is a player of " + team); Or String team = "New York Yankees"; String player = new String(“Derek Jeter”); String nbaPlayer = player + " is a player of " + team; System.out.println(nbaPlayer);

5 4 String Method : length 每一個 String 物件都有一組方法 (method) 可以 用來操作、處理這個字串 int length( ) 這裡的 int 是 length 這個方法所傳回 (return) 的值的資料型態 (data type) length 是這個方法的名稱 ( ) 空括號指的是 length 這個方法不須代入參 數 length 的功能是傳回這個字串的字元數 Example: String team = "New York Yankees"; System.out.println(team.length());

6 5 String Method : Substrings String substring(int startIndex) 這個方法會傳回一個 String substring 是這個方法的名稱 (int startIndex) 指的是 這個方法必須代入一個 int 的參 數 功能 : substring 傳回一個新的字串,這個字串的第一 個字元是原字串中具有 startIndex 這個 index 的字元 結束的字元就是原字串的最後一個字元

7 6 String Method : Substrings String substring(int startIndex, int endIndex) 這個方法會傳回一個 String substring 是這個方法的名稱 (int startIndex, int stopIndex) 指的是 這個方法必須代 入兩個 int 的參數 功能 : substring 傳回一個新的字串,這個字串的第一個字 元是原字串中具有 startIndex 這個 index 的字元 結束的字元是具有 stopIndex-1 這個 index 的字元

8 7 String Method : Getting a single character char charAt(int index) 這個方法會傳回一個 char charAt 是這個方法的名稱 (int index) 指的是 這個方法必須代入一個 int 的參數 功能 : charAt 傳回位置在 index 的 character ( 字元 ) 例如 : String team = “New York Yankees"; char teamLetter = team.charAt(9);

9 8 String Method : Replacing characters String replace(char oldChar, char newChar) 這個方法會傳回一個 String replace 是這個方法的名稱 (char oldChar, char newChar) 指的是 這個方法必須 代入兩個 char 的參數 功能 : replace 傳回一個新的字串, 這個字串是由原字 串中把每一個 oldChar 全改成 newChar 所得到的結果. 例如 : String team = “New York Yankees"; System.out.println(team.replace(‘n',‘m'));

10 9 The Math class Math 這個 class 包含一些 methods 用來執行數學運算. 例如 : static double ceil(double num) static double floor(double num) static double sqrt(double num) static double pow(double num, double power) static 這個關鍵字代表的是我們會使用這個類別本身來 使用這些 method (Math.sqrt(…)).

11 10 The Math class Examples double area = Math.PI * radius * radius; double circumference = 2.0 * Math.PI * radius; double s = Math.sqrt(2.0 * Math.pow(r, 2.0)); double squareAreaLB = Math.floor(s * s); double squareAreaUB = Math.ceil(s * s);

12 11 Generating Random Numbers Math 這個 class 包含一個產生亂數的 method 所得到 的 double 是隨機產生的大於或等於 0 且小於 1 的數字 語法如下 : double randNum = Math.random(); 如果需要的亂數是介於其他數字區間時,可以用一些數學 運算得到這些數字。例如 [0, 15) : double randNum = Math.random() * 15.0; [15, 100) : double randNum = Math.random() * 85.0 + 15.0; 如果需要的亂數是整數,也可以用一些數學運算得到這些 數字。例如 : ( 大於等於 0 且小於等於 15 的整數 ) int randNum =(int)(Math.random() * 15.0);


Download ppt "Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009."

Similar presentations


Ads by Google