- Implementing an
object is to:
- Represent the
state information of the
object accurately
with
(instance) variables,
and
- Represent the
operations on the object
accurately with
(instance) methods
|
- Example:
- Bank Account object
- Has an account number
- Has an owner name
- Has a balance
- You can deposit some money into
it
- You can withdraw some money from
it
|
- Implementation of a
Bank Account object
public class BankAccount
{
public int accNum; // These variables are used to
public String name; // store state information on
public double balance; // "BankAccounts"
/* ==============================================
deposit(amount): Add "amount" to balance
============================================== */
public void deposit( double amount )
{
balance += amount;
}
/* ======================================================
withdraw(amount): Subtract "amount" from balance
====================================================== */
public void withdraw( double amount )
{
if ( balance >= amount )
balance -= amount; // Subtract "amount" from balance
}
/* ==============================================
convToString(): return a String containing
information of BankAccount
============================================== */
public String convToString( )
{
return( "Account number: " + accNum
+ ", Name: " + name
+ ", Balance: " + balance);
}
}
|
|
|