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:
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
Suppose we want to add three ranges of integers?
We can repeat the code three times, changing the values for each copy
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).
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
Calling a method Write a statement comprised of the method name with actual parameters inside the parentheses and separated by commas
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.
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
Call stack example Make intelligent comments here
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
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
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.
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
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);
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
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
Program Design Top-down and bottom-up Stubbing out modules Coding and testing modules
The Luhn Algorithm http://en.wikipedia.org/wiki/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 + 0 = 1, 14: 1 + 4 = 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
Example from Wikipedia Account number "7992739871" will have a check digit added, making it of the form 7992739871x: The yellow boxes are the doubled digits; sum the digits if doubled value is greater than 9
Check digit To make 7992739871x 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.
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).