Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Methods to our madness. Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter,

Similar presentations


Presentation on theme: "Chapter 5 Methods to our madness. Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter,"— Presentation transcript:

1 Chapter 5 Methods to our madness

2 Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter, ‘_’, or ‘$’ character Additional characters can be letters, ‘_’, ‘$’, and digits. Cannot be a reserved word Cannot be “true,” “false,” or “null” Identifiers can be any length

3 Suppose we want to add three ranges of integers?

4 We can repeat the code three times, changing the values for each copy

5 This approach has four drawbacks Copying and modifying code blocks takes (hopefully) valuable time for a repetitive task. Compiling repetitive code blocks makes the program larger on disk and in memory and reduces locality for caching. Every instance of the code has to be debugged and tested separately. Repeating code blocks over and over makes the program harder to understand (maintain).

6 Defining methods solves the problems Methods define a block of code to perform a specific task using a set of parameters. The method can be written once and debugged, tested, and maintained in one place. Changes can be Partitioning programs into methods makes understanding the processing easier. Methods save space and improve locality

7 Defining a method modifiers return_value_type method_name (parameters) { statements }

8 Calling a method Write a statement comprised of the method name with actual parameters inside the parentheses and separated by commas

9 The method signature The method name and the parameter list constitute the method signature. The return type is not part of the method signature and cannot be used to distinguish methods. This will be important later when we learn about polymorphism and inheritance.

10 Sounding like a pro Java has methods; other languages have functions and procedures. We define methods and declare variables Correct terminology is a social convention, but it matters a lot to some people and it will help your career if you use the accepted terms

11 Call stack example Make intelligent comments here

12 Java is a “call by value” language A copy of the value is passed as a parameter The value outside the method is not affected by any operation inside the method

13 Example of “call by value”

14 Modularization/Encapsulation Improves design by isolating components and algorithms into circumscribed modules Simplifies maintenance by confining debugging, testing, and trouble-shooting to a defined, independent module Allows re-using the code in other programs

15 Overloading methods An overloaded methods have the same method name, but different parameters Different parameters can be different types or different number of parameters Overloaded methods are not distinguished by different return values.

16

17 Scope Scope refers to where in the program a variable can be referenced Generally, variables can be referenced anywhere in the block of code they are declared in Method parameters are variables within the method

18 Examples of scope

19 The Math class methods double sin(double radians); // sine double cos(double radians); // cosine double tan(double radians); // tangent double asin(double radians); // arcsine double acos(double radians); // arccosine double atan(double radians); // arctangent

20 More Math class methods double toRadians(double degrees); // converts from degrees to radians double toDegrees(double radians); // converts from radians to degrees

21 Exponential Math class methods

22 Rounding methods in Math class double ceil(double x);// round up double floor(double x);// round down double rint(double x); // note caveat int round(float x); long round(double x);

23 The min, max, and abs methods A family of overloaded methods for any combination of int, long, float, and double Math.min(a, b); // minimum of a and b Math.max(a, b); // maximum of a and b Math.abs(x); // absolute value of x

24 Random numbers Math.random() returns a double value in the range 0.0 <= x < 1.0 The formula a + Math.random() * b generates numbers in the range a <= x < (a+b) Do not rely on built-in random number generators for serious numerical processing applications—use special libraries

25

26 Program Design Top-down and bottom-up Stubbing out modules Coding and testing modules

27 The Luhn Algorithm Counting from the rightmost digit and moving left, double the value of every second digit. Sum the digits of the products (e.g., 10: = 1, 14: = 5) together with the undoubled digits from the original number. If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.modulo

28 Example from Wikipedia Account number " " will have a check digit added, making it of the form x: The yellow boxes are the doubled digits; sum the digits if doubled value is greater than 9

29 Check digit To make x a valid credit card, the sum needs to be an even multiple of 10. Since the sum is 67, the ‘x’ digit should be a 3 to make the sum equal to 70.

30 Lab 1 Write a program that accepts a credit card number and prints a message indicating whether the number is valid or invalid using the Luhn algorithm. Extra credit: determine the type of credit card for five different issuers and print the name of the credit card along with the valid or invalid message (or issuer unknown message).


Download ppt "Chapter 5 Methods to our madness. Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter,"

Similar presentations


Ads by Google