SlideShare a Scribd company logo
https://www.facebook.com/Oxus20
oxus20@gmail.com
Java
Methods
Conditional statements
Zalmai Arman
Outline
https://www.facebook.com/Oxus20
Introduction
Creating & Calling a Method
Void Method
Passing Parameters by Values
Overloading Methods
The Scope of Variables
The Math Class
The Random Method
Method Abstraction
Introduction
» In the preceding chapters, you learned about such
methods as System.out.println,
JOptionPane.showMessageDialog,
JOptionPane.showInputDialog.
» A method is a collection of statements that are grouped
together to perform an operation.
» When you call the System.out.println method, for
example, the system actually executes several statements
in order to display a message.
https://www.facebook.com/Oxus20
Creating a Method
» In general, a method has the following syntax:
modifier returnValueType methodName(list of parameters)
{
// Method body;
}
» Let's take a look at a method created to find which of two
integers is bigger.This method, named max, has two int
parameters, num1 and num2, the larger of which is
returned by the method.
https://www.facebook.com/Oxus20
Creating & Calling Method
https://www.facebook.com/Oxus20
Output will be: the result of max methd is 45
Creating a Method
» A method declaration consists of a method header and a
method body.
https://www.facebook.com/Oxus20
Method Header
Method Body
Modifier return value type method name formal parameter
Creating a Method
» The method header specifies the modifiers, return value type,
method name, and parameters of the method.The modifier, which
is optional, tells the compiler how to call the method.The
static modifier is used for all the methods in this chapter.The
reason for using it will be discussed in "Objects and Classes.“.
» A method may return a value.The returnValueType is the data
type of the value the method returns. Some methods perform
the desired operations without returning a value. In this case,
the returnValueType is the keyword void. For example, the
returnValueType in the main method is void, as well as in
System.exit, System.out.println, and
JOptionPane.showMessageDialog.The method that returns a
value is called a nonvoid method, and the method that does
not return a value is called a void method.
https://www.facebook.com/Oxus20
Creating a Method
» The variables defined in the method header are known as
formal parameters or simply parameters.
» When a method is invoked, you pass a value to the
parameter.This value is referred to as actual parameter or
argument.
» The parameter list refers to the type, order, and number
of the parameters of a method.The method name and the
parameter list together constitute the method signature.
Parameters are optional; that is, a method may contain no
parameters.
https://www.facebook.com/Oxus20
Void Method
» The preceding section gives an example of a nonvoid
method.This section shows how to declare and invoke a
void method.
» gives a program that declares a method named grade and
invokes it to print the grade for a given score.
» The grade method is a void method. It does not return
any value.A call to a void method must be a statement.
So, it is invoked as a statement in the main method.This
statement is like any Java statement terminated with a
semicolon.
https://www.facebook.com/Oxus20
Void Method
https://www.facebook.com/Oxus20
Void Method
https://www.facebook.com/Oxus20
» A return statement is not needed for a void method, but it
can be used for terminating the method and returning to
the method's caller.The syntax is simply:
return;
Passing Parameters by Values
https://www.facebook.com/Oxus20
» The power of a method is its ability to work with
parameters.
» You can use println to print any string and max to find the
maximum between any two int values.
» When calling a method, you need to provide arguments,
which must be given in the same order as their respective
parameters in the method specification.This is known as
parameter order association.
» For example, the following method prints a message n
times:
Passing Parameters by Values
https://www.facebook.com/Oxus20
» You can use nPrintln(“hello", 3) to print "Hello" three times.The
nPrintln(“hello", 3) statement passes the actual string parameter,
“hello", to the parameter, message; passes 3 to n; and prints “hello"
three times. However, the statement nPrintln(3,“hello") would be
wrong.The data type of 3 does not match the data type for the first
parameter, message, nor does the second parameter,“hello", match
the second parameter, n.
Passing Parameters by Values
https://www.facebook.com/Oxus20
» The arguments must match the parameters in order,
number, and compatible type, as defined in the method
signature.
» Compatible type means that you can pass an argument to
a parameter without explicit casting, such as passing an int
value argument to a double value parameter.
» When you invoke a method with a parameter, the value of
the argument is passed to the parameter.This is referred
to as pass-by-value.
Passing Parameters by Values
https://www.facebook.com/Oxus20
» Swap is a program that demonstrates the effect of passing
by value.
» The program creates a method for swapping two
variables.
» The swap method is invoked by passing two arguments.
Interestingly, the values of the arguments are not changed
after the method is invoked.
Passing Parameters by Values
https://www.facebook.com/Oxus20
Passing Parameters by Values
https://www.facebook.com/Oxus20
» Before the swap method is invoked, num1 is 1 and num2
is 2.
» After the swap method is invoked, num1 is still 1 and
num2 is still 2.Their values are not swapped after the
swap method is invoked.
» the values of the arguments num1 and num2 are passed to
n1 and n2, but n1 and n2 have their own memory
locations independent of num1 and num2.
» Therefore, changes in n1 and n2 do not affect the contents
of num1 and num2.
Overloading Method
https://www.facebook.com/Oxus20
» The max method that was used earlier works only with
the int data type.
» But what if you need to find which of two floating-point
numbers has the maximum value?
» The solution is to create another method with the same
name but different parameters, as shown in the following
code:
public static double max(double num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
}
Overloading Method
https://www.facebook.com/Oxus20
» If you call max with int parameters, the max method that
expects int parameters will be invoked;
» if you call max with double parameters, the max method
that expects double parameters will be invoked.
» This is referred to as method overloading; that is, two
methods have the same name but different parameter lists
within one class.
» Next page program that creates three methods.The first
finds the maximum integer, the second finds the
maximum double, and the third finds the maximum
among three double values.All three methods are named
max.
Overloading Method
https://www.facebook.com/Oxus20
Overloading Method
https://www.facebook.com/Oxus20
» When calling max(5, 6) , the max method for finding the
maximum of two integers is invoked.
» When calling max(5.5, 6.6), the max method for finding the
maximum of two doubles is invoked.
» When calling max(10, 11.2, 12.5) , the max method for
finding the maximum of three double values is invoked.
» Can you invoke the max method with an int value and a
double value, such as max(10, 11.2,12.5)? If so, which of the
max methods is invoked?The answer to the first question is
yes.The answer to the second is that the max method for
finding the maximum of two double values is invoked.The
argument value 10 is automatically converted into a double
value and passed to this method.
Overloading Method
https://www.facebook.com/Oxus20
» Sometimes there are two
or more possible matches
for an invocation of a
method, but the compiler
cannot determine the most
specific match.This is
referred to as ambiguous
invocation.Ambiguous
invocation causes a
compilation error.
Consider the following
code:
» Both max(int, double) and max(double,
int) are possible candidates to match
max(1, 2). Since neither of them is
more specific than the other, the
invocation is ambiguous, resulting in a
compilation error.
The Scope and Variables
https://www.facebook.com/Oxus20
» The scope of a variable is the part of the program where the
variable can be referenced.
» A variable defined inside a method is referred to as a local
variable.
» The scope of a local variable starts from its declaration and
continues to the end of the block that contains the variable.
» A local variable must be declared before it can be used.
» A parameter is actually a local variable.The scope of a method
parameter covers the entire method.
The Scope and Variables
https://www.facebook.com/Oxus20
» A variable declared in the initial action part of a for loop header has
its scope in the entire loop.
» But a variable declared inside a for loop body has its scope limited in
the loop body from its declaration to the end of the block that
contains the variable.
The Scope and Variables
https://www.facebook.com/Oxus20
» You can declare a local variable with the same name multiple times in
different non-nesting blocks in a method.
» but you cannot declare a local variable twice in nested blocks.
The Scope and Variables
https://www.facebook.com/Oxus20
» Do not declare a variable inside a block and then attempt to use it
outside the block. Here is an example of a common mistake:
» The last statement would cause a syntax error because variable i is
not defined outside of the for loop.
The Math Class
https://www.facebook.com/Oxus20
» The Math class contains the methods needed to perform basic
mathematical functions.
» Trigonometric Methods
public static double sin(double radians)
public static double cos(double radians)
public static double tan(double radians)
public static double asin(double radians)
public static double acos(double radians)
public static double atan(double radians)
public static double toRadians(double degree)
public static double toDegrees(double radians)
The Math Class
https://www.facebook.com/Oxus20
Examples
Math.sin(0) returns 0.0
Math.sin(Math.toRadians(270)) returns -1.0
Math.sin(Math.PI / 6) returns 0.5
Math.sin(Math.PI / 2) returns 1.0
Math.cos(0) returns 1.0
Math.cos(Math.PI / 6) returns 0.866
Math.cos(Math.PI / 2) returns 0
The Math Class
https://www.facebook.com/Oxus20
Exponent Methods
» There are five methods related to exponents in the Math class:
/** Return e raised to the power of x (ex) */
public static double exp(double x)
/** Return the natural logarithm of x (ln(x) = loge(x)) */
static double log(double x)
/** Return the base 10 logarithm of x (log10(x)) */
public static double log10(double x)
/** Return a raised to the power of b (xb) */
public static double pow(double x, double b)
/** Return the square root of a () */
public static double sqrt(double x)
» Note that the parameter in the sqrt method must not be negative.
The Math Class
https://www.facebook.com/Oxus20
Examples
Math.exp(1) returns 2.71828
Math.log(Math.E) returns 1.0
Math.log10(10) returns 1.0
Math.pow(2, 3) returns 8.0
Math.pow(3, 2) returns 9.0
Math.pow(3.5, 2.5) returns 22.91765
Math.sqrt(4) returns 2.0
Math.sqrt(10.5) returns 3.24
The Math Class
https://www.facebook.com/Oxus20
The Rounding Methods
» The Math class contains five rounding methods:
/** x rounded up to its nearest integer. This integer is
* returned as a double value. */
public static double ceil(double x)
/** x is rounded down to its nearest integer. This integer is
* returned as a double value. */
public static double floor(double x)
/** x is rounded to its nearest integer. If x is equally close
* to two integers, the even one is returned as a double. */
public static double rint(double x)
/** Return (int)Math.floor(x + 0.5). */
public static int round(float x)
/** Return (long)Math.floor(x + 0.5). */
public static long round(double x)
The Math Class
https://www.facebook.com/Oxus20
Examples
Math.ceil(2.1) returns 3.0
Math.ceil(2.0) returns 2.0
Math.ceil(–2.0) returns –2.0
Math.ceil(–2.1) returns -2.0
Math.floor(2.1) returns 2.0
Math.floor(2.0) returns 2.0
Math.floor(-2.0) returns –2.0
Math.floor(-2.1) returns -3.0
Math.rint(2.1) returns 2.0
Math.rint(2.0) returns 2.0
Math.rint(-2.0) returns –2.0
Math.rint(-2.1) returns –2.0
Math.rint(2.5) returns 2.0
Math.rint(-2.5) returns -2.0
Math.round(2.6f) returns 3 // Returns int
Math.round(2.0) returns 2 // Returns long
Math.round(–2.0f) returns -2
Math.round(–2.6) returns -3
The Math Class
https://www.facebook.com/Oxus20
The min, max, and abs Methods
» The min and max methods are overloaded to return the minimum
and maximum numbers between two numbers (int, long, float, or
double). For example, max(3.4, 5.0) returns 5.0, and min(3, 2)
returns 2.
» The abs method is overloaded to return the absolute value of the
number (int, long, float, and double). For example,
Math.max(2, 3) returns 3
Math.max(2.5, 3) returns 3.0
Math.min(2.5, 3.6) returns 2.5
Math.abs(-2) returns 2
Math.abs(-2.1) returns 2.1
The Math Class
https://www.facebook.com/Oxus20
The random Method
» The Math class also has a powerful method, random, which generates a
random double value greater than or equal to 0.0 and less than 1.0 (0.0 <=
Math.random() < 1.0).
» Not all classes need a main method.The Math class and JOptionPane class
do not have main methods.These classes contain methods for other classes
to use.
The Math Class
https://www.facebook.com/Oxus20
The random Method
Method Abstraction
https://www.facebook.com/Oxus20
» The key to developing software is to apply the concept of abstraction.
» Method abstraction is achieved by separating the use of a method from
its implementation.
» The client can use a method without knowing how it is implemented.
The details of the implementation are encapsulated in the method
and hidden from the client who invokes the method.This is known as
information hiding or encapsulation.
» If you decide to change the implementation, the client program will
not be affected, provided that you do not change the method
signature.The implementation of the method is hidden from the
client in a "black box,"
Method Abstraction
https://www.facebook.com/Oxus20
» You have already used the System.out.print method to display a
string, the JOptionPane.showInputDialog method to read a
string from a dialog box, and the max method to find the
maximum number.You know how to write the code to invoke
these methods in your program, but as a user of these methods,
you are not required to know how they are implemented.
END
https://www.facebook.com/Oxus20
38

More Related Content

PPTX
PDF
Class and Objects in Java
PDF
Methods in Java
PPTX
Constructor in java
PPT
governor, vice governor, sangguniang panlalawigan
PPT
Jdbc ppt
PPTX
Chapter 1 Introduction to Media and Information Literacy
PPTX
Database Management System ppt
Class and Objects in Java
Methods in Java
Constructor in java
governor, vice governor, sangguniang panlalawigan
Jdbc ppt
Chapter 1 Introduction to Media and Information Literacy
Database Management System ppt

What's hot (20)

PPT
Method overriding
PDF
Inheritance In Java
PPTX
Java packages
PDF
Java notes
PPTX
Methods in java
PPTX
Classes objects in java
PDF
Java 8 Lambda Expressions
PPTX
Exception handling
PPTX
Java abstract class & abstract methods
PPTX
Method overloading
PPTX
Core java complete ppt(note)
PPTX
Inheritance in java
PPT
C# Basics
PPTX
Introduction to Java Strings, By Kavita Ganesan
PDF
Arrays in Java
PPTX
Classes, objects in JAVA
PPTX
PPTX
Javascript functions
Method overriding
Inheritance In Java
Java packages
Java notes
Methods in java
Classes objects in java
Java 8 Lambda Expressions
Exception handling
Java abstract class & abstract methods
Method overloading
Core java complete ppt(note)
Inheritance in java
C# Basics
Introduction to Java Strings, By Kavita Ganesan
Arrays in Java
Classes, objects in JAVA
Javascript functions
Ad

Similar to Java Methods (20)

PDF
Java Arrays
PPTX
Conditional Statement
PDF
CIS 1403 lab 3 functions and methods in Java
PPT
procedures
PPTX
Progamming Primer Polymorphism (Method Overloading) VB
PPTX
Lecture_7 Method Overloading.pptx
DOCX
Java execise
PPTX
Java method present by showrov ahamed
PPTX
Intro to programing with java-lecture 3
PDF
Discrete structure ch 3 short question's
PDF
Java Polymorphism: Types And Examples (Geekster)
PDF
Mcs 021
PPTX
IntroductionJava Programming - Math Class
PPTX
PPTX
JMeter Post-Processors
PPT
Effective Java - Design Method Signature Overload Varargs
PDF
Algorithms
PPTX
Sharbani bhattacharya VB Structures
PPTX
Hemajava
Java Arrays
Conditional Statement
CIS 1403 lab 3 functions and methods in Java
procedures
Progamming Primer Polymorphism (Method Overloading) VB
Lecture_7 Method Overloading.pptx
Java execise
Java method present by showrov ahamed
Intro to programing with java-lecture 3
Discrete structure ch 3 short question's
Java Polymorphism: Types And Examples (Geekster)
Mcs 021
IntroductionJava Programming - Math Class
JMeter Post-Processors
Effective Java - Design Method Signature Overload Varargs
Algorithms
Sharbani bhattacharya VB Structures
Hemajava
Ad

More from OXUS 20 (19)

PPTX
Structure programming – Java Programming – Theory
PDF
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PDF
Java Applet and Graphics
PDF
Fundamentals of Database Systems Questions and Answers
PDF
Everything about Database JOINS and Relationships
PDF
Create Splash Screen with Java Step by Step
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
PDF
Web Design and Development Life Cycle and Technologies
PDF
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
PDF
Java Unicode with Cool GUI Examples
PDF
JAVA GUI PART III
PDF
Java Regular Expression PART II
PDF
Java Regular Expression PART I
PDF
Java GUI PART II
PDF
JAVA GUI PART I
PDF
Java Guessing Game Number Tutorial
PDF
JAVA Programming Questions and Answers PART III
PDF
Object Oriented Programming with Real World Examples
PDF
Object Oriented Concept Static vs. Non Static
Structure programming – Java Programming – Theory
PHP Basic and Fundamental Questions and Answers with Detail Explanation
Java Applet and Graphics
Fundamentals of Database Systems Questions and Answers
Everything about Database JOINS and Relationships
Create Splash Screen with Java Step by Step
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Web Design and Development Life Cycle and Technologies
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Unicode with Cool GUI Examples
JAVA GUI PART III
Java Regular Expression PART II
Java Regular Expression PART I
Java GUI PART II
JAVA GUI PART I
Java Guessing Game Number Tutorial
JAVA Programming Questions and Answers PART III
Object Oriented Programming with Real World Examples
Object Oriented Concept Static vs. Non Static

Recently uploaded (20)

PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
System and Network Administration Chapter 2
PPTX
L1 - Introduction to python Backend.pptx
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
AI in Product Development-omnex systems
PDF
PTS Company Brochure 2025 (1).pdf.......
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Design an Analysis of Algorithms I-SECS-1021-03
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Softaken Excel to vCard Converter Software.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
System and Network Administration Chapter 2
L1 - Introduction to python Backend.pptx
ISO 45001 Occupational Health and Safety Management System
Which alternative to Crystal Reports is best for small or large businesses.pdf
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Upgrade and Innovation Strategies for SAP ERP Customers
Design an Analysis of Algorithms II-SECS-1021-03
VVF-Customer-Presentation2025-Ver1.9.pptx
AI in Product Development-omnex systems
PTS Company Brochure 2025 (1).pdf.......

Java Methods

  • 2. Outline https://www.facebook.com/Oxus20 Introduction Creating & Calling a Method Void Method Passing Parameters by Values Overloading Methods The Scope of Variables The Math Class The Random Method Method Abstraction
  • 3. Introduction » In the preceding chapters, you learned about such methods as System.out.println, JOptionPane.showMessageDialog, JOptionPane.showInputDialog. » A method is a collection of statements that are grouped together to perform an operation. » When you call the System.out.println method, for example, the system actually executes several statements in order to display a message. https://www.facebook.com/Oxus20
  • 4. Creating a Method » In general, a method has the following syntax: modifier returnValueType methodName(list of parameters) { // Method body; } » Let's take a look at a method created to find which of two integers is bigger.This method, named max, has two int parameters, num1 and num2, the larger of which is returned by the method. https://www.facebook.com/Oxus20
  • 5. Creating & Calling Method https://www.facebook.com/Oxus20 Output will be: the result of max methd is 45
  • 6. Creating a Method » A method declaration consists of a method header and a method body. https://www.facebook.com/Oxus20 Method Header Method Body Modifier return value type method name formal parameter
  • 7. Creating a Method » The method header specifies the modifiers, return value type, method name, and parameters of the method.The modifier, which is optional, tells the compiler how to call the method.The static modifier is used for all the methods in this chapter.The reason for using it will be discussed in "Objects and Classes.“. » A method may return a value.The returnValueType is the data type of the value the method returns. Some methods perform the desired operations without returning a value. In this case, the returnValueType is the keyword void. For example, the returnValueType in the main method is void, as well as in System.exit, System.out.println, and JOptionPane.showMessageDialog.The method that returns a value is called a nonvoid method, and the method that does not return a value is called a void method. https://www.facebook.com/Oxus20
  • 8. Creating a Method » The variables defined in the method header are known as formal parameters or simply parameters. » When a method is invoked, you pass a value to the parameter.This value is referred to as actual parameter or argument. » The parameter list refers to the type, order, and number of the parameters of a method.The method name and the parameter list together constitute the method signature. Parameters are optional; that is, a method may contain no parameters. https://www.facebook.com/Oxus20
  • 9. Void Method » The preceding section gives an example of a nonvoid method.This section shows how to declare and invoke a void method. » gives a program that declares a method named grade and invokes it to print the grade for a given score. » The grade method is a void method. It does not return any value.A call to a void method must be a statement. So, it is invoked as a statement in the main method.This statement is like any Java statement terminated with a semicolon. https://www.facebook.com/Oxus20
  • 11. Void Method https://www.facebook.com/Oxus20 » A return statement is not needed for a void method, but it can be used for terminating the method and returning to the method's caller.The syntax is simply: return;
  • 12. Passing Parameters by Values https://www.facebook.com/Oxus20 » The power of a method is its ability to work with parameters. » You can use println to print any string and max to find the maximum between any two int values. » When calling a method, you need to provide arguments, which must be given in the same order as their respective parameters in the method specification.This is known as parameter order association. » For example, the following method prints a message n times:
  • 13. Passing Parameters by Values https://www.facebook.com/Oxus20 » You can use nPrintln(“hello", 3) to print "Hello" three times.The nPrintln(“hello", 3) statement passes the actual string parameter, “hello", to the parameter, message; passes 3 to n; and prints “hello" three times. However, the statement nPrintln(3,“hello") would be wrong.The data type of 3 does not match the data type for the first parameter, message, nor does the second parameter,“hello", match the second parameter, n.
  • 14. Passing Parameters by Values https://www.facebook.com/Oxus20 » The arguments must match the parameters in order, number, and compatible type, as defined in the method signature. » Compatible type means that you can pass an argument to a parameter without explicit casting, such as passing an int value argument to a double value parameter. » When you invoke a method with a parameter, the value of the argument is passed to the parameter.This is referred to as pass-by-value.
  • 15. Passing Parameters by Values https://www.facebook.com/Oxus20 » Swap is a program that demonstrates the effect of passing by value. » The program creates a method for swapping two variables. » The swap method is invoked by passing two arguments. Interestingly, the values of the arguments are not changed after the method is invoked.
  • 16. Passing Parameters by Values https://www.facebook.com/Oxus20
  • 17. Passing Parameters by Values https://www.facebook.com/Oxus20 » Before the swap method is invoked, num1 is 1 and num2 is 2. » After the swap method is invoked, num1 is still 1 and num2 is still 2.Their values are not swapped after the swap method is invoked. » the values of the arguments num1 and num2 are passed to n1 and n2, but n1 and n2 have their own memory locations independent of num1 and num2. » Therefore, changes in n1 and n2 do not affect the contents of num1 and num2.
  • 18. Overloading Method https://www.facebook.com/Oxus20 » The max method that was used earlier works only with the int data type. » But what if you need to find which of two floating-point numbers has the maximum value? » The solution is to create another method with the same name but different parameters, as shown in the following code: public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; }
  • 19. Overloading Method https://www.facebook.com/Oxus20 » If you call max with int parameters, the max method that expects int parameters will be invoked; » if you call max with double parameters, the max method that expects double parameters will be invoked. » This is referred to as method overloading; that is, two methods have the same name but different parameter lists within one class. » Next page program that creates three methods.The first finds the maximum integer, the second finds the maximum double, and the third finds the maximum among three double values.All three methods are named max.
  • 21. Overloading Method https://www.facebook.com/Oxus20 » When calling max(5, 6) , the max method for finding the maximum of two integers is invoked. » When calling max(5.5, 6.6), the max method for finding the maximum of two doubles is invoked. » When calling max(10, 11.2, 12.5) , the max method for finding the maximum of three double values is invoked. » Can you invoke the max method with an int value and a double value, such as max(10, 11.2,12.5)? If so, which of the max methods is invoked?The answer to the first question is yes.The answer to the second is that the max method for finding the maximum of two double values is invoked.The argument value 10 is automatically converted into a double value and passed to this method.
  • 22. Overloading Method https://www.facebook.com/Oxus20 » Sometimes there are two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match.This is referred to as ambiguous invocation.Ambiguous invocation causes a compilation error. Consider the following code: » Both max(int, double) and max(double, int) are possible candidates to match max(1, 2). Since neither of them is more specific than the other, the invocation is ambiguous, resulting in a compilation error.
  • 23. The Scope and Variables https://www.facebook.com/Oxus20 » The scope of a variable is the part of the program where the variable can be referenced. » A variable defined inside a method is referred to as a local variable. » The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. » A local variable must be declared before it can be used. » A parameter is actually a local variable.The scope of a method parameter covers the entire method.
  • 24. The Scope and Variables https://www.facebook.com/Oxus20 » A variable declared in the initial action part of a for loop header has its scope in the entire loop. » But a variable declared inside a for loop body has its scope limited in the loop body from its declaration to the end of the block that contains the variable.
  • 25. The Scope and Variables https://www.facebook.com/Oxus20 » You can declare a local variable with the same name multiple times in different non-nesting blocks in a method. » but you cannot declare a local variable twice in nested blocks.
  • 26. The Scope and Variables https://www.facebook.com/Oxus20 » Do not declare a variable inside a block and then attempt to use it outside the block. Here is an example of a common mistake: » The last statement would cause a syntax error because variable i is not defined outside of the for loop.
  • 27. The Math Class https://www.facebook.com/Oxus20 » The Math class contains the methods needed to perform basic mathematical functions. » Trigonometric Methods public static double sin(double radians) public static double cos(double radians) public static double tan(double radians) public static double asin(double radians) public static double acos(double radians) public static double atan(double radians) public static double toRadians(double degree) public static double toDegrees(double radians)
  • 28. The Math Class https://www.facebook.com/Oxus20 Examples Math.sin(0) returns 0.0 Math.sin(Math.toRadians(270)) returns -1.0 Math.sin(Math.PI / 6) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.cos(0) returns 1.0 Math.cos(Math.PI / 6) returns 0.866 Math.cos(Math.PI / 2) returns 0
  • 29. The Math Class https://www.facebook.com/Oxus20 Exponent Methods » There are five methods related to exponents in the Math class: /** Return e raised to the power of x (ex) */ public static double exp(double x) /** Return the natural logarithm of x (ln(x) = loge(x)) */ static double log(double x) /** Return the base 10 logarithm of x (log10(x)) */ public static double log10(double x) /** Return a raised to the power of b (xb) */ public static double pow(double x, double b) /** Return the square root of a () */ public static double sqrt(double x) » Note that the parameter in the sqrt method must not be negative.
  • 30. The Math Class https://www.facebook.com/Oxus20 Examples Math.exp(1) returns 2.71828 Math.log(Math.E) returns 1.0 Math.log10(10) returns 1.0 Math.pow(2, 3) returns 8.0 Math.pow(3, 2) returns 9.0 Math.pow(3.5, 2.5) returns 22.91765 Math.sqrt(4) returns 2.0 Math.sqrt(10.5) returns 3.24
  • 31. The Math Class https://www.facebook.com/Oxus20 The Rounding Methods » The Math class contains five rounding methods: /** x rounded up to its nearest integer. This integer is * returned as a double value. */ public static double ceil(double x) /** x is rounded down to its nearest integer. This integer is * returned as a double value. */ public static double floor(double x) /** x is rounded to its nearest integer. If x is equally close * to two integers, the even one is returned as a double. */ public static double rint(double x) /** Return (int)Math.floor(x + 0.5). */ public static int round(float x) /** Return (long)Math.floor(x + 0.5). */ public static long round(double x)
  • 32. The Math Class https://www.facebook.com/Oxus20 Examples Math.ceil(2.1) returns 3.0 Math.ceil(2.0) returns 2.0 Math.ceil(–2.0) returns –2.0 Math.ceil(–2.1) returns -2.0 Math.floor(2.1) returns 2.0 Math.floor(2.0) returns 2.0 Math.floor(-2.0) returns –2.0 Math.floor(-2.1) returns -3.0 Math.rint(2.1) returns 2.0 Math.rint(2.0) returns 2.0 Math.rint(-2.0) returns –2.0 Math.rint(-2.1) returns –2.0 Math.rint(2.5) returns 2.0 Math.rint(-2.5) returns -2.0 Math.round(2.6f) returns 3 // Returns int Math.round(2.0) returns 2 // Returns long Math.round(–2.0f) returns -2 Math.round(–2.6) returns -3
  • 33. The Math Class https://www.facebook.com/Oxus20 The min, max, and abs Methods » The min and max methods are overloaded to return the minimum and maximum numbers between two numbers (int, long, float, or double). For example, max(3.4, 5.0) returns 5.0, and min(3, 2) returns 2. » The abs method is overloaded to return the absolute value of the number (int, long, float, and double). For example, Math.max(2, 3) returns 3 Math.max(2.5, 3) returns 3.0 Math.min(2.5, 3.6) returns 2.5 Math.abs(-2) returns 2 Math.abs(-2.1) returns 2.1
  • 34. The Math Class https://www.facebook.com/Oxus20 The random Method » The Math class also has a powerful method, random, which generates a random double value greater than or equal to 0.0 and less than 1.0 (0.0 <= Math.random() < 1.0). » Not all classes need a main method.The Math class and JOptionPane class do not have main methods.These classes contain methods for other classes to use.
  • 36. Method Abstraction https://www.facebook.com/Oxus20 » The key to developing software is to apply the concept of abstraction. » Method abstraction is achieved by separating the use of a method from its implementation. » The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method.This is known as information hiding or encapsulation. » If you decide to change the implementation, the client program will not be affected, provided that you do not change the method signature.The implementation of the method is hidden from the client in a "black box,"
  • 37. Method Abstraction https://www.facebook.com/Oxus20 » You have already used the System.out.print method to display a string, the JOptionPane.showInputDialog method to read a string from a dialog box, and the max method to find the maximum number.You know how to write the code to invoke these methods in your program, but as a user of these methods, you are not required to know how they are implemented.