Coding Example
Coding Example
1 Ques&on:
The Account class, shown in the following image, represents a basic bank account.
It contains instance data representing the account number, the account’s current
balance, and the name of the account’s owner. Note that instance data can be an
object reference variable (not just a primitive type), such as the account owner’s
name, which is a reference to a String object. The interest rate for the account is
stored as a constant.
The constructor of the Account class accepts three parameters that are used to
initialize the instance data. The deposit() and withdraw() methods perform the basic
transactions on the account, adjusting the balance based on the parameters. There
is also an addInterest() method that updates the balance by adding in the interest
earned. These methods represent valid ways to change the balance, so a classic
mutator such as setBalance() is not provided.
Modify the Account class so that it also permits an account to be opened with just a
name and an account number, assuming an initial balance of zero. Modify the
main() method of the Transactions() class to demonstrate this new capability.
2 Code
/* Account.java */
import java.text.NumberFormat;
//-----------------------------------------------------------------
// Sets up the account using the specified data and an initial
// balance of 0.
//-----------------------------------------------------------------
public Account(String owner, long account)
{
name = owner;
acctNumber = account;
balance = 0.0;
}
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account(String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
//-----------------------------------------------------------------
// Deposits the specified amount into the account. Returns the
// new balance.
//-----------------------------------------------------------------
public double deposit(double amount)
{
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Withdraws the specified amount from the account and applies
// the fee. Returns the new balance.
//-----------------------------------------------------------------
public double withdraw(double amount, double fee)
{
balance = balance - amount - fee;
return balance;
}
//-----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of the account.
//-----------------------------------------------------------------
public double getBalance()
{
return balance;
}
//-----------------------------------------------------------------
// Returns a one-line description of the account as a string.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
}
//********************************************************************
/* Transac1ons.java */
public class Transactions
{
//-----------------------------------------------------------------
// Creates some Account objects to demonstrate the overloaded
// constructor.
//-----------------------------------------------------------------
public static void main(String[] args)
{
Account3 acct1 = new Account3("Ted Murphy", 72354, 102.56);
Account3 acct2 = new Account3("Jane Smith", 69713);
Account3 acct3 = new Account3("Edward Demsey", 93757);
acct1.deposit(25.85);
acct1.addInterest();
acct2.addInterest();
acct3.addInterest();
System.out.println();
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
}
}