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

Add a condition to the deposit method of the class BankAccount class, restrictin

ID: 3767976 • Letter: A

Question

Add a condition to the deposit method of the class BankAccount class, restricting deposits to $100,000.00 (the insurance limit of the U.S. government). The method should block until the sufficient money have been withdrawn by another thread. Test your program with a large number of deposit threads.

/**
This program runs threads that deposit and withdraw
money from the same bank account.
*/
public class BankAccountThreadRunner
{
public static void main(String[] args)
{
BankAccount account = new BankAccount();
final double AMOUNT = 10000;
final int REPETITIONS = 10;
final int DEPOSIT_THREADS = 10;
final int WITHDRAW_THREADS = 2;

      for (int i = 0; i < DEPOSIT_THREADS; i++)
{
DepositRunnable d = new DepositRunnable(account, AMOUNT, REPETITIONS);
Thread t = ____________________;
__________________
}

for (int i = 0; i < WITHDRAW_THREADS; i++)
{
WithdrawRunnable d = new WithdrawRunnable(account, AMOUNT, REPETITIONS * DEPOSIT_THREADS / WITHDRAW_THREADS);
Thread t ____________
_____________________________
}
}
}

public class BankAccount
{
public static final double MAX_BALANCE = 100000;

   private double balance;
private Lock balanceChangeLock;
private Condition sufficientFundsCondition;
private Condition lessThanMaxBalanceCondition;

   /**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
......
......
}

   /**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
throws InterruptedException
{
______________________________________________
try
{
while (balance + amount > MAX_BALANCE)
____________________________________________
System.out.print("Depositing " + amount);
double newBalance = balance + amount;
System.out.println(", new balance is " + newBalance);
balance = newBalance;
____________________________________________________
}
finally
{
_________________________________________
}
}

   /**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
throws InterruptedException
{
_____________________________________
try
{
while (balance < amount)
______________________________________________
System.out.print("Withdrawing " + amount);
double newBalance = balance - amount;
System.out.println(", new balance is " + newBalance);
balance = newBalance;
__________________________________________________
}
finally
{
_______________________________________
}
}

   /**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}

/**
A deposit runnable makes periodic deposits to a bank account.
*/
public class DepositRunnable _____________________________
{
private static final int DELAY = 1;
private BankAccount account;
private double amount;
private int count;

   /**
Constructs a deposit runnable.
@param anAccount the account into which to deposit money
@param anAmount the amount to deposit in each repetition
@param aCount the number of repetitions
*/
public DepositRunnable(BankAccount anAccount, double anAmount,
int aCount)
{
account = anAccount;
amount = anAmount;
count = aCount;
}

   public void run()
{
try
{
for (int i = 1; i <= count; i++)
{
____________________________
__________________________________
}
}
catch (InterruptedException exception) {}
}
}

/**
A withdraw runnable makes periodic withdrawals from a bank account.
*/
public class WithdrawRunnable implements Runnable
{
private static final int DELAY = 1;
private BankAccount account;
private double amount;
private int count;

   /**
Constructs a withdraw runnable.
@param anAccount the account from which to withdraw money
@param anAmount the amount to deposit in each repetition
@param aCount the number of repetitions
*/
public WithdrawRunnable(BankAccount anAccount, double anAmount,
int aCount)
{
account = anAccount;
amount = anAmount;
count = aCount;
}

   public void run()
{
try
{
for (int i = 1; i <= count; i++)
{
______________________________
__________________________
}
}
catch (InterruptedException exception) {}
}
}

Explanation / Answer

public class BankAccountThreadRunner
{
public static void main(String[] args)
{
   BankAccount account = new BankAccount();
final double AMOUNT = 100 ;
final int REPETITIONS = 100 ;
final int THREADS = 100 ;
for ( int i = 1; i <= THREADS; i++)
{
DepositRunnable d = new DepositRunnable( account, AMOUNT, REPETITIONS);
WithdrawRunnable w = new WithdrawRunnable( account, AMOUNT, REPETITIONS);
Thread dt = new Thread(d);
Thread wt = new Thread(w);
dt.start();
wt.start();
       }
     }
}

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/** A bank account has a balance that can be simultaneously changed by
deposits and withdrawals.
*/
public class BankAccount
   {
    private double balance;
     private Lock balanceChangeLock;
     private Condition sufficientFundsCondition;
  
/** Constructs a bank account with a zero balance. */
      public BankAccount()
    {
        balance = 0 ;
        balanceChangeLock = new ReentrantLock();
        sufficientFundsCondition = balanceChangeLock.newCondition();
      }

/**
     Deposits money into the bank account.
      @param amount the amount to deposit
*/
public void deposit( double amount)
{
                balanceChangeLock.lock();
                try
                {
                System.out.print( "Depositing " + amount);
                double newBalance = balance + amount;
                System.out.println( ", new balance is " + newBalance);
                balance = newBalance;
                sufficientFundsCondition.signalAll();
                }
        finally
        {
        balanceChangeLock.unlock();
        }
}

/** Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw( double amount)
throws InterruptedException
{
balanceChangeLock.lock();
try
   {
    while (balance < amount)
     sufficientFundsCondition.await();
      System.out.print( "Withdrawing " + amount);
      double newBalance = balance - amount;
       System.out.println( ", new balance is " + newBalance);
       balance = newBalance;
}
finally
   {
    balanceChangeLock.unlock();
   }
}

/** Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
   return balance;
    }
}

Program Run:
Depositing 100.0, new balance is 100.0
Withdrawing 100.0, new balance is 0.0
Depositing 100.0, new balance is 100.0
Depositing 100.0, new balance is 200.0
...
Withdrawing 100.0, new balance is 100.0
Depositing 100.0, new balance is 200.0
Withdrawing 100.0, new balance is 100.0
Withdrawing 100.0, new balance is 0.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote