0% found this document useful (0 votes)
14 views

LESSON 6 B Methods-PassByValue

Here is a program that allows the user to convert between Celsius and Fahrenheit using methods: import java.util.Scanner; public class TemperatureConverter { public static double celsiusToFahrenheit(double celsius) { return (9.0/5) * celsius + 32; } public static double fahrenheitToCelsius(double fahrenheit) { return (5/9.0) * (fahrenheit - 32); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter 1 to convert from Celsius to Fahrenheit or 2 to convert from Fahrenheit to Celsius: "); int

Uploaded by

Nezuko
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

LESSON 6 B Methods-PassByValue

Here is a program that allows the user to convert between Celsius and Fahrenheit using methods: import java.util.Scanner; public class TemperatureConverter { public static double celsiusToFahrenheit(double celsius) { return (9.0/5) * celsius + 32; } public static double fahrenheitToCelsius(double fahrenheit) { return (5/9.0) * (fahrenheit - 32); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter 1 to convert from Celsius to Fahrenheit or 2 to convert from Fahrenheit to Celsius: "); int

Uploaded by

Nezuko
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Basic Method 2

JAVA LECTURE PRESENTATION


Concept of pass-by-value

In relation to using parameters in the


method
Pass-by-value
• Pass the value to be processed inside the
method
• Used as parameters/arguments
• order and type of parameter matters in Java
• If there are 3 formal parameters(method
definition), it is expected that there are also 3
actual parameters to be used in the method
call
Example #1

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

• methodName – same name in the method


header
• Parameters – type and the number of
parameters
Example:
• Method header

• 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

Any return type


With parameters
Method Header

• <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

You might also like