2012.11.09.  Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Introduction to Java Programming Lecture 10 Method Benefits, Declaring, and Calling Methods.
Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
: Arrange the Numbers ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11481: Arrange the Numbers 解題者:李重儀 解題日期: 2008 年 9 月 13 日 題意: 將數列 {1,2,3, …,N}
Introduction to Java Programming Lecture 13 Classes I OO Programming.
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
Introduction to Constructor, Overloading, and Accessibility CS340100, NTHU Yoshi.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章 CS340100, NTHU Yoshi.
建立靜態資料成員 關鍵字: static 靜態資料成員不屬於特定物件,而是該類物件 所共同擁有的,可做為類別範圍裡的靜態變數。
數學軟體簡介 PART II Matlab 介紹. 今天進度: 多項式的處理與分析 多項式的表示 一個 n 次多項式可以表示成 p(x)=a n x n +a n-1 x n-1 +…+a 1 x+a 0 因此,在 Matlab 中可以用一個長度為 n+1 的向量 來表示 p(x) 如下: p=[a.
第一章 變數、常數及資料型態. 變數 C 程式語言的變數名稱 第一個字必須是英文字母或底線 (_) 之後可以是數字, 英文字母或底線 (_) 不可以是保留字 例: Num (Ο) _score (Ο) C&C (X) 8num (X)
JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.
Binary Search (I) Date: June 17, 2009 Introducer: Hsing-Yen Ann.
第 13 章 常數、靜態成員與朋友關 係 13-1 常數物件與成員 13-1 常數物件與成員 13-2 物件成員與巢狀類別 13-2 物件成員與巢狀類別 13-3 靜態成員資料與函數 13-3 靜態成員資料與函數 13-4 朋友關係 13-4 朋友關係.
STAT0_corr1 二變數的相關性  變數之間的關係是統計研究上的一大目標  討論二分類變數的相關性,以列聯表來表示  討論二連續隨機變數時,可以作 x-y 散佈圖觀察它 們的關係強度  以相關係數來代表二者關係的強度.
第 18 章 名稱空間與例外處理 18-1 名稱空間 18-1 名稱空間 18-2 例外處理 18-2 例外處理.
程式註解說明. 2 程式註解格式 塊狀註解 對檔案、 class 、 method 、資料結構、一段程式 …. 等程式區塊 做說明。 第一行的開頭必需為 “/*” 且沒有其他文字,最後一行的開頭 必需以 “*/” 做為結束,在中間每一行的開頭都必需是一個 “*” 。 單行註解 佔據一整行的說明。 以.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
Monte Carlo Simulation Part.2 Metropolis Algorithm Dept. Phys. Tunghai Univ. Numerical Methods C. T. Shih.
第 7 章 程序與函數 7-1 模組化程式設計 7-1 模組化程式設計 7-2 VB.NET 的程序與函數 7-2 VB.NET 的程序與函數 7-3 程序與函數的變數範圍 7-3 程序與函數的變數範圍 7-4 VB.NET 常用的內建函數 7-4 VB.NET 常用的內建函數 7-5 遞迴函數 7-5.
Introduction to Java Programming Lecture 15 Objects and Classes.
第五章 函數與儲存類別. 函數 函數為完成某一次特定任務或工作的小程式 函數的類型 庫存函數 (library functions) 如: scanf() 、 printf() 函數 … 等 此函數為系統所提供可以直接呼叫使用之 自定函數 (user-define functions) 此函數是使用者自行定義之函數.
Chapter 13 塑模靜態觀點:物件圖 Static View : Object Diagram.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
第九章 結構. 結構的宣告 結構 它是由許多不同 ( 或相同 ) 資料型態的變數所組成的 集合,通常利用結構標籤稱呼此集合 struct student { char *name; int score; }; struct 為保留字,表示結構的宣告開始 結構項目需定義於大括號「 { } 」內 結尾需加上分號.
第三部分:研究設計 ( 二): 研究工具的信效度 與研究效度 (第九章之第 306 頁 -308 頁;第四章)
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
: Tight words ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: : Tight Words 解題者:鐘緯駿、林一帆 解題日期: 2006 年 03 月 14 日 題意: 給定數字 k 與 n (0 ≦ k.
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
類別函式庫. Java Bean 一個單獨的 AWT 元件都是一個 bean. Java Bean 也有一個 bean 的 class 可繼承, 但是並不強 迫一定要繼承. Bean 的結構 Properties 屬性 : 描述 bean 的屬性 如顏色, 大小,label 等. 透過一對 methods.
第12章 建立多表單與模組的應用程式 12-1 Visual Basic的方案與專案 12-2 模組化Visual Basic應用程式
1 政大公企中心產業人才投資課程 -- 企業決策分析方法 -- 黃智聰 政大公企中心產業人才投資課程 課程名稱:企業決策分析方法 授課老師:黃智聰 授課內容: 質化因素在企業決策分析之重要性 參考書目: Hill, C. R., W. E. Griffiths, and G. G. Judge, (2001),
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
Outline Overloading Methods The Scope of Variables The Math Class Floating point Format [Sample code] TestMethodOverloading.java.
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
資料結構實習-一 參數傳遞.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
第 9 章 TSR 程式基本教練. 本章提要 TSR 程式 以熱鍵 (Hot key) 叫用 TSR 程式 Clock 中斷 int 08h 、 int 1ch DOS reentrant 的問題 有用的 TSR 程式.
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
Pointer and Array 指標 與 陣列 double *a, b[10]; char name[80], *np;
Chapter 2. Recurrence Relations (遞迴關係)
第 17 章 樣板 17-1 樣板的基礎 17-1 樣板的基礎 17-2 樣板函數 17-2 樣板函數 17-3 樣板類別 17-3 樣板類別 17-4 非型態參數的樣板類別 17-4 非型態參數的樣板類別 17-5 樣板類別的繼承 17-5 樣板類別的繼承 17-6 特化樣板與靜態成員 17-6 特化樣板與靜態成員.
Chapter 10 m-way 搜尋樹與B-Tree
第12章 多重表單與模組的應用程式 12-1 VB.NET的方案與專案 12-2 模組化VB.NET應用程式 12-3 多重表單的應用程式
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
JAVA 程式設計與資料結構 第十六章 Hash Tables. Introduction Hash Tables 結構為一個 Array ,稱之為 Bucket array 。 如果想要新增一個物件,要根據這個物件的特性 將其加入 Hash Table 內。 Bucket Array 用 A 來代替,其.
Inheritance and Polymorphism
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
連續隨機變數 連續變數:時間、分數、重量、……
Java class Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java.
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
Visual C++重點複習.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
11 Ch05 遞迴 淡江大學 周清江 1. 2  遞迴函數乃是一個自己反覆呼叫自己的函數  一個典型的遞迴演算法 n! = n * (n-1)! = n * (n-1) * (n-2)! = n * (n-1) * (n-2) * (n-3)! = … = n * (n-1) * (n-2)
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
C 語言練習題 2010/12/2. C 程式的格式 一、 C 程式的格式 (1). /* …. */ 是 C 程式的 ______ main() 的內容是由敘述構成的 (2). 敘述要以 __ 為結束符號 (3). 變數、函數都要做 ____ 的宣告 (4). ‘=’ 是 ____ 用的算符 (5).
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
Introduction to Java Programming Lecture 16 Super and Sub Classes.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
物件導向實習 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average.
1 運算子多載 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所.
方法 ( 函數 ) 自訂方法的運用 內建的類別及方法 1. method 的認識 Java 把函數稱為 method method 可用如下的語法來定義: 2 [public static] 傳回值型態 method 名稱 ( 型態 引數 1, 型態 引數 2,...) { 程式敘述 ; return.
P. 1 C++ 的多面性 (Overloading) o 使一個算符或函式具有處理多種資料型別能力的方法稱為複載 (Overloading) o C++ 中對複載的意義為 : 讓同一種名稱或用法具有多種意義 o 當我們定義多個具有相同名稱, 但卻 m 有不同參數個數或 m 相同參數個數但參數型別不同.
> Polymorphism. Polymorphism - 2 多型的功能 多型提供了「介面與實作分離」的一個重 要性,能將 what (是什麼)自 how (怎麼 做)之中抽離。 多型( polymorphism )除去型別之間的耦 合關係。
Speaker: Liu Yu-Jiun Date: 2009/4/15
Presentation transcript:

 Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java TestRandomCharacter 、 RandomCharacter.java

定義 : Method name is the same but the operation will be different.

解釋 : Overloading 有人將他稱為「多載」,是指說在 「相同類別」中,定義「名稱相同」,但是「引 數個數不同」,或是「引數型態不同」的函式, 這樣 Java 就可以根據引數的個數、或是引數的 型態,呼叫到對應的函式。

程式 : TestMethodOverloading.java 課本 p.220

public class TestMethodOverloading { public static void main(String[] args) { // Invole the max method with int parameters System.out.println("The maximum between 3 and 4 is " + max(3,4)); System.out.println("The maximum between 3.0 and 5.4 is " + max(3.0,5.4)); System.out.println("The maximum between 3.0 and 5.4, and is " + max(3.0,5.4,10.14)); } // Return the max between two int values public static int max(int num1, int num2) { if(num1 > num2) return num1; else return num2; } // Find the max between two double values public static double max(double num1, double num2) { if(num1 > num2) return num1; else return num2; } // Return the max among three double values public static double max(double num1, double num2, double num3) { return max( max(num1,num2), num3 ); }

max(3,4)max(3.0,5. 4) max(3.0,5.4,10.14) public static int max(int num1, int num2) public static double max(double num1, double num2) public static double max(double num1, double num2, double num3)

程式 : AmbiguousOverloading.java 課本 p.221

public class AmbiguousOverloading { public static void main(String[] args) { // Invole the max method with int parameters System.out.println( max(3,4) ); } public static double max(int num1, double num2) { if(num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if(num1 > num2) return num1; else return num2; }

[ 變數的有效範圍 ] public static void main() { int a = 20; for( int b=0 ; b<10 ; b++ ) { System.out.println(b); } System.out.println(a); } The scope of a The scope of b 課本 5.9 節

[ Java 變數重複宣告的錯誤 ] public static void main() { int a = 20; for( int a=0 ; a<10 ; a++ ) { System.out.println(a); } System.out.println(a); } The scope of a 課本 5.9 節

C 在巢狀 blocks 中 可以宣告同名的變數 Java 不行

 C 有「全域變數」和「區域變數」的概念  Java 只有「區域變數」的概念  Java 的全域變數比較類似 Class variable ( 定義在 method 裡的變數, 是一種 local variable) ※ In Java, all variables that are not local variables are fields of a class. 課本 p.195

 Trigonometric  Exponent  Rounding  min, max, abs  random  Java API : 課本 5.10 節

範圍 : 0 <= Math.random() < 1.0 EX: (int) (Math.random() * 10) 0 to (int) (Math.random() * 50) 50 to 99

程式 : TestRandomCharacter.java RandomCharacter.java 課本 p.227

 Random integer  (int)(Math.random()) *( )  (int)((int)’a’ + Math.random() * ((int)’z’-(int)’a’+1))  Random char  (char)(‘a’+Math.random()*(‘z’-’a’+1))

public class TestRandomCharacter { public static void main(String[] args) { final int NUMBER_OF_CHARS =175; final int CHARS_PER_LINE =25; for(int i=0;i<NUMBER_OF_CHARS;i++){ char ch =RandomCharacter.getRandomLowerCaseLetter(); if((i+1)% CHARS_PER_LINE ==0){ System.out.println(ch); }else{ System.out.print(ch); }

import java.lang.Math; public class RandomCharacter { public static char getRandomCharacter(char ch1,char ch2){ return (char)(ch1+Math.random()*(ch2-ch1+1)); } public static char getRandomLowerCaseLetter(){ return getRandomCharacter('a','z'); } public static char getRandomUpperCaseLetter(){ return getRandomCharacter('A','Z'); } public static char getRandomCharacter(){ return getRandomCharacter('\u0000','\uFFFF'); } //section 2.17 :character has a unique Unicode between 0 and FFFF in hexadecimal(65535 in decimal)

1. 喜巴辣 電腦與玩家各擲兩顆骰子 方法 getRandomNum() 產生 1~6 的亂數 方法 compare(a,b), 比較點數大小

 Ppt 下載: 