Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class BankAccount { private double balance; private int count; /** * Constructs a bank account with a zero balance. */ BankAccount() { balance =

image text in transcribed

public class BankAccount { private double balance; private int count;

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

/** * Constructs a bank account with a given balance. * * @param initialBalance the initial balance */ BankAccount(double initialBalance) { balance = initialBalance; }

/** * Deposits money into the bank account and charges the fee * for the transaction. * * @param amount the amount to deposit * @param feePercent the fee percent for the transaction */ void deposit(double amount, double feePercent) { balance = balance + amount; // balance = balance - (balance * feePercent / 100); count++; }

/** * Withdraws money from the bank account and charges the fee * for the transaction. * * @param amount the amount to withdraw * @param feePercent the fee percent for the transaction */ void withdraw(double amount, double feePercent) { balance = balance - amount; // balance = balance - (balance * feePercent / 100); count++; }

/** * Deducts monthly charge for transactions exceeding free allotment and * resets the transactions count. * * @param freeCount number of transactions allowed with no charge * @param feePercent fee percent for transaction exceeding free allotment */ void deductMonthlyCharge(int freeCount, double feePercent) { int charge = Math.max(count, freeCount) - freeCount; for (int i = 0; i

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

Problem 2 Enhance the BankAccount class and see how abstraction and encapsulation enable evolutionary changes to software: Begin with a simple enhancement: charging a fee for every deposit and withdrawal. Supply a mechanism for setting the fee and modify the deposit and withdraw methods so that the fee is levied. The initial transaction fee is 50 cents, and this can be changed later on. Test your class and check that the fee is computed correctly. Now make a more complex change. The bank will allow a fixed number of free transactions (deposits and withdrawals) every month, and charge for transactions exceeding the free allotment. The charge is not levied immediately but at the end of the month. The initial value for the number of free transactions is 10. Supply a new method deductMonthlycharge to the BankAccount class that deducts the monthly charge and resets the transaction count. (Hint: Use Math.max( actual transaction count , free transaction count) in your computation) Produce a test program that verifies that the fees are calculated correctly over several months

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions