Wednesday, January 20, 2016

Topic 11: Class Methods in Java

This is the general form of a method:

type name(parameter-list) {
// body of method
}

Here, type specifies the type of data returned by the 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. 

// This program includes a method inside the box class.
class Box
{
double width, height, depth;
// display volume of a box
void volume()
{
System.out.println("Volume is " + 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
mybox1.width = mybox1.height = mybox1.depth = 15;
/* assign different values to mybox2's instance variables */
mybox2.width = mybox2.height = mybox2.depth = 9;
// display volumes
mybox1.volume();
mybox2.volume();

}
}

When a method uses an instance variable that is defined by its class, it does so directly, without explicit reference to an object and without use of the dot operator, i.e. the reason why 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. The same thing applies to methods.

 

Returning a Value

Replace the volume() defined in preceding program to return a value:

// Now, volume() returns the volume of a box.
double volume()
{
return width * height * depth;
}

The following statement in the body of the main class, executes the value of mybox1.volume( ) and its value is then stored in vol.

vol = mybox1.volume();

There are two important things to understand about returning values:

  • The type of data returned by a method must be compatible with the return type specified by the method.

  • The variable receiving the value returned by a method must also be compatible with the return type specified for the method.


Parameterized method

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. An argument is a value that is passed to a method when it is invoked.

A method that takes the dimensions of a box in its parameters and sets each instance variable appropriately. This concept is implemented by the following program:

// In class Box
double volume()
{
return width * height * depth;
}
// sets dimensions of box
void setDim(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}


// In the main method
Box mybox1 = new Box();
Box mybox2 = new Box();
// initialize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);


// get volumes
System.out.println("Volume is " + mybox1.volume());
System.out.println("Volume is " + mybox2.volume());


Click for NEXT article.

 

Please feel free to correct me by commenting your suggestions and feedback.

No comments:

Post a Comment