1 / 20

Methods

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

jaimin
Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Methods CSCI 1301

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

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

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

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

  6. 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(); } }

  7. 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!”); }

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

  9. 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 }

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

  11. 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!”); }

  12. 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); }

  13. 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; }

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

  15. 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); }

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

  17. Terminology Review Values that are sent into a method. • arguments • modifiers • return type • parameters

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

  19. Terminology Review A special variable that holds a value being passed into a method. • argument • modifier • return type • parameter

  20. Terminology Review A variable declared inside a method and is not accessible to statements outside the method. • argument • local variable • lifetime variable • final variable

More Related