chapter 5 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 5 PowerPoint Presentation
Download Presentation
Chapter 5

Loading in 2 Seconds...

play fullscreen
1 / 30

Chapter 5 - PowerPoint PPT Presentation


  • 74 Views
  • Uploaded on

Chapter 5. Methods to our madness. 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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 5' - nen


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 5

Chapter 5

Methods to our madness

review
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
this approach has four drawbacks
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
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
defining a method
Defining a method
  • modifiersreturn_value_typemethod_name (parameters) { statements }
calling a method
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 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
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
Call stack example
  • Make intelligent comments here
java is a call by value language
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
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
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
  • 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
the math class methods
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
more math class methods
More Math class methods
  • double toRadians(double degrees); // converts from degrees to radians
  • double toDegrees(double radians); // converts from radians to degrees
exponential math class methods
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
rounding methods in math class
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
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
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
Program Design
  • Top-down and bottom-up
  • Stubbing out modules
  • Coding and testing modules
the luhn algorithm
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.
example from wikipedia
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
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
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).