Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@ concurrency:
cancel-in-progress: true

jobs:
wrapper-validation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: gradle/actions/wrapper-validation@v6

build:
runs-on: ubuntu-latest
needs: wrapper-validation
steps:
- uses: actions/checkout@v7
- uses: actions/setup-java@v6
with:
distribution: temurin
java-version: '25'
- uses: gradle/actions/setup-gradle@v6
- run: ./gradlew build
working-directory: examples
8 changes: 1 addition & 7 deletions .github/workflows/lint-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ concurrency:
cancel-in-progress: true

jobs:
wrapper-validation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: gradle/actions/wrapper-validation@v6

website:
runs-on: ubuntu-latest
steps:
Expand All @@ -29,12 +23,12 @@ jobs:
- run: pnpm format:check
examples:
runs-on: ubuntu-latest
needs: wrapper-validation
steps:
- uses: actions/checkout@v7
- uses: actions/setup-java@v6
with:
distribution: temurin
java-version: '25'
- uses: gradle/actions/setup-gradle@v6
- run: ./gradlew spotlessCheck
working-directory: examples
2 changes: 2 additions & 0 deletions examples/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.gradle.caching=true
org.gradle.configuration-cache=true
44 changes: 42 additions & 2 deletions examples/stage0/snippets/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ plugins {
id "com.diffplug.spotless" version "8.0.0"
}

repositories {
mavenCentral()
}

