Đang tải... (xem toàn văn)
Chapter 5 Chapter 5 Methods 1 2 Contents Introduction to Methods Passing Arguments to a Method More about Local Variables Returning a Value from a Method Problem Solving with Methods 2 3 1 Introductio.
Chapter Methods Contents Introduction to Methods Passing Arguments to a Method More about Local Variables Returning a Value from a Method Problem Solving with Methods Introduction to Methods A method is a collection of statements that performs a specific task We have experienced methods in two ways: Creating a method named main in every program Using predefined methods from the Java API, such as System.out.println Integer.parseInt Math.pow Introduction to Methods Methods are commonly used to break a problem into small manageable pieces Instead of writing one long method that contains all of the statements, several small methods that each solve a specific part of the problem can be written This approach is called divide and conquer because a large problem is divided into several problems Introduction to Methods public class BigProblem { public static void main(String[] args) { statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; statement; } } public class DividedProblem { public static void main(String[] args) { statement; statement; statement; } public static void method2() { statement; statement; statement; } public static void method3() { statement; statement; statement; } public static void method4() { statement; Introduction to Methods Another reason to write methods is that they simplify programs If a specific task is performed in several places in a program, a method can be written once and then be executed anytime it is needed This benefit of using methods is known as code reuse void Methods and ValueReturning Methods Two general categories of methods: void methods Value-returning methods A void method performs a task and then This method displays the value on terminates the screen, and then terminates int number = 7; System.out.println(number); number = 0; void Methods and ValueReturning Methods A value-returning methods not only performs a task, but also sends a value back to the code that called it int number; String str = “700”; number = Integer.parseInt(str); This method converts the string to a number, and then returns the number to this line of code Defining a void Method To create a method we must write its definition, which consists of two general parts: A header Appears at the beginning of a method definition Header public static void main(String[] args) Lists several important things about the method { Body Method Modifiers} System.out.println(“Hello World!”); Return type Method's name Arguments Defining a void Method Return Type Method Modifiers Method Name Parentheses public static void displayMessage () { The method header is never te System.out.println(“Hello from the displayMessage method.”); } Method Modifiers: - public: The method is publicly available to code outside the class - static: The method belongs to the class, not a specific object Return Type: - void: The method is a void method, and does not return a value Method Name: A descriptive name that can describes the function of the method Parentheses: - The method name is always followed by a set of parentheses 10 Using the @return Tag in Documentation Comments Using a @return tag to provide a description of the return value when writing the documentation comments The general format of a @return tag comment @return Description Description is a description of the return value The @return tag must appear after the general description of the method The description can span several lines It ends at65 the end of the documentation comment (*/ Returning a boolean Value public static boolean isValid(int number) { boolean status; if(number >= && number