Methods
This presentation is the property of its rightful owner.
Sponsored Links
1 / 20

Methods PowerPoint PPT Presentation


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

Methods. CSCI 1301. What is a method?. A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library System.out. println Integer. parseInt Math. pow User Defined methods: you create your own

Download Presentation

Methods

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

Methods

CSCI 1301


What is a method

What is a method?

  • A method is a collection of statements that performs a specific task.

  • Pre-Defined methods: available in Java library

    • System.out.println

    • Integer.parseInt

    • Math.pow

  • User Defined methods: you create your own

    • main is a method you have created in every program


Divide and conquer

Divide and Conquer

  • Methods break a complex program into small manageable pieces.

  • Write several small methods which each solve a specific part of the problem.


Methods

printLetters();

printNumbers();

call statements

public static void printLetters()

{ System.out.println ("Part 1: Letters");

System.out.println ("A");

System.out.println ("B");

System.out.println ("C");

}

public static void printNumbers()

{ System.out.println ("Part 2: Numbers");

System.out.println ("1");

System.out.println ("2");

System.out.println ("3");

}

method definitions

public class DividedProblem

{

public static void main (String [] args)

{

}

}

public class BigProblem

{

public static void main (String [] args)

{

}

}

System.out.println ("Part 1: Letters");

System.out.println ("A");

System.out.println ("B");

System.out.println ("C");

printAnimals();

System.out.println ("Part 2: Numbers");

System.out.println ("1");

System.out.println ("2");

System.out.println ("3");

System.out.println ("Part 3: Animals");

System.out.println ("Dog");

System.out.println ("Cat");

System.out.println ("Bird");

public static void printAnimals()

{ System.out.println ("Part 3: Animals");

System.out.println ("Dog");

System.out.println ("Cat");

System.out.println ("Bird");

}

Using Methods requires:

1. Defining a method

2. Calling a method


Save time by reusing code

Save Time by Reusing Code

  • Methods enable code reuse.

  • Once a method has been defined it can be called repeatedly.


Methods

Predefined method

Math is a class in the Java library. It contains several methods including pow.

pow(double a, double b) - Returns the value of the first argument raised to the power of the second argument.

User Defined method

The display method is called 3 times.

Output of Program

------------------

Result: 61.0

------------------

------------------

Result: 45.0

------------------

------------------

Result: 48.6

------------------

public class CodeReuse

{

public static void main (String [] args)

{

double x, y, z;

x = Math.pow(5, 2) + Math.pow(6,2);

y = Math.pow (3, 2) * 5;

z = (9.0/5.0) * Math.pow (3, 3);

display(x);

display(y);

display(z);

}

public static void display(double a)

{ System.out.println("------------------");

System.out.println ("Result: "+ a );

System.out.println("------------------");

System.out.println();

}

}


Defining methods

method name

parameters

header

body

Defining Methods

  • A method definition consists of a header and a body

    • header: contains name of method and lists parameters

    • body: statements enclosed within a set of curly braces

public static void main (String [] args)

{

System.out.println(“Hello World!”);

}


Parts of a method header

Parts of a Method Header

  • Method modifiers – public and static are used to make a method available for use by other classes yet belong to the class in which it is defined.

  • Return type – void means the method does not return a value. Otherwise specify the type of data the method returns (e.g., int, double … etc.)

  • Method name –use a descriptive name. Use the same naming restrictions as variable names.

  • Parameters enclose within parentheses allow data to be passed to the method. If the method does not receive any arguments, the parentheses are empty.

public static voiddisplayMessage( )

public static voidshowPay(int hours, double rate)

public static doublesum(double a, double b)

NOTICE: Do NOT use a semicolon to end a method header


More about parameters

More about parameters

  • Values that are passed into a method are called arguments, and the variables that receive those values are called parameters.

arguments

Math.pow(5, 2)

parameters

public static double pow (doubleb, doublex)

{

//code to compute and return bx  52

}


More about parameters1

More about parameters

  • The parameter list defines temporary variables that will hold the values passed to the method. These variables will be destroyed when the method ends.

  • The order of parameters must match the order of the arguments

    • Math.pow(5, 2) computes 52 but Math.pow(2, 5) computes 25

Math.pow(5, 2)

arguments

public static double pow (doubleb, doublex)

{

//code to compute and return bx  52

}

parameters


Writing the method body

Writing the Method Body

Include the statements to execute within the curly braces.

public static void displayMessage( )

{

System.out.print(“Hello”);

System.out.println(“ World!”);

}


Writing the method body1

Writing the Method Body

Create local variables, if necessary, to temporarily hold data. Like parameters, these local variables will be destroyed when the method ends.

public static void showPay (int hours, double rate)

{

doublepay; //local variable

pay = hours * rate;

System.out.println(“ Pay: $” + pay);

}


Writing the method body2

Writing the Method Body

If there is a return type, use a return statement to return a value of the same data type.

public static double sum (double a, double b)

{

double total; //local variable

total = a + b;

return total;

}


Calling methods

Calling Methods

  • Parentheses are still need, even if the method does not have parameters

public static void main (String [] args )

{

System.out.println(“Goodbye”);

}

displayMessage();

call statement

public static void displayMessage( )

{

System.out.print(“Hello”);

System.out.println(“ World!”);

}

method definition


Calling methods1

Calling Methods

  • Do not include data types when passing arguments

public static void main (String [] args)

{

int hoursWorked = 40;

double payRate = 16.50;

}

valid call statement

showPay(hoursWorked, payRate);

showPay(inthoursWorked, double payRate);

ERROR!!

public static void showPay (int hours, double rate)

{ double pay; //local variable

pay = hours * rate;

System.out.println(“ Pay: $” + pay);

}


Calling methods2

Calling Methods

  • User defined methods can call other methods

  • The value returned by a method can be used within a programming statement.

public static doublesample (double a, double b)

{

double total; //local variable

total = +

return total;

}

displayMessage();

sum(a,b)

Math.pow(a,b);


Terminology review

Terminology Review

Values that are sent into a method.

  • arguments

  • modifiers

  • return type

  • parameters


Terminology review1

Terminology Review

  Only a copy of an argument’s value is passed into a parameter value.

  • pass by reference

  • pass by value

  • mutator method

  • void method


Terminology review2

Terminology Review

A special variable that holds a value being passed into a method.

  • argument

  • modifier

  • return type

  • parameter


Terminology review3

Terminology Review

A variable declared inside a method and is not accessible to statements outside the method.

  • argument

  • local variable

  • lifetime variable

  • final variable


  • Login