Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 - PowerPoint PPT Presentation

Loading...

PPT – Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 PowerPoint presentation | free to view - id: 1c20f7-ZDc1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Java Programming: From Problem Analysis to Program Design, 3e Chapter 7

Description:

Learn about standard (predefined) methods and discover how to use ... static double t; public static void main(String[] args) int num; double x, z; char ch; ... – PowerPoint PPT presentation

Number of Views:100
Avg rating:3.0/5.0
Slides: 34
Provided by: course248
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Java Programming: From Problem Analysis to Program Design, 3e Chapter 7


1
Java Programming From Problem Analysis to
Program Design, 3e Chapter 7
  • User-Defined Methods

2
Chapter Objectives
  • Understand how methods are used in Java
    programming
  • Learn about standard (predefined) methods and
    discover how to use them in a program
  • Learn about user-defined methods
  • Examine value-returning methods, including actual
    and formal parameters

3
Chapter Objectives (continued)
  • Explore how to construct and use a
    value-returning, user-defined method in a program
  • Learn how to construct and use user-defined void
    methods in a program
  • Explore variables as parameters
  • Learn about the scope of an identifier
  • Become aware of method overloading

4
Predefined Classes
  • Methods already written and provided by Java
  • Organized as a collection of classes (class
    libraries)
  • To use import package
  • Method type data type of value returned by method

5
Predefined Classes (continued)
6
Predefined Classes (continued)
7
Predefined Classes (continued)
8
Predefined Classes (continued)
9
class Character (Package java.lang)
10
class Character (Package java.lang) (continued)
11
class Character (Package java.lang)
12
User-Defined Methods
  • Value-returning methods
  • Used in expressions
  • Calculate and return a value
  • Can save value for later calculation or print
    value
  • modifiers public, private, protected, static,
    abstract, final
  • returnType type of the value that the method
    calculates and returns (using return statement)
  • methodName Java identifier name of method

13
Equivalent Method Definitions
public static double larger(double x, double
y) double max if (x gt y)
max x else max y return
max
14
Equivalent Method Definitions (continued)
  • public static double larger(double x, double y)
  • if (x gt y)
  • return x
  • else
  • return y

15
Equivalent Method Definitions (continued)
  • public static double larger(double x, double y)
  • if (x gt y)
  • return x
  • return y

16
Flow of Execution
  • Execution always begins with the first statement
    in the method main
  • User-defined methods execute only when called
  • Call to method transfers control from caller to
    called method
  • In method call statement, specify only actual
    parameters, not data type or method type
  • Control goes back to caller when method exits

17
Programming Example Largest Number
  • Input set of 10 numbers
  • Output largest of 10 numbers
  • Solution
  • Get numbers one at a time
  • Method largest number returns the larger of 2
    numbers
  • For loop calls method largest number on each
    number received and compares to current largest
    number

18
Solution Largest Number
static Scanner console new Scanner(System.in)
public static void main(String args)
double num double max int count
System.out.println("Enter 10 numbers.") num
console.nextDouble() max num
for (count 1 count lt 10 count)
num console.nextDouble() max
larger(max, num) System.out.println("
The largest number is "
max)
19
Sample Run Largest Number
  • Sample Run
  • Enter 10 numbers
  • 10.5 56.34 73.3 42 22 67 88.55 26 62 11
  • The largest number is 88.55

20
Void Methods
  • Similar in structure to value-returning methods
  • Call to method is always stand-alone statement
  • Can use return statement to exit method early

21
Void Methods Syntax
  • Method Definition
  • -The general form (syntax) of a void method
    without parameters is as follows
  • modifier(s) void methodName()
  • statements
  • Method Call (Within the Class)
  • -The method call has the following syntax
  • methodName()

22
Primitive Data Type Variables as Parameters
  • A formal parameter receives a copy of its
    corresponding actual parameter
  • If a formal parameter is a variable of a
    primitive data type
  • Value of actual parameter is directly stored
  • Cannot pass information outside the method
  • Provides only a one-way link between actual
    parameters and formal parameters

23
Scope of an Identifier Within a Class
  • Local identifier Identifier declared within a
    method or block, which is visible only within
    that method or block
  • Within a method or a block, an identifier must be
    declared before it can be used a block is a set
    of statements enclosed within braces

24
Scope of an Identifier Within a Class (continued)
  • A methods definition can contain several blocks
  • The body of a loop or an if statement also form a
    block
  • Within a class, outside of every method
    definition, (and every block), an identifier can
    be declared anywhere

25
Scope of an Identifier Within a Class (continued)
  • Within a method, an identifier used to name a
    variable in the outer block of the method cannot
    be used to name any other variable in an inner
    block of the method
  • For example, in the method definition on the next
    slide, the second declaration of the variable x
    is illegal

26
Scope of an Identifier Within a Class (continued)
  • public static void illegalIdentifierDeclaration()
  • int x
  • //block
  • double x //illegal declaration,
  • //x is already declared
  • ...

27
Scope Rules
  • Scope rules of an identifier declared within a
    class and accessed within a method (block) of the
    class
  • An identifier, say X, declared within a method
    (block) is accessible
  • Only within the block from the point at which it
    is declared until the end of the block
  • By those blocks that are nested within that block

28
Scope Rules (continued)
  • Suppose X is an identifier declared within a
    class and outside of every methods definition
    (block)
  • If X is declared without the reserved word static
    (such as a named constant or a method name), then
    it cannot be accessed in a static method
  • If X is declared with the reserved word static
    (such as a named constant or a method name), then
    it can be accessed within a method (block),
    provided the method (block) does not have any
    other identifier named X

29
Scope Rules (continued)
  • Example 7-12 public class ScopeRules
  • static final double rate 10.50
  • static int z
  • static double t
  • public static void main(String args)
  • int num
  • double x, z
  • char ch
  • //...
  • public static void one(int x, char y)
  • //...

30
Scope Rules (continued)
  • public static int w
  • public static void two(int one, int z)
  • char ch
  • int a
  • //block three
  • int x 12
  • //...
  • //end block three
  • //...

31
Scope Rules Demonstrated
32
Scope Rules Demonstrated (continued)
33
Chapter Summary
  • Predefined methods
  • User-defined methods
  • Value-returning methods
  • Void methods
  • Formal parameters
  • Actual parameters
  • Flow of Execution
  • Scope of an identifier within a class
About PowerShow.com