Presentation is loading. Please wait.

Presentation is loading. Please wait.

Starting Out with Java: From Control Structures through Objects

Similar presentations


Presentation on theme: "Starting Out with Java: From Control Structures through Objects"— Presentation transcript:

1 Starting Out with Java: From Control Structures through Objects
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis

2 Reading Quiz In Java a named block of code that accomplishes a specific task is called a: a) Function b) Subroutine c) Loop d) Method A _______ method is one that does not return a value when finished. a) boolean b) String c) void d) class

3 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

4 What is a Method? A named group of Java commands.

5 A Shakespearean Insult Generator
Using Methods to Build A Shakespearean Insult Generator Insult me! How it works

6 PseudoCode for Insult Generator
Clear the screen Display “Shakespearean Insult Generator” Begin the insult with Thou Add adjectives Add noun Add insult ending

7 clearScreen() Method call Method header Method body Method declaration

8 The Method Call Call public static void main(String[] args) {
clearScreen(); }

9 Two Parts of Method Declaration
Header public static void clearScreen() { System.out.println(“\f"); } Body

10 What is the output of this program?
public class Checkpoint { public static void main(String[] args) method1(); method2(); } public static void method1() System.out.println("abra"); public static void method2() System.out.print("cad"); What is the output? Circle method headers. Box method calls

11 What is the output of this program?
public class Checkpoint { public static void main(String[] args) method1(); method2(); } public static void method1() System.out.println("abra"); public static void method2() System.out.print("cad"); circle method headers box method calls output: abra cadabra

12 Let’s write some methods
beginInsult() adjective() - use if-else-if loop in main() for many insults 2 adjectives noun() use switch endInsult()

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

14 Better and Deeper generateInsults() generateInsults(500)
Arguments and parameters Data types must match Can pass many arguments like Math.pow() Pass by reference; scope

15 Checkpoint

16 Checkpoint argument is given to method call
parameter receives argument in method body

17

18

19 User Input and Passing Data
Ask user for # of insults. getNumInsults() Returns an int

20 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) );

21 Defining a Value-Returning Method
public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } Return type The return statement causes the method to end execution and it returns a value back to the statement that called the method. This expression must be of the same data type as the return type

22 Calling a Value-Returning Method
total = sum(value1, value2); public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } 40 20 60

23 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?

24 Checkpoint is it void or value-returning?
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?

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

26 Returning a Reference to a String Object
customerName = fullName("John", "Martin"); public static String fullName(String first, String last) { String name; name = first + " " + last; return name; } See example: ReturnString.java address 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. “John Martin”

27 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;

28 Checkpoint

29 Checkpoint public static int days( int years, int months, int weeks) public static double distance(double rate, double time)


Download ppt "Starting Out with Java: From Control Structures through Objects"

Similar presentations


Ads by Google