Methods and parameters in java
This presentation is the property of its rightful owner.
Sponsored Links
1 / 19

Methods and Parameters in Java PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on
  • Presentation posted in: General

Methods and Parameters in Java. 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

Download Presentation

Methods and Parameters in Java

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Methods and parameters in java

Methods and Parametersin Java


Methods in java

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


User defined methods

User-defined methods

  • Defined by Java

    • System.out.println()

    • Math.sqrt()

  • Defined by users/programmers


Basic anatomy of a method

Basic anatomy of a method

Return type

Name

Parameters (if any)

Instructions

… intfindMax(int[] scores)

{

// instructions

}


Basic syntax of methods

Basic syntax of methods

… returnTypename(parameters)

{

// instructions

}

For simplicity for this workshop,

… is public static


Return type of a method

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


Name of a method

Name of a Method

Same rules as naming a variable…


Parameters of a method

Parameters of a Method

  • Type and name

    • E.g. intfindMax(int[] scores)

  • More than one parameter

    • E.g. intfindMin(int x, int y)


Instructions body of a method

Instructions (Body) of a Method

Instructions as before in the main method

You have been mostly writing instructions for the main method


Using calling a method

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()


Using calling a method parameter p assing

Using/Calling a Method—Parameter Passing

  • If a method has parameters

    • The parameter types have to match when the method is called

  • Definition

    • … intfindMin(int x, int y)

  • Call

    • int a, b. m;

    • m = findMin(a, b);

    • m = findMin(a, 10);

    • m = findMin(a + 10, b);


Summary

Summary

  • Defining a method

    • Return type

    • Name

    • Parameters

    • Instructions (body)

  • Calling a method

    • Parameter types need to match


Problem decomposition for tic tac toe

Problem decomposition for Tic-tac-toe

  • Display the board

  • Make a move

    • Ask for a move

    • Update the board

  • Decide winner

    • Check 3 rows

    • Check 3 columns

    • Check 2 diagonals

      Tasks 1 to 3 can be implemented as methods

      Subtasks 2a-2b, 3a-2c can be as well


Defining methods in tic tac toe

Defining Methods in Tic-tac-toe

  • Display the Board

    • Return type ?

    • Parameters ?

  • Make a move

    • Return type ?

    • Parameters ?

  • Decide Winner

    • Return type ?

    • Parameters ?


One possibility

One Possibility

  • Display the Board

    • Return type -- void

    • Parameters -- board

  • Make a move

    • Return type -- void

    • Parameters -- board, player

  • Decide Winner

    • Return type -- char (X , O, space--no winner)

    • Parameters – board


Main method first level in decomposition

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);

}


Main method refining it

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

}


Main method high level readable

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

}


Two more methods

Two more methods

  • switchPlayer(player)

  • 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


  • Login