Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 Methods CSCI 1301

3 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

4 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.

5 public class BigProblem { public static void main (String [] args) { } public class DividedProblem { public static void main (String [] args) { } public static void printAnimals() { System.out.println ("Part 3: Animals"); System.out.println ("Dog"); System.out.println ("Cat"); System.out.println ("Bird"); } printAnimals(); public static void printNumbers() { System.out.println ("Part 2: Numbers"); System.out.println ("1"); System.out.println ("2"); System.out.println ("3"); } printNumbers(); public static void printLetters() { System.out.println ("Part 1: Letters"); System.out.println ("A"); System.out.println ("B"); System.out.println ("C"); } printLetters(); System.out.println ("Part 1: Letters"); System.out.println ("A"); System.out.println ("B"); System.out.println ("C"); 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"); Using Methods requires: 1. Defining a method 2. Calling a method method definitions call statements

6 Save Time by Reusing Code Methods enable code reuse. Once a method has been defined it can be called repeatedly.

7 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(); } Math is a class in the Java library. It contains several methods including pow. powpow(double a, double b) - Returns the value of the first argument raised to the power of the second argument. Predefined methodUser Defined method The display method is called 3 times. ------------------ Result: 61.0 ------------------ Result: 45.0 ------------------ Result: 48.6 ------------------ Output of Program

8 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!”); } header body method nameparameters

9 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 double sum (double a, double b) public static void displayMessage( ) public static void showPay (int hours, double rate) NOTICE: Do NOT use a semicolon to end a method header

10 More about parameters Values that are passed into a method are called arguments, and the variables that receive those values are called parameters. Math.pow(5, 2) public static double pow (double b, double x) { //code to compute and return b x  5 2 } arguments parameters

11 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 5 2 but Math.pow(2, 5) computes 2 5 Math.pow(5, 2) public static double pow (double b, double x) { //code to compute and return b x  5 2 } arguments parameters

12 Writing the Method Body Include the statements to execute within the curly braces. { System.out.print(“Hello”); System.out.println(“ World!”); } public static void displayMessage( )

13 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. { double pay; //local variable pay = hours * rate; System.out.println(“ Pay: $” + pay); } public static void showPay (int hours, double rate)

14 Writing the Method Body If there is a return type, use a return statement to return a value of the same data type. { double total; //local variable total = a + b; return total; } public static double sum (double a, double b)

15 Calling Methods Parentheses are still need, even if the method does not have parameters public static void displayMessage( ) { System.out.print(“Hello”); System.out.println(“ World!”); } public static void main (String [] args ) { System.out.println(“Goodbye”); } call statement displayMessage(); method definition

16 Calling Methods Do not include data types when passing arguments public static void showPay (int hours, double rate) { double pay; //local variable pay = hours * rate; System.out.println(“ Pay: $” + pay); } public static void main (String [] args) { int hoursWorked = 40; double payRate = 16.50; } showPay(hoursWorked, payRate); showPay(int hoursWorked, double payRate); valid call statement ERROR!!

17 Calling Methods User defined methods can call other methods The value returned by a method can be used within a programming statement. public static double sample (double a, double b) { double total; //local variable total = + return total; } displayMessage(); sum(a,b)Math.pow(a,b);

18 Terminology Review Values that are sent into a method. a)arguments b)modifiers c)return type d)parameters

19 Terminology Review Only a copy of an argument’s value is passed into a parameter value. a)pass by reference b)pass by value c)mutator method d)void method

20 Terminology Review A special variable that holds a value being passed into a method. a)argument b)modifier c)return type d)parameter

21 Terminology Review A variable declared inside a method and is not accessible to statements outside the method. a)argument b)local variable c)lifetime variable d)final variable


Download ppt "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."

Similar presentations


Ads by Google