Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using assertion to test the precondition

Tags:

java

I am working on a school assignment, I am supposed to use assertions to test the preconditions of the deposit method and the constructor. I figured out the method, but I am stuck on how to add to the constructor.

Here is what I have so far:

/**
   A bank account has a balance that can be changed by 
   deposits and withdrawals.
*/
public class BankAccount
{  
   private double balance;

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

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

   /**
      Deposits money into the bank account.
      @param amount the amount to deposit
   */
   public void deposit(double amount)
   {  
      assert amount >=0;

       double newBalance = balance + amount;
      balance = newBalance;
   }

   /**
      Withdraws money from the bank account.
      @param amount the amount to withdraw
   */
   public void withdraw(double amount)
   {   
      double newBalance = balance - amount;
      balance = newBalance;
   }

   /**
      Gets the current balance of the bank account.
      @return the current balance
   */
   public double getBalance()
   {   
      return balance;
   }
}
like image 634
Jose M. Avatar asked Aug 11 '13 00:08

Jose M.


People also ask

How do you properly use assert?

To properly use assertions as a debugging tool, you shouldn't use try … except blocks that catch and handle AssertionError exceptions. If an assertion fails, then your program should crash because a condition that was supposed to be true became false.

How do you check preconditions in Python?

Python provides a statement called the assert statement that can be used to check function preconditions. An assert statement checks the value of a boolean expression. If the expression is True , the assert statement allows the program to proceed normally.

What is the use of assertion?

The function of assertion is to let readers to feel that they should not disagree or dispute what they read or hear; rather, they should accept the idea or notion as an indisputable fact. It has proved to be one of the best approaches for writers to express their personal feelings, beliefs, and ideas in a direct way.


1 Answers

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

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

Creating any illegal BankAccount immediately results in an exception (if assertions are enabled).

Assertions can be placed practically everywhere in the code. They are for debugging purposes and will have no effect if you disable them (e.g. during production).

If the purpose of the bankaccount is to always be positive, you might want to add additional assertions, for example:

/**
   Withdraws money from the bank account.
   @param amount the amount to withdraw
*/
public void withdraw(double amount)
{   
    assert amount <= this.balance;

    this.balance -= amount;
}

Again, assertions are only for debugging, you should not try to catch them. Any assertion-exception indicates a fault in your program (or a fault in the assertion statement).

So the following should NOT be used:

try
{
    BankAccount newbankaccount = new BankAccount(5);
    newbankaccount.withdraw(6.0);
}
catch (Exception e)
{
    // illegal withdrawal
}

Instead, you should check the pre-conditions.

BankAccount newbankaccount = new BankAccount(5);
if (newbankaccount.getBalance() < 6.0)
{
    // illegal withdrawal
}
else
    newbankaccount.withdraw(6.0);

An assertion-exception should only fire if there is a logic error in your application.

like image 62
bas Avatar answered Sep 30 '22 06:09

bas