4.2 Class with Methods

As we have already mentioned, as well as member variables defining its state, a class can have methods defining its behavior. A method is similar to a procedure or function in a nonobject-oriented programming language. For our bank account class, examples of methods are deposit and withdraw. The following is code for the deposit method that increases the balance by the amount deposited:

public void deposit(double amount)
{
balance = balance + amount;
}


The method declaration is of the form

access_modifier return_type identifier (arg1_type arg1_name, ...)

The access modifier for the deposit method example is public. This means that any class can access the deposit method. We will look at access modifiers in Chapter 5. The method return type can be any Java data type. If the method does not return a value, use the keyword void as though void were a data type. Since our deposit method does not return a value, we use void. The method identifier itself can be any valid Java identifier. Optionally, a method can have any number of arguments preceded by their data types. In
the deposit method, we have one argument, amount, which is of type double. A method may have no arguments, in which case add () after the method identifier. For example,

public void clearBalance()
{
balance = 0;
}


The method body is enclosed in braces, { and }, and can have any number of statements. In the deposit method, we have just one statement

balance = balance + amount;

balance is an Account class member variable, so still is in scope in the deposit method. amount is a variable local to the deposit method, so cannot be accessed outside the deposit method.In the case of a withdrawal from our bank account, the balance will be decreased by the amount withdrawn. If the resulting balance is less than zero, an error message is printed and no withdrawal is made. We would also like our method to return the value of the outstanding balance.

The Java code for the withdraw method follows:

public double withdraw(double amount)
{
if (balance - amount < 0)
{
System.out.println(''Insufficient Funds");
}
Else
{
balance = balance - amount;
}
return balance;
}



Note the return statement in the withdraw method. A return statement is used to exit from a method. Control passes to the statement following the one that invoked the method.
If a method is void, use the statement

return;

to exit from the method. Note that control passes to the invoking method after the last statement in the current method, so a return statement is required in a void method only if we wish to prematurely exit from the method if some condition is met. For methods other than void, we need to use the statement

return expression;

where expression is the same data type as the current method's return type. For non-void methods, the last statement in the method must be a return statement.

Note that since the withdraw method returns the balance, which is an Account class member variable of type double, the withdraw method is of type double. The Account class in this section now includes the deposit and withdraws methods.

Account

class Account
{
int accountNo;
String accountName;
double balance;
public void deposit(double amount)
{
balance = balance + amount;
}
public double withdraw(double amount)
{
if (balance - amount < 0)
{
System.out.println("Insufficient Funds");

}
Else
{
balance = balance - amount;
}
return balance;
}
}



The syntax for invoking a method is

object_identifier.method(optional arguments);

An example of statements invoking the deposit and withdraw methods of the
Account class is

fredsAccount.deposit(100);
amountLeft = fredsAccount.withdraw(120);


Where amountLeft is a variable of type double.

No comments:

Post a Comment