java {
sourceCompatibility = JavaVersion.VERSION_25
targetCompatibility = JavaVersion.VERSION_25
Expand Down Expand Up @@ -31,10 +35,46 @@ tasks.register('runSnippets') {
dependsOn snippetRunTasks
}

def allJavaFiles = fileTree(".") {
include "**/*.java"
exclude "**/build/**", "**/build-*/**"
}

// Compact source files (JEP 512) declare members at the top level with no enclosing
// type, which google-java-format cannot parse. They only get whitespace cleanup.
def compactSourceFiles = allJavaFiles.filter { it.text =~ /(?m)^void main\(/ }

spotless {
java {
target fileTree('.') { include '**/*.java' }
licenseHeaderFile(rootProject.file('../../gradle/LICENSE_HEADER'), "^(?!/\\*|\\s*\\*)")
target allJavaFiles - compactSourceFiles
toggleOffOn()
googleJavaFormat()
trimTrailingWhitespace()
endWithNewline()
}
format "compactSources", {
target compactSourceFiles
trimTrailingWhitespace()
endWithNewline()
}
groovyGradle {
target fileTree(".") {
include "**/*.gradle"
exclude "**/build/**", "**/build-*/**"
}
greclipse()
leadingTabsToSpaces(4)
trimTrailingWhitespace()
endWithNewline()
}
format "misc", {
target fileTree(".") {
include "**/*.md", "**/.gitignore"
exclude "**/build/**", "**/build-*/**"
}
trimTrailingWhitespace()
leadingTabsToSpaces(2)
endWithNewline()
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/stage0/snippets/src/Conditionals.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public double setThrottle(double speed) {


void main() {

// [ifSyntax]
if (condition) {
// code to run when condition is true
Expand Down Expand Up @@ -84,4 +84,4 @@ void main() {
}
// [/conditionalExample]

}
}
8 changes: 4 additions & 4 deletions examples/stage0/snippets/src/Debugging.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/

void main() {

//[logicError1]
int width = 2;
int height = 5;
int area = width + height;
int area = width + height;
System.out.println("Area of the rectangle is " + area);
//[/logicError1]



}

}
4 changes: 2 additions & 2 deletions examples/stage0/snippets/src/JavaFundamentals.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ void main() {
double speed = 1;
System.out.println("Left Motor Speed " + speed);
//[/stringConcatenation1]

// [stringConcatenation2]
int first = 6;
double second = 2.0;

System.out.println(first + " " + second);
System.out.println(first + " " + second);

//[/stringConcatenation2]

Expand Down
100 changes: 55 additions & 45 deletions examples/stage0/snippets/src/classes-methods/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,60 @@

// [pointFull]
class Point {
// [finalFields]
// Each Point has its own x and y coordinates, and they never change.
private final double x;
private final double y;
// [/finalFields]
// [staticConstant]
// Shared by every Point, instead of belonging to just one instance.
public static final Point ORIGIN = new Point(0, 0);
// [/staticConstant]

// [constructor]
// Sets this Point's coordinates to the given x and y values.
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// [/constructor]

// [getters]
// Let other classes read the private x and y fields.
public double getX() { return this.x; }
public double getY() { return this.y; }
// [/getters]

// [plus]
// Adds this Point's coordinates to another Point's coordinates.
public Point plus(Point other) {
return new Point(this.x + other.x, this.y + other.y);
}
// [/plus]

// [minus]
// Subtracts another Point's coordinates from this Point's coordinates.
public Point minus(Point other) {
return new Point(this.x - other.x, this.y - other.y);
}
// [/minus]

// [norm]
// Computes this Point's distance from the origin.
public double norm() {
double sumOfSquares = this.x * this.x + this.y * this.y;
return Math.sqrt(sumOfSquares);
}
// [/norm]
// [finalFields]
// Each Point has its own x and y coordinates, and they never change.
private final double x;
private final double y;
// [/finalFields]
// [staticConstant]
// Shared by every Point, instead of belonging to just one instance.
public static final Point ORIGIN = new Point(0, 0);

// [/staticConstant]

// [constructor]
// Sets this Point's coordinates to the given x and y values.
public Point(double x, double y) {
this.x = x;
this.y = y;
}

// [/constructor]

// [getters]
// Let other classes read the private x and y fields.
public double getX() {
return this.x;
}

public double getY() {
return this.y;
}

// [/getters]

// [plus]
// Adds this Point's coordinates to another Point's coordinates.
public Point plus(Point other) {
return new Point(this.x + other.x, this.y + other.y);
}

// [/plus]

// [minus]
// Subtracts another Point's coordinates from this Point's coordinates.
public Point minus(Point other) {
return new Point(this.x - other.x, this.y - other.y);
}

// [/minus]

// [norm]
// Computes this Point's distance from the origin.
public double norm() {
double sumOfSquares = this.x * this.x + this.y * this.y;
return Math.sqrt(sumOfSquares);
}
// [/norm]
}
// [/pointFull]
100 changes: 53 additions & 47 deletions examples/stage0/snippets/src/classes-methods/RobotTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,58 @@

// [robotTrackerFull]
class RobotTracker {
// [mutableField]
// Not final, since the robot's position changes as it moves.
private Point position;
// [/mutableField]

// [robotTrackerConstructor]
// Starts tracking from a given position.
public RobotTracker(Point startPosition) {
this.position = startPosition;
}
// [/robotTrackerConstructor]

// [constructorChaining]
// Starts tracking from the origin by default.
public RobotTracker() {
this(Point.ORIGIN);
}
// [/constructorChaining]

// [stateMutation]
// Moves the tracked position by delta.
public void move(Point delta) {
this.position = this.position.plus(delta);
}
// [/stateMutation]

// [localVariableChain]
// Finds the straight-line distance from the current position to target.
public double distanceTo(Point target) {
Point diff = target.minus(this.position);
return diff.norm();
}
// [/localVariableChain]

// [getPosition]
// Lets other classes read the current position.
public Point getPosition() {
return this.position;
}
// [/getPosition]

// [useStaticConstant]
// Moves the tracked position back to the origin.
public void reset() {
this.position = Point.ORIGIN;
}
// [/useStaticConstant]
// [mutableField]
// Not final, since the robot's position changes as it moves.
private Point position;

// [/mutableField]

// [robotTrackerConstructor]
// Starts tracking from a given position.
public RobotTracker(Point startPosition) {
this.position = startPosition;
}

// [/robotTrackerConstructor]

// [constructorChaining]
// Starts tracking from the origin by default.
public RobotTracker() {
this(Point.ORIGIN);
}

// [/constructorChaining]

// [stateMutation]
// Moves the tracked position by delta.
public void move(Point delta) {
this.position = this.position.plus(delta);
}

// [/stateMutation]

// [localVariableChain]
// Finds the straight-line distance from the current position to target.
public double distanceTo(Point target) {
Point diff = target.minus(this.position);
return diff.norm();
}

// [/localVariableChain]

// [getPosition]
// Lets other classes read the current position.
public Point getPosition() {
return this.position;
}

// [/getPosition]

// [useStaticConstant]
// Moves the tracked position back to the origin.
public void reset() {
this.position = Point.ORIGIN;
}
// [/useStaticConstant]
}
// [/robotTrackerFull]
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

// [distanceSensorInterface]
interface DistanceSensor {
double getDistanceMeters();
double getDistanceMeters();
}
// [/distanceSensorInterface]
10 changes: 5 additions & 5 deletions examples/stage0/snippets/src/interfaces-lists/LidarSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

// [lidarSensorClass]
class LidarSensor implements DistanceSensor {
@Override
public double getDistanceMeters() {
// In real life, this would actually interact with hardware
return 1.2;
}
@Override
public double getDistanceMeters() {
// In real life, this would actually interact with hardware
return 1.2;
}
}
// [/lidarSensorClass]
Loading
Loading