LESSON 6 B Methods-PassByValue
LESSON 6 B Methods-PassByValue
DisplayHello(3)
Value 3 is being received
by variable z
DisplayHello(x)
The value of x is being
received by z
DisplayHello(x+2)
The value of 5 is being
received by z
Example #2: 2 parameters
Format in defining a new User-
defined method that DO NOT
RETURN a value and WITH
parameter
Return type: void
Pass-by-value Concepts
Method header
WHERE:
public – access modifier
static – does not need a class to use the
method
void – return type
methodName – user-defined
parameters – formal parameters
Method Call
• Method call
Example:
• Method header
• Method call
Almost complete program
Creating a new User-defined
method that RETURNs a value and
NO parameter
Method Header
• <returnType>
– One return type
– int, char, String, double
• Must include a return statement
Method Call
Example of a method call that
returns a value.
• String str=“”;
• Str = JOptionPane.showInputDialog(“Enter”);
• int x;
• X = Integer.parseInt(JOptionPane.showInputDialog(“Enter data”);
Example #1: int return type
Example #2: char return type
Creating User-defined method
that RETURNs a value and WITH
parameter
• <returnType>
– One return type
– int, char, String, double
• Must include a return statement
Method Call
Example:
Seatwork
1) Write a program that allows the user to
convert either from degrees Celsius to
Fahrenheit or degrees Fahrenheit to Celsius.
Use the following formulas
degreesC = 5 ( degreesF – 32)/9
degreesF – (9(degreesC) /5) + 32
- Allow the user to select which to converts
- Use methods within main only