mat 2170
play

Mat 2170 Methods Week 7 Scope return Examples Methods - PowerPoint PPT Presentation

Mat 2170 Week 7 Methods Algorithms Week 7 Mat 2170 Methods Week 7 Scope return Examples Methods Algorithms Predicate Methods Mechanics Decomposition Spring 2014 Algorithms Graphics Student Responsibilities Mat 2170 Week


  1. Mat 2170 Week 7 Methods – Algorithms Week 7 Mat 2170 Methods Week 7 Scope return Examples Methods – Algorithms Predicate Methods Mechanics Decomposition Spring 2014 Algorithms Graphics

  2. Student Responsibilities Mat 2170 Week 7 Methods – Reading: Textbook, Sections 5.2 – 5.5 Algorithms Lab Week 7 Methods Attendance Scope return Examples Overview Chapter Five, Sections 2 — 5 : Predicate 5.2 Writing your own methods Methods Mechanics 5.3 Mechanics of the method–calling process Decomposition 5.4 Decomposition Algorithms Graphics 5.5 Algorithmic methods

  3. 5.2 Writing Our Own Methods Mat 2170 Week 7 The general form of a method definition is: Methods – Algorithms scope type name ( argument list ) { Week 7 Methods statements in the method body Scope } return Examples where Predicate scope : indicates what blocks of code have access to the Methods method choices: public , private , or protected Mechanics Decomposition type : indicates the type of value the method returns (if any) Algorithms name : is the name of the method Graphics argument list : is the ordered list of declarations for the variables used to hold the values of each argument

  4. Scope and Type Mat 2170 Week 7 scope type name ( argument list ) { Methods – Algorithms statements in the method body } Week 7 Methods Scope Scope : what code blocks have access? return 1. The most common value for scope is private , which means Examples that the method is available only within its own class. Predicate 2. If other classes need access to the method, scope should be Methods public instead. Mechanics Decomposition Type should be void if a method does not return a value. Algorithms Such methods are sometimes called procedures . Graphics If a method has a return type other than void , then it must return a value.

  5. Returning Values from a Method You can return a single value from a method by including a Mat 2170 Week 7 return statement, which is usually written as: Methods – Algorithms return expression ; Week 7 Methods where expression is a Java expression that specifies the value Scope the method is to return return Examples As an example, the method definition: Predicate Methods private double feetToInches (double feet) { Mechanics return 12.0 * feet; Decomposition } Algorithms Graphics converts an argument indicating a distance in feet to the equivalent number of inches, and returns this calculated value to the calling program.

  6. Methods Involving Control Statements The body of a method can contain statements of any type, Mat 2170 Week 7 including control statements: for , while , if , and switch . Methods – Algorithms As an example, the following method uses an if statement to find the larger of the two integer arguments: Week 7 Methods private int MyMax (int x, int y) { Scope if (x > y) return { return x; Examples } Predicate else // x <= y Methods { Mechanics return y; } Decomposition } Algorithms Graphics return statements can be used at any point in the method, and may appear more than once , although only one will be executed during a particular call.

  7. The factorial Method The factorial of a number n (written as n !) is defined to be Mat 2170 Week 7 the product of the integers from 1 to n . Thus, 5! is Methods – 1 × 2 × 3 × 4 × 5, or 120. Algorithms Week 7 The following method definition uses a for loop to compute Methods the factorial function: Scope private int factorial (int n) { return int result = 1; Examples for (int i = 2; i < = n; i++) Predicate { Methods result *= i; Mechanics } Decomposition return result; Algorithms } Graphics Note here that the accumulator result stores a product rather than a sum, so it must be initialized to 1 instead of 0 .

  8. Non–numeric Methods Methods in Java can return values of any type. The following Mat 2170 Week 7 method, for example, returns the English name of the day of Methods – the week, given a number between 0(Sunday) and 6(Saturday): Algorithms Week 7 private String weekdayName (int day) { Methods switch (day) { Scope case 0: return "Sunday"; return case 1: return "Monday"; Examples case 2: return "Tuesday"; case 3: return "Wednesday"; Predicate case 4: return "Thursday"; Methods case 5: return "Friday"; Mechanics case 6: return "Saturday"; Decomposition default: return "Illegal weekday"; Algorithms } } Graphics ( String is a class defined in the package java.lang .) There is no need for a break statement following a return .

  9. Methods Returning Graphical Objects Textbook has examples of these types of methods. Mat 2170 Week 7 Methods – The following method creates a filled circle centered at the Algorithms point ( x , y ), with a radius of r pixels, and is filled using the Week 7 color specified in the parameter list. Methods private GOval createFilledCircle (double x, Scope double y, double r, Color color) { return GOval circle = new GOval(x-r, y-r, 2*r, 2*r); Examples circle.setFilled(true); Predicate circle.setColor(color); Methods return circle; Mechanics } Decomposition Algorithms Graphics If you are creating a GraphicsProgram that requires many filled circles in different colors, the createFilledCircle() method turns out to save a considerable amount of code.

  10. Predicate Methods Mat 2170 Week 7 Methods that return a boolean value play an important role in Methods – programming and are called predicate methods . Algorithms Week 7 As an example, the following method returns true if the first Methods argument is divisible by the second, and false otherwise: Scope return Examples private boolean isDivisibleBy (int x, int y) { Predicate Methods return x % y == 0; Mechanics } Decomposition Algorithms Graphics Notice that when x is evenly divisible by y , true is returned, otherwise false is returned — an if statement isn’t required in this case.

  11. Invoking Predicate Methods Mat 2170 Once you have defined a predicate method, you can use it just Week 7 like any other Boolean value. Methods – Algorithms For example, you can print the integers between low and high Week 7 that are divisible by 7 by running a for loop through the Methods integers [ low..high ] and checking which are divisible by 7: Scope return for (int i = low; i < = high; i++) Examples { Predicate Methods if (isDivisibleBy(i, 7)) { Mechanics println(i); Decomposition } Algorithms } Graphics Notice that numbers which aren’t divisible by 7 are simply ignored.

  12. Using Predicate Methods Effectively Mat 2170 While the following code is not incorrect, it is inelegant : Week 7 Methods – Algorithms private boolean isDivisibleBy (int x, int y) { if (x % y == 0) Week 7 { return true; Methods } else Scope { return false; return } } Examples Predicate Methods Mechanics A similar problem occurs when beginning programmers include Decomposition an explicit comparison in an if statement to see if a predicate Algorithms method returns true . Graphics Avoid redundant tests such as this: if (isDivisibleBy(i, 7) == true)

  13. Method: Powers of Two The following method takes an integer n and returns true if n Mat 2170 Week 7 is a power of two, and false otherwise. Methods – Algorithms The powers of 2 are: 1, 2, 4, 8, 16, 32, and so forth; numbers that are less than or equal to zero cannot be powers of two. Week 7 Methods private boolean isPowerOfTwo (int n) { Scope if (n < 1) return false; return while (n > 1) { Examples if (n % 2 == 1) return false; Predicate n /= 2; Methods } Mechanics return true; Decomposition } Algorithms Graphics If at any time it is discovered that the value is not a power of 2, false is returned. If execution drops out of the loop, then the original number was a power of 2, and true is returned.

  14. 5.3 Mechanics of the Method–Calling Process Mat 2170 When you invoke a method Week 7 Methods – the following actions occur: Algorithms Week 7 The argument expressions are evaluated (in the context of Methods the calling method) Scope return Examples Each argument value is copied into the corresponding Predicate parameter variable , which is allocated in a newly assigned Methods region of memory called a stack frame . Mechanics Decomposition Algorithms This assignment follows the order in which the arguments Graphics appear: the first argument is copied into the first parameter variable, and so on.

  15. Mechanics of the Method–Calling Process, Cont. Mat 2170 Week 7 Methods – The statements in the method body are evaluated (using the Algorithms new stack frame to look up the values of local variables). Week 7 Methods Scope When a return statement is encountered, it computes the return return value and substitutes that value in place of the original Examples call. Predicate Methods Mechanics Decomposition The stack frame for the called method is discarded , and Algorithms execution is returned to the calling program, continuing from Graphics where it left off.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend