import { validationRegexes } from "./validation-regexes.js"; /* * TUTORIAL CONTENT LIVES HERE * --------------------------- * Edit this file to change examples, prompts, checks, or quick-check questions. * The interface in app.js renders every lesson from this object automatically. * * Exercise checks are JavaScript regular expressions stored as strings. They are * intentionally tolerant of spacing so learners can format Java naturally. */ export const tutorial = { meta: { title: "LiquidJava Interactive Tutorial", eyebrow: "A hands-on interactive tutorial", introduction: "Learn how LiquidJava catches invalid values and illegal object behavior before a Java program runs.", }, lessons: [ { id: "values", number: "02", title: "Put boundaries on values", shortTitle: "Values", lead: "A regular Java type says “this is an integer.” A refinement can also limit that integer to the range expected by the program.", concept: { label: "Read the range", code: '@Refinement("_ >= 0 && _ = and = 0 && _ = low && _ { @StateRefinement(to="size(this) == 0") public void ArrayList(); // Constructor @StateRefinement( to="size(this) == size(old(this)) + 1" ) public boolean add(E element); }`, explanation: "The ghost variable size records how many elements an ArrayList contains. add relates the new size to the old size.", notes: [ "Ghost state exists for verification; it is not a runtime field.", "old(this) refers to the object state before the method call.", "Other refinements can use the ghost value to prevent out-of-bounds access.", ], }, exercise: { title: "Complete the stack refinements", prompt: "Replace the true refinements so the constructor, push, pop, and peek maintain and check the size ghost variable.", starterCode: `import liquidjava.specification.ExternalRefinementsFor; import liquidjava.specification.Ghost; import liquidjava.specification.StateRefinement; @ExternalRefinementsFor("java.util.Stack") @Ghost("int size") public interface StackRefinements { @StateRefinement(to="true") public void Stack(); // Constructor @StateRefinement(to="true") public E push(E elem); @StateRefinement(from="true", to="true") public E pop(); @StateRefinement(from="true") public E peek(); }`, solutionCode: `import liquidjava.specification.ExternalRefinementsFor; import liquidjava.specification.Ghost; import liquidjava.specification.StateRefinement; @ExternalRefinementsFor("java.util.Stack") @Ghost("int size") public interface StackRefinements { @StateRefinement(to="size(this) == 0") public void Stack(); @StateRefinement(to="size(this) == size(old(this)) + 1") public E push(E elem); @StateRefinement(from="size(this) > 0", to="size(this) == size(old(this)) - 1") public E pop(); @StateRefinement(from="size(this) > 0") public E peek(); }`, checks: [ { pattern: validationRegexes.stack.constructor, message: "The constructor should initialize size(this) to 0.", }, { pattern: validationRegexes.stack.push, message: "push should increase the previous size by one.", }, { pattern: validationRegexes.stack.pop, message: "pop should require a non-empty stack and decrease size by one.", }, { pattern: validationRegexes.stack.peek, message: "peek should require size(this) to be greater than zero.", }, ], }, questions: [ { id: "ghosts-size", type: "text", prompt: "Starting from an empty stack, what is size after push(), push(), pop()?", placeholder: "Enter an integer", inputMode: "numeric", accepted: ["1"], explanation: "The two pushes increase size to 2, and pop decreases it to 1.", }, { id: "ghosts-empty", type: "radio", prompt: "Which operations are rejected when size(this) == 0?", choices: ["push() only", "pop() and peek()", "The constructor only"], correct: 1, explanation: "Both pop and peek require size(this) > 0.", }, ], }, ], }; if (typeof window !== "undefined") window.LIQUID_JAVA_TUTORIAL = tutorial;