Skip to content

Commit 96e381a

Browse files
committed
Add Challenge
1 parent fa691fa commit 96e381a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ If you get stuck, here are some **hints**:
220220

221221
With the correct implementation, LiquidJava will report an error in line 11 of [StackExample.java](./src/main/java/com/tutorial/part4/exercise/StackExample.java), since we are trying to pop an element of the stack when it is empty.
222222

223+
### 5. Challenge
224+
225+
> Open [Downloader.java](./src/main/java/com/tutorial/part5/Downloader.java). Your task is to refine the `Downloader` class by replacing the `"true"` refinements with the appropriate ones to ensure the correct behavior of the `start`, `updateProgress`, `complete` and `getFile` methods, using the `progress` ghost variable to keep track of the download progress (from `0` to `100`), and not allow incorrect uses of these methods.
226+
227+
You will have to use most of the concepts learned throughout this tutorial, including refinement aliases, state refinements, and ghost variables.
228+
229+
If you get stuck, here are some **hints**:
230+
231+
- The refinement alias `Percent` should only allow integers between `0` and `100` and then it should be used to refine the `percentage` parameter of the `updateProgress` method
232+
- You should combine states and ghost variable assertions in the state refinements of the `updateProgress` and `complete` methods using the `&&` operator
233+
- You should define that the new percentage value in the `updateProgress` method should always be greater than the current `progress` ghost variable value
234+
- The `getFile` method should only be called when the download is finished
235+
236+
With the correct implementation, LiquidJava should report an error in line 35, since we are trying to complete the download when the progress is not yet `100%`. Simply add a new line that updates the progress to `100` before calling the `complete` method to fix the error.
237+
223238
---
224239

225240
## Quiz
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.tutorial.part5;
2+
3+
import liquidjava.specification.RefinementAlias;
4+
import liquidjava.specification.StateSet;
5+
import liquidjava.specification.Ghost;
6+
import liquidjava.specification.StateRefinement;
7+
import liquidjava.specification.Refinement;
8+
9+
@RefinementAlias("Percent(int x) { true }")
10+
@StateSet({"waiting", "downloading", "finished"})
11+
@Ghost("int progress")
12+
public class Downloader {
13+
14+
@StateRefinement(to="waiting(this)")
15+
public Downloader() {}
16+
17+
@StateRefinement(from="true", to="true")
18+
public void start() {}
19+
20+
@StateRefinement(from="true", to="true")
21+
@Refinement("true")
22+
public void updateProgress(@Refinement("true") int percentage) {}
23+
24+
@StateRefinement(from="true", to="true")
25+
public void complete() {}
26+
27+
@StateRefinement(from="true")
28+
public void getFile() {}
29+
30+
public static void main(String[] args) {
31+
Downloader d = new Downloader();
32+
d.start();
33+
d.updateProgress(30);
34+
d.updateProgress(70);
35+
d.complete(); // should be an error
36+
d.getFile();
37+
}
38+
}
39+

0 commit comments

Comments
 (0)