30
Chapter 5 Methods to our madness

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

Embed Size (px)

Citation preview

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

Chapter 5

Methods to our madness

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

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

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

Suppose we want to add three ranges of integers?

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

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

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

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).

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

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

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

Defining a method

• modifiers return_value_type method_name (parameters) { statements }

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

Calling a method

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

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

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.

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

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

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

Call stack example

• Make intelligent comments here

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

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

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

Example of “call by value”

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

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

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

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.

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

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

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

Examples of scope

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

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

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

More Math class methods

• double toRadians(double degrees); // converts from degrees to radians

• double toDegrees(double radians);// converts from radians to degrees

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

Exponential Math class methods

• double exp(double x); // • double log(double x); // loge x

• double log10(double x); // log10 x• double pow(double a, double b); // ab

• double sqrt(double x); // square root √x

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

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);

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

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

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

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

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

Program Design

• Top-down and bottom-up• Stubbing out modules• Coding and testing modules

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

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.

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

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

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

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.

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

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).