Download presentation
Presentation is loading. Please wait.
Published byPreston James Modified over 9 years ago
1
Methods
2
2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName( listOfParameters ) { sequenceOfStatements } modifier returnType methodName( listOfParameters ) { sequenceOfStatements } modifier: either “public”, “private” or “protected” returnType: a type name methodName: a valid identifier listOfParameters: a comma separated list of type declarations
3
3 Methods A method may produce (return) data If a method doesn’t produce data ▫the return type is “void” If a method does produce data ▫the type of the data is given by the return type ▫The last statement executed must be a “return”. Return statement ▫Syntax: return expression; ▫Semantics: The method is done. Value sent to the ‘caller’
4
4 Example Write a method that computes the largest of two integer numbers. public int max(int n1, int n2) { if(n1 > n2) { return n1; } else { return n2; } public int max(int n1, int n2) { if(n1 > n2) { return n1; } else { return n2; } I’m the max No, I’m the max
5
5 Example When the method is ‘called’ (i.e. when it is used) ▫The method is invoked by name ▫The ‘caller’ gives values for the formal parameters ▫The code body executes ▫The returned value (if any) is handed back to the caller. The returned value "is" the method call. public int max(int n1, int n2) { if(n1 > n2) { return n1; } else { return n2; } public int max(int n1, int n2) { if(n1 > n2) { return n1; } else { return n2; } int a = max(3, 5); int b = max(10, -1); int c = max(a, b); int d = max(a, max(b, c)); int a = max(3, 5); int b = max(10, -1); int c = max(a, b); int d = max(a, max(b, c));
6
6 Void and Non-Void methods A void method doesn’t return data – but it does something. ▫Use as a command A non-void method ‘is’ the data it returns. ▫Use as data; typically in the context of assignment int z = max(a,max(b,c));
7
7 Formal/Actual The method is defined by ▫Writing the body ▫Giving it a name ▫Defining the number and type of input (FORMAL PARAMETERS) ▫Defining the type of the output The method is used by ▫Referencing the name ▫Providing actual values to be used for the input (ACTUAL ARGUMENTS) ▫Making use of any output public int max(int n1, int n2) { if(n1 > n2) { return n1; } else { return n2; } public int max(int n1, int n2) { if(n1 > n2) { return n1; } else { return n2; } int a = max(3, 5); int b = max(10, -1); int c = max(a, b); int d = max(a, max(b, c)); int a = max(3, 5); int b = max(10, -1); int c = max(a, b); int d = max(a, max(b, c));
8
8 Methods and Classes A method is always associated with a class ▫A class is a collection of variables and methods ▫So far, our classes have contained exactly one method and no variables. public class ClassName { } Methods Declarations
9
9 Scope The SCOPE of a variable is the region of code in which the variable name can be used. Variables declared in a block ▫Scope begins at declaration ▫Scope ends at the end of the enclosing block Variables declared in a method ▫See above since method bodies are blocks Formal parameters ▫Scope begins at declaration ▫Scope ends at the end of the method Variables declared in a class ▫Scope extends throughout the class Variables are known as ▫LOCAL if declared in a method ▫INSTANCE variables if declared in a class
10
10 Scope Example public class SomeProgram { private int x; private int y; public static void main(String[] args) { int z = max(3, 12); … } public static int max(int a, int b) { … } public class SomeProgram { private int x; private int y; public static void main(String[] args) { int z = max(3, 12); … } public static int max(int a, int b) { … }
11
11 Method Example Write a method to compute the average of two integer numbers public double average(int n1, int n2) { return (n1 + n2) / 2.0; } public double average(int n1, int n2) { return (n1 + n2) / 2.0; } Write a method to compute whether a student passes a course. The syllabus states that a student passes if they receive at least a 60% average on 3 quizzes (10 points each) and a 65% average on three exams (100 points each). In addition, if any single exam is less than 35% they do not pass.
12
12 Consider…. Write a code fragment that reads information for a group of students and prints out for each one whether they pass or not. public boolean passes(int q1, int q2, int q3, int e1, int e2, int e3) { double quizAverage = (q1 + q2 + q3) / 30.0; double examAverage = (e1 + e2 + e3) / 300.0; int minExam = min(e1, min(e2, e3)); return quizAverage >=.6 && examAverage >=.65 && minExam >=.35; } public boolean passes(int q1, int q2, int q3, int e1, int e2, int e3) { double quizAverage = (q1 + q2 + q3) / 30.0; double examAverage = (e1 + e2 + e3) / 300.0; int minExam = min(e1, min(e2, e3)); return quizAverage >=.6 && examAverage >=.65 && minExam >=.35; } char c = Keyboard.readChar(); while(c != ‘Q’) { int q1 = Keyboard.readInt(); int q2 = Keyboard.readInt(); int q3 = Keyboard.readInt(); int e1 = Keyboard.readInt(); int e2 = Keyboard.readInt(); int e3 = Keyboard.readInt(); System.out.println(“passes?: “ + passes(q1,q2,q3,e1,e2,e3)); c = Keyboard.readChar(); } char c = Keyboard.readChar(); while(c != ‘Q’) { int q1 = Keyboard.readInt(); int q2 = Keyboard.readInt(); int q3 = Keyboard.readInt(); int e1 = Keyboard.readInt(); int e2 = Keyboard.readInt(); int e3 = Keyboard.readInt(); System.out.println(“passes?: “ + passes(q1,q2,q3,e1,e2,e3)); c = Keyboard.readChar(); }
13
13 Method Example Write a method to compute the amount of money (in cents) in a bank account at the end of some term. The starting balance is given (in cents) along with the annual interest rate (a double given in percent) and the number of years. public int getBalance(int balance, double rate, int yrs) { for(int yr=1; yr <= yrs; yr++){ balance += (int)((balance)*(1+rate/100)); } return balance; } public int getBalance(int balance, double rate, int yrs) { for(int yr=1; yr <= yrs; yr++){ balance += (int)((balance)*(1+rate/100)); } return balance; }
14
14 Top-Down Design Often need to break large problems into parts. Each part can be broken into sub-parts Similar to a book outline
15
15 Case Study Write a program to print a table of expected return on some stock investment. The stock is purchased prior to retirement – what is the value of the stock at retirement? Factors: ▫The age when the stock is purchased ▫The age at retirement ▫The amount of stock purchased Input: ▫Age at time of purchase (min / max ) ▫Age at retirement (min / max ) ▫Amount purchased
16
16 Example Output Assume 10 years between ages at purchase and 5 years between ages at retirement.
17
17 Details Interest is compounded annually Formula is ▫A = P((1 + r)^t) ▫P is principle / r is annual rate / t is number of years Anticipated rate is determined by age and principle ▫Let B = 10% / Y = years to retirement ▫Rate = B + (Y / 10) + (P – 25000)/100000
18
18 How should we ‘decompose’? Print Table Read Input Print Header Print Data Print row Compute Rate Compute Balance
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.