0% found this document useful (0 votes)
4 views6 pages

Type Name (Parameter-List) : (//body of Method)

The document discusses methods in classes. It explains that methods consist of a return type, name, parameters, and body. It provides an example of a Box class with a void volume() method that prints the volume. It then adds a version that returns a double by having the volume() method return width*height*depth instead of printing it. This allows storing and printing the returned volume separately.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Type Name (Parameter-List) : (//body of Method)

The document discusses methods in classes. It explains that methods consist of a return type, name, parameters, and body. It provides an example of a Box class with a void volume() method that prints the volume. It then adds a version that returns a double by having the volume() method return width*height*depth instead of printing it. This allows storing and printing the returned volume separately.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introducing Methods

We know that classes consist of two things: instance variable and methods.

This is general form of a method:

type name(parameter-list)
{
//body of method
}
Here type specifies the type of data returned by method. This can be any valid type, including class types
that we create. If the method does not return a value, its return type must be void.

The parameter list is a sequence of type and identifiers pairs separated by commas. Parameters are
essentially variables that receive the value of the argument passed to the method when it is called. If the
method has no parameters, then the parameter list will be empty.

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.

We need to study now how to create various types of methods, including those that take parameters
and those that returns values.

We must add a method to Box.


//This program includes a method inside the box class.
class Box
{
double width;
double height;
double depth;
//display volume of a box
void volume()
{
[Link](“ volume is”);
[Link]( width*height*depth);
}
}
class BoxDemo3{
public static void main(String args[]){
Box mybox1 = new Box();
Box mybox2= new Box();
//assign values to mybox1’s instance variables
[Link]=10;
[Link]=20;
[Link]=15;
/ * assign different values to mybox2 ‘s instance variables */
[Link]=3;
[Link]=6;
[Link]=9;
//display volume of first box
[Link]();
//display volume of second box
[Link]();
}
}
The program generates the following output, which is the same
as the previous version
Volume is 3000.0;
Volume is 162.0

Explanation:
[Link]();

[Link]();

The first line here invokes the volume() method on mybox1. That is, it calls volume() relative to the
mybox1 object, using the object’s name followed by the dot operator.

Thus, the call to [Link]() displays the volume of the box defined by mybox1, and the call to
[Link]() displays the volume of the box defined by mybox2. Each time volume () is invoked, it
displays the volume for the specified box.

When [Link]() is executed, the Java run time system transfers control to the code defined
inside volume(). After the statement inside volume() have executed, control is returned to the calling
routine, and execution resumes with the line of code following call.

Note: When an instance variable is accessed by code that is not part of the class in which instance
variable is defined, it must be done through an object, by use of the dot operator. However, when an
instance is accessed by the code that is a part of the same class as the instance variable, that variable
can be referred to directly. The same thing applies to methods.
Returning a Value

class Box

double width;

double height;

double depth;

//compute and return volume of a box

double volume()

return width*height*depth;

class BoxMethod2

public static void main(String args[])

Box mybox1 = new Box();

Box mybox2= new Box();

double vol;

//assign values to mybox1’s instance variables

[Link]=10;

[Link]=20;
[Link]=15;

// assign different values to mybox2 instance variables

[Link]=3;

[Link]=6;

[Link]=9;

//get volume of first box

vol=[Link]();

[Link]( vol);

//display volume of second box

[Link]();

vol=[Link]();

[Link]( vol);

You might also like