You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+8-24Lines changed: 8 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,35 +96,19 @@ Now we get an error! LiquidJava is telling us that the implementation of the met
96
96
97
97
#### Exercise
98
98
99
-
> Open [Bank.java](./src/main/java/com/tutorial/part1/exercise/Bank.java).
99
+
> Open [Counter.java](./src/main/java/com/tutorial/part1/exercise/Counter.java).
100
100
101
-
This class simulates a simple bank account with two methods: `deposit` and `withdraw`. In the `main` method, we simulate a wrong usage of the `deposit` and `withdraw` methods of a bank account, since it tries to withdraw more money than the current balance. Let's make use of LiquidJava refinements to ensure the correct usage of these methods.
101
+
This class simulates a simple counter with two methods: `increment` and `decrement`. In the `main` method, we simulate a wrong usage of the `decrement` method, since it tries to decrement the counter below zero. Let's make use of LiquidJava refinements to ensure the correct usage of these methods.
102
102
103
103
> Replace the `"true"` refinements with the appropriate ones to ensure the correct behavior of both methods.
104
104
105
-
- The `balance` parameter of both methods should be non-negative.
106
-
- The `amount` parameter of the `deposit` method should be greater than zero.
107
-
- The `amount` parameter of the `withdraw` method should be greater than zero and less than or equal to the `balance`.
108
-
- The return value of the `deposit` method should be equal to the sum of `balance` and `amount`.
109
-
-The return value of the `withdraw` method should be equal to the difference between `balance` and `amount`.
105
+
- The `count` parameter of the `increment` method should be non-negative.
106
+
- The `count` parameter of the `decrement` method should be greater than zero.
107
+
- The return value of the `increment` method should be equal to `count + 1`.
108
+
- The return value of the `decrement` method should be equal to `count - 1`.
109
+
-Use `_` or `return` to refer to the return value in the refinements.
110
110
111
-
With the correct refinements in place, LiquidJava will report an error in the `withdraw` method call, since it tries to withdraw more money than it was deposited.
112
-
113
-
> Modify the `withdraw` method call to withdraw `10` or less to fix the error.
114
-
115
-
However, notice that we are repeating the same refinement twice in the `balance` parameter of both methods. For this, we can use a refinement aliases to define commonly used refinements and avoid repetition.
116
-
117
-
> Add the following lines of code above the class definition:
118
-
119
-
```java
120
-
importliquidjava.specification.RefinementAlias;
121
-
122
-
@RefinementAlias("NonNegative(int v) { v >= 0 }")
123
-
```
124
-
125
-
> Then, replace all occurrences of `@Refinement("_ >= 0")` with `@Refinement("NonNegative(_)")`.
126
-
127
-
The refinements are now easier to understand, while still providing the same guarantees!
111
+
With the correct refinements in place, LiquidJava will report an error in the second `decrement` method call, since it tries to decrement the counter when it is already zero.
0 commit comments