Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All rights reserved.
Reading Quiz 1. In Java a named block of code that accomplishes a specific task is called a: a) Function b) Subroutine c) Loop d) Method 2. A method is one that does not return a value when finished. a) boolean b) String c) void d) class
Chapter Topics Chapter 5 discusses the following main topics: Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods 5-3
What is a Method? A named group of Java commands.
Using Methods to Build A Shakespearean Insult Generator Insult me! How it works
PseudoCode for Insult Generator 1. Clear the screen 2. Display Shakespearean Insult Generator 3. Begin the insult with Thou 4. Add adjectives 5. Add noun 6. Add insult ending
clearscreen() Method call Method header Method body Method declaration
The Method Call Call public static void main(string[] args) clearscreen(); 5-8
Two Parts of Method Declaration Header public static void clearscreen() System.out.println( \f"); Body 5-9
What is the output of this program? public class Checkpoint public static void main(string[] args) method1(); method1(); method2(); What is the output? public static void method1() System.out.println("abra"); public static void method2() System.out.print("cad"); method1(); Circle method headers. Box method calls
What is the output of this program? public class Checkpoint public static void main(string[] args) method1(); method1(); method2(); circle method headers box method calls public static void method1() System.out.println("abra"); public static void method2() System.out.print("cad"); method1(); output: abra abra cadabra
Let s write some methods begininsult() adjective() - use if-else-if loop in main() for many insults 2 adjectives noun() - use switch endinsult()
Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer. Methods simplify programs. If a specific task is performed in several places in the program, a method can be written once to perform that task, and then be executed anytime it is needed. This is known as code reuse. 5-13
Better and Deeper generateinsults() generateinsults(500) Arguments and parameters Data types must match Can pass many arguments like Math.pow() Pass by reference; scope
Checkpoint
Checkpoint argument is given to method call parameter receives argument in method body
99 1.5 99 1.5 0 0 99 1.5
User Input and Passing Data Ask user for # of insults. getnuminsults() Returns an int
Methods we know already. Do they receive and/or return data? int i = keyboard.nextint(); System.out.println( Hello World ); String upper = name.touppercase(); clearscreen(); System.out.println( Math.sqrt(49) );
Defining a Value-Returning Method public static int sum(int num1, int num2) int result; result = num1 + num2; return result; This expression must be of the same data type as the return type Return type The return statement causes the method to end execution and it returns a value back to the statement that called the method. 5-21
Calling a Value-Returning Method total = sum(value1, value2); 60 20 40 public static int sum(int num1, int num2) int result; result = num1 + num2; return result; 5-22
Checkpoint is it void or value-returning? 5.3b Is the following line of code a method header or method call? void or value-returning?
Checkpoint a void method has no return value when invoked it appears on a line by itself a value returning method comes back with a value when complete must appear in an assignment statement or output line. is it void or value-returning? 5.3b Is the following line of code a method header or method call? void or value-returning?
What data types can be passed or returned? Any: int, double, boolean, String, Any primitive or object Parameter type must match Argument type. Return type must match the use in the call.
Returning a Reference to a String Object customername = fullname("john", "Martin"); address public static String fullname(string first, String last) John Martin See example: String name; name = first + " " + last; return name; ReturnString.java Local variable name holds the reference to the object. The return statement sends a copy of the reference back to the call statement and it is stored in customername. 5-26
String Returning Method Example public class ReturnString public static void main(string[] args) String customername; customername = fullname("john", "Martin"); System.out.println(customerName); public static String fullname(string first, String last) String name; name = first + " " + last; return name;
Checkpoint
Checkpoint public static int days( int years, int months, int weeks) public static double distance(double rate, double time)