Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/******************************************************************** // Account

ID: 3906717 • Letter: #

Question

/********************************************************************
// Account.java Author: Lewis/Loftus
//
// Represents a bank account with basic services such as deposit
// and withdraw.
//********************************************************************

import java.text.NumberFormat;

public class Account
{
private final double RATE = 0.035; // interest rate of 3.5%

private long acctNumber;
private double balance;
private String name;

//-----------------------------------------------------------------
// 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 + " " + name + " " + fmt.format(balance));
}
}

Modify the Account class from Chapter 4 so that it also permits an account to be opened with just a name and an account num ber, assuming an initial balance of zero. Modify the main method of the Transactions class to demonstrate this new capability. PP 7.1

Explanation / Answer

Modified Account.java

/********************************************************************

// Account.java Author: Lewis/Loftus

//

// Represents a bank account with basic services such as deposit

// and withdraw.

//********************************************************************/

import java.text.NumberFormat;

public class Account

{

              private final double RATE = 0.035; // interest rate of 3.5%

              private long acctNumber;

              private double balance;

              private String name;

              //-----------------------------------------------------------------

              // 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;

              }

              //-----------------------------------------------------------------

              // Sets up the account by defining its owner, account number,

              // and assuming initial balance as 0.

              //-----------------------------------------------------------------

              public Account(String owner, long account)

              {

                             name = owner;

                             acctNumber = account;

                             balance = 0;

              }

              //-----------------------------------------------------------------

              // 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 + " " + name + " " + fmt.format(balance));

              }

}