Advertisement
pellekrogholt

Untitled

May 2nd, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. /**
  2.  *
  3.  * This example is based in the jml 4 page
  4.  * it should fail at RAC
  5.  *
  6.  * - within the jml2 nothing happens
  7.  *
  8.  */
  9. public class Account {
  10.    
  11.   private /*@ spec_public @*/ int bal;
  12.   //@ public invariant bal >= 0;
  13.  
  14.   /*@ requires amt >= 0;
  15.     @ assignable bal;
  16.     @ ensures bal == amt; @*/
  17.   public Account(int amt) {
  18.     bal = amt;
  19.   }
  20.  
  21.   /*@ assignable bal;
  22.     @ ensures bal == acc.bal; @*/
  23.   public Account(Account acc) {
  24.     bal = acc.balance();
  25.   }
  26.  
  27.   /*@ requires amt > 0 && amt <= acc.balance();
  28.     @ assignable bal, acc.bal;
  29.     @ ensures bal == \old(bal) + amt
  30.     @   && acc.bal == \old(acc.bal - amt); @*/
  31.   public void transfer(int amt, Account acc) {
  32.     acc.withdraw(amt);
  33.     deposit(amt);
  34.   }
  35.  
  36.   /*@ requires amt > 0 && amt <= bal;
  37.     @ assignable bal;
  38.     @ ensures bal == \old(bal) - amt; @*/
  39.   public void withdraw(int amt) {
  40.     bal -= amt;
  41.   }
  42.  
  43.   /*@ requires amt > 0;
  44.     @ assignable bal;
  45.     @ ensures bal == \old(bal) + amt; @*/
  46.   public void deposit(int amt) {
  47.     bal += amt;
  48.   }
  49.  
  50.   //@ ensures \result == bal;
  51.   public /*@ pure @*/ int balance() {
  52.     return bal;
  53.   }
  54.    
  55.     public static void main(String[] args) {
  56.         Account acc = new Account(100);
  57.         acc.withdraw(200);
  58.         System.out.println("Balance after withdrawal: " + acc.balance());
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement