Skip to content

Commit e3d4375

Browse files
committed
Update Exercise
1 parent 64d66d2 commit e3d4375

File tree

3 files changed

+32
-47
lines changed

3 files changed

+32
-47
lines changed

README.md

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,19 @@ Now we get an error! LiquidJava is telling us that the implementation of the met
9696

9797
#### Exercise
9898

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).
100100
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.
102102

103103
> Replace the `"true"` refinements with the appropriate ones to ensure the correct behavior of both methods.
104104
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.
110110

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-
import liquidjava.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.
128112

129113
### 2. State Refinements
130114

src/main/java/com/tutorial/part1/exercise/Bank.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.tutorial.part1.exercise;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
public class Counter {
6+
7+
@Refinement("true")
8+
public static int increment(@Refinement("true") int count) {
9+
return count + 1;
10+
}
11+
12+
@Refinement("true")
13+
public static int decrement(@Refinement("true") int count) {
14+
return count - 1;
15+
}
16+
17+
public static void main(String[] args) {
18+
int c = 0;
19+
c = Counter.increment(c);
20+
c = Counter.decrement(c);
21+
c = Counter.decrement(c); // should be an error
22+
System.out.println(c);
23+
}
24+
}

0 commit comments

Comments
 (0)