1 / 18

Methods

Methods. Methods. 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: either “public”, “private” or “protected”

lazaro
Download Presentation

Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Methods

  2. Methods • 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 returnTypemethodName( 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. 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. 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; } } I’m the max No, I’m the max

  5. public int max(int n1, int n2) { if(n1 > n2) { return n1; } else { return n2; } } 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. int a = max(3, 5); int b = max(10, -1); int c = max(a, b); int d = max(a, max(b, c));

  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. public int max(int n1, int n2) { if(n1 > n2) { return n1; } else { return n2; } } 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 int a = max(3, 5); int b = max(10, -1); int c = max(a, b); int d = max(a, max(b, c));

  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 { Declarations Methods }

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

  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; } • 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. Consider…. 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; intminExam = min(e1, min(e2, e3)); return quizAverage >= .6 && examAverage >= .65 && minExam >= .35; } • Write a code fragment that reads information for a group of students and prints out for each one whether they pass or not. 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. 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 intgetBalance(int balance, double rate, int yrs) { for(int yr=1; yr <= yrs; yr++){ balance += (int)((balance)*(1+rate/100)); } return balance; }

  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 Book Chapter 1 Chapter 2 Chapter N Section A Section B Section C

  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. Example Output Assume 10 years between ages at purchase and 5 years between ages at retirement.

  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. How should we ‘decompose’?

More Related