Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods and Parameters in Java. Methods in Java Also known as  functions, procedures, subroutines For example  System.out.println()  Math.sqrt() Provide.

Similar presentations


Presentation on theme: "Methods and Parameters in Java. Methods in Java Also known as  functions, procedures, subroutines For example  System.out.println()  Math.sqrt() Provide."— Presentation transcript:

1 Methods and Parameters in Java

2 Methods in Java Also known as  functions, procedures, subroutines For example  System.out.println()  Math.sqrt() Provide a name for a sequence of instructions  Provide abstraction and hide the underlying details Can be used by many programs One of the ways to help implement decomposition

3 User-defined methods Defined by Java  System.out.println()  Math.sqrt() Defined by users/programmers

4 Basic anatomy of a method 1. Return type 2. Name 3. Parameters (if any) 4. Instructions … int findMax(int[] scores) { // instructions }

5 Basic syntax of methods … returnType name ( parameters ) { // instructions } For simplicity for this workshop, … is public static

6 Return Type of a Method Any data type  int, double, boolean, char, …  The method returns a value of that type  ie, a function No return value  void  ie, a procedure

7 Name of a Method Same rules as naming a variable…

8 Parameters of a Method Type and name  E.g. int findMax(int[] scores) More than one parameter  E.g. int findMin(int x, int y)

9 Instructions (Body) of a Method Instructions as before in the main method You have been mostly writing instructions for the main method

10 Using/Calling a Method Once a method is defined  It can be used or “called” For example,  You have been calling the method System.out.println()

11 Using/Calling a Method—Parameter Passing If a method has parameters  The parameter types have to match when the method is called Definition  … int findMin(int x, int y) Call  int a, b. m;  m = findMin(a, b);  m = findMin(a, 10);  m = findMin(a + 10, b);

12 Summary Defining a method  Return type  Name  Parameters  Instructions (body) Calling a method  Parameter types need to match

13 Problem decomposition for Tic-tac-toe 1. Display the board 2. Make a move a. Ask for a move b. Update the board 3. Decide winner a. Check 3 rows b. Check 3 columns c. Check 2 diagonals Tasks 1 to 3 can be implemented as methods Subtasks 2a-2b, 3a-2c can be as well

14 Defining Methods in Tic-tac-toe 1. Display the Board  Return type ?  Parameters ? 2. Make a move  Return type ?  Parameters ? 3. Decide Winner  Return type ?  Parameters ?

15 One Possibility 1. Display the Board  Return type -- void  Parameters -- board 2. Make a move  Return type -- void  Parameters -- board, player 3. Decide Winner  Return type -- char ( X, O, space--no winner)  Parameters – board

16 Main method—first level in decomposition … main(…) { char[][] board = new char[3][3]; char player = ’X’, winner = ’ ’; // initialize the board … displayBoard(board); makeAMove(board, player); winner = decideWinner(board); }

17 Main method—Refining it … main(…) { char[][] board = new char[3][3]; char player = ’X’, winner = ’ ’; // initialize the board … do { displayBoard(board); makeAMove(board, player); player = switchPlayer(player); winner = decideWinner(board); } while (…); // no winner and board is not full }

18 Main method (high-level & readable) … main(…) { char[][] board = new char[3][3]; char player = ’X’, winner = ’ ’; // initialize the board … do { displayBoard(board); makeAMove(board, player); player = switchPlayer(player); winner = decideWinner(board); } while (winner == ’ ’ && !isFull(board)); // no winner and board is not full }

19 Two more methods 1. switchPlayer(player) 2. isFull(board) Note that: We don’t have to know exactly how to implement the methods  we just need to know we need them  we can further decompose them to solve them It’s ok to add methods  we might not cover everything the first try


Download ppt "Methods and Parameters in Java. Methods in Java Also known as  functions, procedures, subroutines For example  System.out.println()  Math.sqrt() Provide."

Similar presentations


Ads by Google