Question

Question 2 Programming by Contract (a) Design a BankAccount class for maintaining bank balances. Each bank account should have a current balance and methods implementing deposits and withdrawals; however money can only be withdrawn from an account if there are sufficient funds. Each account has a cash withdrawal limit of $800 per day. Give Java implementations of the methods in your design. Explain how your implementation enforces the daily limit on withdrawals. (b) Give preconditions and postconditions for each method in the BankAccount class and provide a rigorous argument that balance 20 is a class invariant. Explain how your code is consistent with your pre- and postconditions
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class BankAccount

{

public static void main ( String [] args )

{

BankAccount myAccount = new BankAccount( 0.0 );

myAccount.deposit( 500.00 );

System.out.println( "Current balance is " + myAccount.getBalance() );

myAccount.withdraw( 100.00 );

}

/** ********** Implementation starts here ********************* */

/**

* Class Invariant: balance >= 0.0

*/

private double balance = 0.0;

/**

* Create a new bank account object, with some initial balance.

*

* @param amount the amount to initially deposit

* Pre-condition: initialBalance >= 0.0

*/

public BankAccount ( double initialBalance )

{

if ( initialBalance < 0.0 )

throw new IllegalArgumentException( "Initial balance ("

+ initialBalance + ") must be >= 0" );

balance = initialBalance;

assert balance >= 0.0 : "invariant violation: balance = " + balance;

}

/**

* @param amount the amount to deposit

* Pre-condition: amount >= 0.0

*/

public void deposit ( double amount )

{

if ( amount < 0.0 )

throw new IllegalArgumentException( "Deposit amount (" + amount

+ ") must be >= 0" );

double newBalance = balance + amount;

assert newBalance >= balance : "post-condition violation: new balance "

+ "(" + newBalance + ") < current balance (" + balance + ")";

balance = newBalance;

assert balance >= 0.0 : "invariant violation: balance = " + balance;

}

/**

* @param amount the amount to deposit

* Pre-condition: amount >= 0.0

*/

public void withdraw( double amount )

{

if ( amount < 0.0 )

throw new IllegalArgumentException( "Withdraw amount (" + amount

+ ") must be >= 0" );

if ( amount > balance )

throw new IllegalArgumentException( "Withdraw amount (" + amount

+ ") must be <= current balance (" + balance + ")" );

balance -= amount;

assert balance >= 0.0 : "invariant violation: balance = " + balance;

}

/**

* @param amount the amount to deposit

* Pre-condition: amount >= 0.0

*/

public double getBalance ()

{

calculateInterest();

assert balance >= 0.0 : "invariant violation: balance = " + balance;

return balance;

}

/**

* Post-condition: new balance >= old balance

*/

private void calculateInterest ()

{

double newBalance = balance * .05;

assert newBalance >= balance : "post-condition violation: new balance "

+ "(" + newBalance + ") < current balance (" + balance + ")";

balance = newBalance;

assert balance >= 0.0 : "post-condition violation: balance = " + balance;

}

}

Add a comment
Know the answer?
Add Answer to:
Question 2 Programming by Contract (a) Design a BankAccount class for maintaining bank balances. Each bank...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • BankAccount and SavingsAccount Classes (JAVA)

    Must be in GUI interfaceDesign an abstract class namedBankAccountto hold the following data for a bankaccount:* Balance* Number of deposits this month* Number of withdrawals (this month)* Annual interest rate* Monthly service chargesThe class should have the following methods:Constructor: The constructor should accept arguments for the balance and annual interest rate.deposit: A method that accepts an argument for the amount of the deposit. The methods should add the argument to the account balance. It should also increment thevariable holding the...

  • Design a class named BankAccount containing the following data field and methods. • One double data...

    Design a class named BankAccount containing the following data field and methods. • One double data field named balance with default values 0.0 to denote the balance of the account. • A no-arg constructor that creates a default bank account. • A constructor that creates a bank account with the specified balance.  throw an IllegalArgumentException when constructing an account with a negative balance • The accessor method for the data field. • A method named deposit(double amount) that deposits...

  • C++ Design a class bankAccount that defines a bank account as an ADT and implements the...

    C++ Design a class bankAccount that defines a bank account as an ADT and implements the basic properties of a bank account. The program will be an interactive, menu-driven program. a. Each object of the class bankAccount will hold the following information about an account: account holder’s name account number balance interest rate The data members MUST be private. Create an array of the bankAccount class that can hold up to 20 class objects. b. Include the member functions to...

  • The program needs to be in python : First, create a BankAccount class. Your class should...

    The program needs to be in python : First, create a BankAccount class. Your class should support the following methods: class BankAccount (object): """Bank Account protected by a pin number.""" def (self, pin) : init "n"Initial account balance is 0 and pin is 'pin'."" self.balance - 0 self.pin pin def deposit (self, pin, amount): """Increment account balance by amount and return new balance.""" def withdraw (self, pin, amount): """Decrement account balance by amount and return amount withdrawn.""" def get balance...

  • Design and implement the following 3 classes with the exact fields and methods (these names and c...

    Design and implement the following 3 classes with the exact fields and methods (these names and caps exactly): 1. An abstract class named BankAccount (java file called BankAccount.java) Description Filed/Method Balance NumberDeposits NumberWithdrawals AnnualInterestRate MonthlyServiceCharge BankAccount SetHonthlyServiceCharges A method that accepts the monthly service charges as an argument and set the field value GetBalance GetNum berDeposits GetNum berWithdrawals GetAnnualinterestRate GetMonthlyServiceCharge A method that returns the monthly service charge Deposit field for the bank account balance A field for the number...

  • c++ please    Define the class bankAccount to implement the basic properties of a bank account....

    c++ please    Define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member func- tions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 compo- nents of...

  • In this, you will create a bank account management system according to the following specifications: BankAccount...

    In this, you will create a bank account management system according to the following specifications: BankAccount Abstract Class contains the following constructors and functions: BankAccount(name, balance): a constructor that creates a new account with a name and starting balance. getBalance(): a function that returns the balance of a specific account. deposit(amount): abstract function to be implemented in both Checking and SavingAccount classes. withdraw(amount): abstract function to be implemented in both Checking and SavingAccount classes. messageTo Client (message): used to print...

  • pls write psuedocode write UML CODE ALSO example 3 files 1 for abstract 1 for bank...

    pls write psuedocode write UML CODE ALSO example 3 files 1 for abstract 1 for bank account and 1 for savings accounts due in 12 hours Lab Assignment 4a Due: June 13th by 9:00 pm Complete the following Programming Assignment. Use good programming style and all the concepts previously covered. Submit the java files electronically through Canvas by the above due date in 1 Zip file Lab4.Zip. (This is from the chapter on Inheritance.) 9. BankAccount and SavingsAccount Classes Design...

  • Design a class named BankAccount that contains: 1. A private int data field named accountId for...

    Design a class named BankAccount that contains: 1. A private int data field named accountId for the account. 2. A private double data field named accountBalance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A private int data field named numberOfDeposits...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT