User Defined Methods - Lecture Slides | CS 110, Study notes of Computer Science

Material Type: Notes; Professor: Adjeroh; Class: Introduction-Computer Science; Subject: Computer Science; University: West Virginia University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/31/2009

koofers-user-xc6
koofers-user-xc6 🇺🇸

9 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Java Programming: From Problem Analysis to Program Design, Second Edition 1
Chapter 7: User-Defined Methods
Methods in Java programming.
Standard (predefined) methods.
User-defined methods.
Value-returning and void methods
Scope of an identifier.
Method overloading
Java Programming: From Problem Analysis to Program Design, Second Edition 2
Predefined Classes
Methods already written and provided by
Java.
Organized as a collection of classes (class
libraries).
To use, import package.
Method type: The data type of the value
returned by the method.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download User Defined Methods - Lecture Slides | CS 110 and more Study notes Computer Science in PDF only on Docsity!

Java Programming: From Problem Analysis to Program Design, Second Edition 1

Chapter 7: User-Defined Methods

Œ Methods in Java programming.

Œ Standard (predefined) methods.

Œ User-defined methods.

Œ Value-returning and void methods

Œ Scope of an identifier.

Œ Method overloading

Java Programming: From Problem Analysis to Program Design, Second Edition 2

Predefined Classes

Œ Methods already written and provided by

Java.

Œ Organized as a collection of classes (class

libraries).

Œ To use, import package.

Œ Method type: The data type of the value

returned by the method.

Java Programming: From Problem Analysis to Program Design, Second Edition 3

Predefined Classes

Java Programming: From Problem Analysis to Program Design, Second Edition 4

Predefined Classes

Java Programming: From Problem Analysis to Program Design, Second Edition 7

class Character (Package: java.lang)

Java Programming: From Problem Analysis to Program Design, Second Edition 8

Syntax of Value-Returning Method

modifier(s) returnType methodName (formal parameter list) { statements }

Java Programming: From Problem Analysis to Program Design, Second Edition 9

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.

Java Programming: From Problem Analysis to Program Design, Second Edition 10

Syntax

Œ Syntax of formal parameter list:

dataType identifier, dataType identifier,...

Œ Syntax to call a value-returning method:

methodName(actual parameter list)

Java Programming: From Problem Analysis to Program Design, Second Edition 13

Equivalent Method Definitions

public static double larger(double x, double y) { if (x >= y) return x; else return y; }

Java Programming: From Problem Analysis to Program Design, Second Edition 14

Equivalent Method Definitions

public static double larger(double x, double y) { if (x >= y) return x;

return y; }

Java Programming: From Problem Analysis to Program Design, Second Edition 15

Programming Example:

Palindrome Number

Œ Palindrome: An integer or string that reads

the same forwards and backwards.

Œ Input: Integer or string.

Œ Output: Boolean message indicating whether

integer string is a palindrome.

Java Programming: From Problem Analysis to Program Design, Second Edition 16

Solution: isPalindrome Method

public static boolean isPalindrome(String str) { int len = str.length(); int i, j; j = len - 1; for (i = 0; i <= (len - 1) / 2; i++) { if (str.charAt(i) != str.charAt(j)) return false; j--; } return true; }

Java Programming: From Problem Analysis to Program Design, Second Edition 19

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 the method call statement, specify only actual parameters, not data type or method type. Œ Control goes back to caller when method exits.

Java Programming: From Problem Analysis to Program Design, Second Edition 20

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.

Java Programming: From Problem Analysis to Program Design, Second Edition 21

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 < 10; count++) { num = console.nextDouble(); max = larger(max, num); } System.out.println("The largest number is "

  • max); }

Java Programming: From Problem Analysis to Program Design, Second Edition 22

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.

Java Programming: From Problem Analysis to Program Design, Second Edition 25

Void Methods with Parameters: Syntax

Method definition: The definition of a void method with parameters has the following syntax: modifier(s) void methodName (formal parameter list) { statements } Formal parameter list: The formal parameter list has the following syntax: dataType variable, dataType variable, ...

Java Programming: From Problem Analysis to Program Design, Second Edition 26

Void Methods with Parameters:

Syntax

Method call: The method call has the following syntax: methodName(actual parameter list);

Actual parameter list: The actual parameter list has the following syntax: expression or variable, expression or variable, ...

Java Programming: From Problem Analysis to Program Design, Second Edition 27

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.

Java Programming: From Problem Analysis to Program Design, Second Edition 28

Reference Variables as Parameters

If a formal parameter is a reference variable:

Œ Copies value of corresponding actual parameter. Œ Value of actual parameter is address of the object where actual data is stored. Œ Both formal and actual parameters refer to same object.

Java Programming: From Problem Analysis to Program Design, Second Edition 31

Scope of an Identifier within a Class Œ Local identifier: An identifier that is declared within a method or block and that is visible only within that method or block. Œ Java does not allow the nesting of methods. That is, you cannot include the definition of one method in the body of another method. Œ Within a method or a block, an identifier must be declared before it can be used. Note that a block is a set of statements enclosed within braces. Œ A method’s definition can contain several blocks. The body of a loop or an if statement also forms a block. Œ Within a class, outside of every method definition (and block), an identifier can be declared anywhere.

Java Programming: From Problem Analysis to Program Design, Second Edition 32

Scope of an Identifier within a Class

Œ Within a method, an identifier that is 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 following method definition, the second declaration of the variable x is illegal:

public static void illegalIdentifierDeclaration() { int x; //block { double x; //illegal declaration, //x is already declared ... } }

Java Programming: From Problem Analysis to Program Design, Second Edition 33

Scope Rules

Œ Scope rules of an identifier that is declared within a class and accessed within a method (block) of the class. Œ An identifier, say X, that is 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. Œ Suppose X is an identifier that is declared within a class and outside of every method’s 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.

Java Programming: From Problem Analysis to Program Design, Second Edition 34

Example 7- 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) { //... }

Scope Rules

Java Programming: From Problem Analysis to Program Design, Second Edition 37

Method Overloading: An

Introduction

Œ Method overloading: More than one method can have the same name. Œ Two methods are said to have different formal parameter lists : Œ If both methods have a different number of formal parameters. Œ If the number of formal parameters is the same in both methods, the data type of the formal parameters in the order you list must differ in at least one position.

Java Programming: From Problem Analysis to Program Design, Second Edition 38

Method Overloading

public void methodOne(int x) public void methodTwo(int x, double y) public void methodThree(double y, int x) public int methodFour(char ch, int x, double y) public int methodFive(char ch, int x, String name)

These methods all have different formal parameter lists.

Java Programming: From Problem Analysis to Program Design, Second Edition 39

Method Overloading

public void methodSix(int x, double y, char ch) public void methodSeven(int one, double u, char firstCh)

Œ The methods methodSix and methodSeven both have three formal parameters, and the data type of the corresponding parameters is the same.

Œ These methods all have the same formal parameter lists.

Java Programming: From Problem Analysis to Program Design, Second Edition 40

Method Overloading

Œ Method overloading: Creating several methods within a class with the same name.

Œ The signature of a method consists of the method name and its formal parameter list. Two methods have different signatures if they have either different names or different formal parameter lists. (Note that the signature of a method does not include the return type of the method.)