您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 12

The implementation of class operations in Java

Operations are implemented as Java methods.

Consider the following operations from a model of a class of BankAccount objects:

      getBalance tell us the balance
      setOverdraftLimit set a new overdraft limit
      overdraftLimitExceeded has the overdraft limit been exceeded?

Java Programming convention:

Method names start with a lower case first letter

As part of the object-oriented modelling process, each operation should be defined in terms of any arguments it may need, and also if, and what type, of reply the operation may return. 

The implementation of methods in Java involves writing Java program statements to specify:

      the return type, if any, of a method’s reply
      the name of the method
      a list of any arguments (and their types) required by the method
      the list of Java statements to be executed when the method is invoked

Consider possible implementation of the operation "getBalance" as a Java method:

      it does return a value, it returns the value of the "balance" variable. This reply is of type "float" (the same type as the "balance" variable)
      the name of the method is "getBalance"
      this method does not need any arguments
      the only action to be performed is to return a reply that is the value inside the "balance" variable

One possible implementation of the "getBalance" operation as a Java method is illustrated as follows:

file: BankAccount.java

class BankAccount

{

// variables

char accountNumber[];

String accountHolderName;

Date accountHolderDateOfBirth;

float balance;

int overdraftLimit;

// methods

float getBalance()

{

return balance;

}

<method2 defined here>

<methodN defined here>

}

Java Programming requirement:

All methods must specify a list of arguments in parentheses immediately after the method name. If no arguments are required, nothing is written between the parentheses. This is illustrated in the method "getBalance", written with no arguments "getBalance()".

Let us consider a possible method implementations of the remaining two operations:

operation: setOverdraftLimit

      it does not need to return any reply (this is indicated in Java with the Java term "void")
      the name of the method is "setOverdraftLimit"
      this method requires one argument, an integer value that is to be the new value of the "overdraftLimit" variable
      the only action to be performed is to overwrite the old value of the "overdraftLimit" variable with the value of the argument provided

operation: overdraftLimitExceeded

      it should return a reply of "true" or "false" (this is of the primitive type "boolean", explored detail in the next unit)
      the name of the method is "overdraftLimitExceeded"
      this method requires no arguments
      the action to be performed is to compare the value of the "balance" variable with the value of the "overdraftLimit" variable, and to return "true" is the balance is a larger negative number, and "false" otherwise

One possible implementation of the "setOverdraftLimit" and "overdraftLimitExceeded" operations as Java methods are illustrated as follows:

file: BankAccount.java

class BankAccount

{

// variables

char accountNumber[];

String accountHolderName;

Date accountHolderDateOfBirth;

float balance;

int overdraftLimit;

// methods

float getBalance()

{

return balance;

}

void setOverdraftLimit( int newLimit )

{

overdraftLimit = newLimit;

}

boolean overdraftLimitExceeded()

{

if( balance < overdraftLimit )

return true;

else

return false;

}

}

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg