Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Java Methods

The document provides an overview of methods in Java programming, detailing their structure, including return types, parameters, and method bodies. It explains how methods are defined within classes, how they can return values, and the distinction between parameters and arguments. Additionally, it includes examples of method usage and exercises for practical application.

Uploaded by

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

Java Methods

The document provides an overview of methods in Java programming, detailing their structure, including return types, parameters, and method bodies. It explains how methods are defined within classes, how they can return values, and the distinction between parameters and arguments. Additionally, it includes examples of method usage and exercises for practical application.

Uploaded by

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

JAVA Programming

Methods

1
Introducing Methods

• Classes usually consist of two things: instance variables and


methods.
• General format
type name(parameter-list) {
// body of method
}

2
Method Definitions
• Methods belong to a class
• Defined inside the class
• Heading
• Method Return type (e.g. int, float, void)
• Method Name (e.g. nextInt, println)
• Method Parameters (e.g. println(…) )
• More…
• Body
• enclosed in braces {}.
• Declarations and/or statements.

3
Method Definitions
• Type specifies the type of data returned by the method.
• If the method does not return a value, its return type
must be void.
• The name of the method must be specified .
• The parameter-list is a sequence of type and identifier
pairs separated by commas.
• Parameters are essentially variables that receive the
value of the arguments passed to the method when it
is called.
• If the method has no parameters, then the parameter
list will be empty.

4
Method Definitions
• Methods that have a return type other than void return a value to the
calling routine using the following form of the return statement:
return value;
• Here, value is the value returned.

5
Adding Method to Box Program

6
Adding Method to Box Program

Output:

7
Adding Method to Box Program
mybox1.volume();
mybox2.volume();
• When mybox1.volume( ) is executed, the Java run-time system
transfers control to the code defined inside volume( ).
• After the statements inside volume( ) have executed, control is
returned to the calling routine, and execution resumes with the
line of code following the call.
• A method is Java’s way implementing subroutines.

8
Adding Method to Box Program
• There is something very important to notice inside the volume( ) method:
the instance variables width, height, and depth are referred to directly,
without preceding them with an object name or the dot operator.
• A method is always invoked relative to some object of its class. Once this
invocation has occurred, the object is known.
• Thus, within a method, there is no need to specify the object a second
time.
• This means that width, height, and depth inside volume( ) implicitly refer
to the copies of those variables found in the object that invokes volume(
).
9
Adding Method to Box Program
When an instance variable is accessed by code
that is not part of the class in which that instance
variable is defined, it must be done through an
object, by use of the dot operator.
However, when an instance variable is accessed by
code that is part of the same class as the instance
variable, that variable can be referred to directly.
10
Adding Method that return value

11
Adding Method that return value

12
Adding Method that return value
• There are two important things to understand about returning values:

1. The type of data returned by a method must be compatible with


the return type specified by the method. For example, if the return
type of some method is boolean, you could not return an integer.
2. The variable receiving the value returned by a method (such as vol,
in this case) must also be compatible with the return type specified
for the method.

13
Adding a Method That Takes Parameters
• Parameters allow a method to be generalized.
• That is, a parameterized method can operate on a variety of data
and/or be used in a number of slightly different situations.

• This method returns the value of 10 squared, its use is very limited.

14
Adding a Method That Takes Parameters

• square( ) is now a general-purpose method that can


compute the square of any integer value, rather than
just 10.
int x, y;
x = square(5); // x equals 25
x = square(9); // x equals 81
y = 2;
x = square(y); // x equals 4
15
Adding a Method That Takes Parameters
• It is important to keep the two terms parameter and argument
straight.
• A parameter is a variable defined by a method that receives a
value when the method is called.
• For example, in square( i), i is a parameter.
• An argument is a value that is passed to a method when it is
invoked.
• For example, square(100) passes 100 as an argument.
• Inside square(i ), the parameter i receives that value

16
Adding a Method That Takes Parameters

17
Adding a Method That Takes Parameters

18
Example1 class Account {
int accno;
int balance;

void SetData(int acno, int bal) {


accno=acno;
balance=bal;
}
void ShowData() {
System.out.println("account number is "+accno);
System.out.println("balance is "+balance); }
}
class AccountDemo{
public static void main(String args[]){
Account c1=new Account();
c1.SetData(2,300);
c1.ShowData();
}} 19
class AccountDemo{
Example 2
public static void main(String args[]){
import java.io.*; Account c1=new Account();
class Account { Account c2=new Account();
int accno; int balance; Account c3=new Account();
void SetData(int acno, int bal) { c1.SetData(100,1500);
accno=acno;balance=bal; } c2.SetData(101,2500);
void ShowData() { c3.SetData(102,3500);
System.out.println("account number is Account customers[]=new Account[3];
"+accno); customers[0]=c1; customers[1]=c2;
System.out.println("balance is customers[2]=c3;
"+balance); for(int i=0;i<3;i++)
} customers[i].ShowData();
} c1.ShowData(); c2.ShowData();
c3.ShowData();
}} 20
Exercise
Use methods and parameters in the following exercise:
1. Create a class named Rectangle and create two objects and
determine the area of the rectangle.
2. Create a class named rainfall. Get the daily rainfall in mm for the
week and find their average rainfall for a week. classify intensity of
rainfall received in a week based on their average rainfall for a week.
The intensity of rainfall is as follows:
Classification of rainfall Average Rain fall in mm
(R)
No Rain <= 0
Light 0 < R <= 10.0
Moderate 10.0 < R < 35.5
Rather Heavy 35.5 < R <= 64.4
Heavy 64.4 < R <= 124.4
Very Heavy R > 124.4 21

You might also like