Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.

Slides:



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

Introduction to Java Programming Lecture 10 Method Benefits, Declaring, and Calling Methods.
Pricing and Power Control in a Multicell Wireless Data Network Po Yu Chen October, 2001 IEEE Journal on Select Areas in Communications.
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Introduction to Java Programming Lecture 13 Classes I OO Programming.
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
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 =
: Boxes ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11003: Boxes 解題者:蔡欣燁 解題日期: 2007 年 3 月 19 日.
: ShellSort ★★☆☆☆ 題組: Problem D 題號: 10152: ShellSort 解題者:林一帆 解題日期: 2006 年 4 月 10 日 題意:烏龜王國的烏龜總是一隻一隻疊在一起。唯一改變烏龜位置 的方法為:一隻烏龜爬出他原來的位置,然後往上爬到最上方。給 你一堆烏龜原來排列的順序,以及我們想要的烏龜的排列順序,你.
JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
: The Playboy Chimp ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10611: The Playboy Chimp 解題者:蔡昇宇 解題日期: 2010 年 2 月 28 日 題意:給一已排序的數列 S( 升冪.
1 網路同步學習 如何使用中山大學管理學院知識管理平台 愷中 製作. 2 如何登入中山大學網路學習平台 1. 首先, 請輸入 2. 點選申請帳號, 依照螢幕所示, 輸入個人資訊.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
Introduction to Java Programming Lecture 15 Objects and Classes.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
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.
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
Digital Signal Processing with Examples in M ATLAB ® Chap 1 Introduction Ming-Hong Shih, Aug 25, 2003.
: 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.
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
: 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.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
第 9 章 TSR 程式基本教練. 本章提要 TSR 程式 以熱鍵 (Hot key) 叫用 TSR 程式 Clock 中斷 int 08h 、 int 1ch DOS reentrant 的問題 有用的 TSR 程式.
Teacher : Ing-Jer Huang TA : Chien-Hung Chen 2015/6/25 Course Embedded Systems : Principles and Implementations Weekly Preview Question CH 2.4~CH 2.6 &
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
: Expect the Expected ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11427: Expect the Expected 解題者:李重儀 解題日期: 2008 年 9 月 21 日 題意:玩一種遊戲 (a game.
Introduction to Java Programming, 4E Y. Daniel Liang.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Chapter 2. Recurrence Relations (遞迴關係)
Introduction to Java Programming Lecture 9 Flow Control : while do-while and for loops II.
Chapter 10 m-way 搜尋樹與B-Tree
: 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 來代替,其.
1 Introduction to Java Programming Lecture 6 Program Input : BufferedReader & Scanner.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
第 6 章 迴圈結構 6-1 計數迴圈 6-1 計數迴圈 6-2 條件迴圈 6-2 條件迴圈 6-3 巢狀迴圈 6-3 巢狀迴圈 6-4 While/End While 迴圈 6-4 While/End While 迴圈 6-5 跳出迴圈 6-5 跳出迴圈 6-6 VB.NET 的錯誤處理 6-6 VB.NET.
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
Visual C++重點複習.
資料結構實習-六.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
:Count the Trees ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10007:Count the Trees 解題者:楊家豪 解題日期: 2006 年 3 月 題意: 給 n 個點, 每一個點有自己的 Label,
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
物件導向實習 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average.
Java Tutorial /10/21. Java Resource Java SDK –Download from –Install Jdk windows-i586.exe –
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
> Polymorphism. Polymorphism - 2 多型的功能 多型提供了「介面與實作分離」的一個重 要性,能將 what (是什麼)自 how (怎麼 做)之中抽離。 多型( polymorphism )除去型別之間的耦 合關係。
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Presentation transcript:

Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods

Methods F Introducing Methods –Methods ( 方法 ) 的宣告 F Passing Parameters – 代入參數 – pass by value ( 代入數值 ) F Overloading Methods –Method 的重載 F Scope of Local Variables F Method Abstraction

Method Structure : Declaring Methods ( 方法的宣告 ) public static int max( int num1, int num2 ) { int result = 0; if (num1 > num2) result = num1; else result = num2; return result ; } Modifiers Return value typeMethod nameParameters Return value Method body

使用 Methods 的好處 寫好之後可以在不同的程式中重複使用. Information hiding -- Hide the implementation from the user (Method 的使用者不需知道這個 method 是怎麼寫 的,只要知道怎麼用就行 : method 的功能、需代入的參數 、 return 的值 ). 減低程式的複雜度 – 程式分隔成數個 methods ,容易整理, 容易維護,也比較容易理解 Squares1.javaSquares1.java Square2.java Square3.javaSquare2.javaSquare3.java

Squares 1 // Squares1: without using methods public class Squares1 { public static void main( String[ ] args ) { for (int index = 1; index <= 10; index ++) { System.out.println("The square of " + index + " is " + ( index*index )); } System.exit(0); } // end of main } // end of class squares

Squares 2 /* Squares2 Using methods with a return value.*/ public class Squares2 { public static void main( String[] args ) { for (int index = 1; index <= 10; index ++) { System.out.println("The square of " + index + " is " + ( square( index ))); System.out.println("The sum of " + index + "+2 squared is " + ( square( index+ 2))); System.out.println("The sum of " + index + " squared+ 2 is " + (( square( index))+2) ); System.out.println(); } System.exit(0); } // end of main

// square method declaration follows: public static int square( int x ) { int y = x * x; return y; } // end of method square } // end of class squares2

Spuares 3 /* Squares3 This program demonstrates the use of methods with a return value and two inputs.*/ public class Squares3 { public static void main( String[] args ) { for (int index = 1; index <= 10; index ++) { System.out.println(index + " The sum of squares of "+ index + " & " + (index+1) + " is " + square( index, index+1 )); } System.exit(0); } // end of main

// square method declaration follows: public static int square( int x, int y ) { int a, b; a = x * x; b = y * y; return (a+b); } // end of method square } // end of class squares3

Pass by Value // TestPassByValue.java: Demonstrate passing values to methods public class TestPassByValue { /** Main method */ public static void main(String[] args) { // Declare and initialize variables int num1 = 1; int num2 = 2; System.out.println("Before invoking the swap method, num1 is " + num1 + " and num2 is " + num2); // Invoke the swap method to attempt to swap two variables swap(num1, num2); System.out.println("After invoking the swap method, num1 is " + num1 + " and num2 is " + num2); }

Pass by Value, Cont. /** Swap two variables */ public static void swap(int n1, int n2) { System.out.println(" Inside the swap method"); System.out.println(" Before swapping n1 is " + n1 + " n2 is " + n2); // Swapping n1 with n2 int temp = n1; n1 = n2; n2 = temp; System.out.println(" After swapping n1 is " + n1 + " n2 is " + n2); }

Method overloading ( 重載 ) // TestMethodOverloading.java: Demonstrate method overloading public class TestMethodOverloading { public static void main(String[] args) { // 代入兩個 int 的 max method System.out.println("The maximum between 3 and 4 is " + max(3, 4)); // 代入兩個 double 的 max method System.out.println("The maximum between 3.0 and 5.4 is " + max(3.0, 5.4)); // 代入三個 double 的 max method System.out.println("The maximum between 3.0, 5.4, and is " + max(3.0, 5.4, 10.14)); }

public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, double num2, double num3) { return max(max(num1, num2), num3); }

Scope of Local Variables A local variable : 宣告在 method 中的變數. Scope : 變數的 scope 指的是該變數在整個程式中可被參照到 的部分 ( 可使用到這個變數的區域 ). 一個 local variable 的 scope 從這個變數的宣告開始,一直到包 含這個變數的 block 結束為止 同一名稱的變數可宣告在不互相包含於的 blocks 中,但不可在 ”nested blocks” 中宣告兩次

Scope of Local Variables, cont. // Fine with no errors public static void correctMethod() { int x = 1; int y = 1; // i is declared for (int i = 1; i < 10; i++) { x += i; } // i is declared again for (int i = 1; i < 10; i++) { y += i; }

Scope of Local Variables, cont. // With compilation errors public static void incorrectMethod() { int x = 1; int y = 1; for (int i = 1; i < 10; i++) { int x = 0; x += i; }

Method Abstraction You can think of the method body as a black box that contains the detailed implementation for the method.