Skip to content
Open
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
67 changes: 67 additions & 0 deletions examples/stage0/snippets/src/Arrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

void main() {
// [motorSpeedsLiteral]
double[] motorSpeeds = {0.5, 0.5, 0.5, 0.5};
// [/motorSpeedsLiteral]

// [motorSpeedsEmpty]
double[] emptyMotorSpeeds = new double[4];
// [/motorSpeedsEmpty]

// [setSpeed]
motorSpeeds[0] = 0.7;
System.out.println(motorSpeeds[0]); // 0.7
// [/setSpeed]

// [speedsLength]
System.out.println(motorSpeeds.length); // 4
// [/speedsLength]

// [pathArray]
Point[] path = {
new Point(0, 0),
new Point(1, 2),
new Point(3, 3),
};
// [/pathArray]

// [pathArrayEmpty]
Point[] emptyPath = new Point[3];
// [/pathArrayEmpty]

try {
// [pathArrayNull]
emptyPath[0].norm(); // error: emptyPath[0] is null
// [/pathArrayNull]
} catch (NullPointerException e) {
// for demo purposes we don't care about the exception'
}

// [indexLoopSpeeds]
double total = 0;
for (int i = 0; i < motorSpeeds.length; i++) {
total += motorSpeeds[i];
}
System.out.println(total); // 2.2
// [/indexLoopSpeeds]

// [forEachPath]
for (Point waypoint : path) {
System.out.println(waypoint.getX() + ", " + waypoint.getY());
}
// [/forEachPath]

// [pathLength]
double pathLength = 0;
for (int i = 1; i < path.length; i++) {
Point segment = path[i].minus(path[i - 1]);
pathLength += segment.norm();
}
System.out.println(pathLength); // 4.47213595499958
// [/pathLength]
}
82 changes: 82 additions & 0 deletions examples/stage0/snippets/src/Loops.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

class Drivetrain {
public void setThrottle(double speed) {}
}

boolean condition = false;
Drivetrain drivetrain = new Drivetrain();
Drivetrain drivetrain = new Drivetrain();

Check failure on line 13 in examples/stage0/snippets/src/Loops.java

View workflow job for this annotation

GitHub Actions / build

variable drivetrain is already defined in class Loops

void main() {
// [whileSyntax]
while (condition) {
// code to run when condition is true
}
// [/whileSyntax]

{
// [whileExample]
int i = 0;
while (i < 6) {
System.out.println(i); // prints 0, 1, 2, 3, 4, 5
i++;
}
// [/whileExample]
}

// [whileExample2]
int autoTimer = 0;
while (autoTimer <= 15){
System.out.println("AutoMode is happening");
autoTimer++;
}
// [/whileExample2]


{
// [ForExample1]
int i = 0;
while (i < 6) {
System.out.println("Hi!");
i++;
}
// [/ForExample1]
}

// [ForExample2]
for (int i = 0; i < 6; i++) {
System.out.println("Hi!");
}
//[/ForExample2]

//
// [forExample]
for (int i = 0; i < 5; i++){
System.out.println(i); // prints 0, 1, 2, 3, 4
}
// [/forExample]

if (false) {
// [Infinite1]
int timer = 0;
while (timer < 7){
drivetrain.setThrottle(1); // sets drive motors to full speed
}
// [/Infinite1]
}

{
// [Infinite2]
int timer = 0;
while (timer < 7) {
drivetrain.setThrottle(1); // sets drive motors to full speed
timer++; // increments timer by 1
}
// [/Infinite2]
}
}
11 changes: 11 additions & 0 deletions examples/stage0/snippets/src/interfaces-lists/DistanceSensor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

// [distanceSensorInterface]
interface DistanceSensor {
double getDistanceMeters();
}
// [/distanceSensorInterface]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

// [importList]
import java.util.ArrayList;
import java.util.List;
// [/importList]

// [isTooClose]
boolean isTooClose(DistanceSensor sensor) {
return sensor.getDistanceMeters() < 1.0;
}
// [/isTooClose]

// [genericLast]
<T> T last(T[] items) {
return items[items.length - 1];
}
// [/genericLast]

void main() {
// [useDistanceSensorCall]
DistanceSensor ultrasonic = new UltrasonicSensor();
DistanceSensor lidar = new LidarSensor();
System.out.println(isTooClose(ultrasonic)); // false
System.out.println(isTooClose(lidar)); // false
// [/useDistanceSensorCall]

// [genericLastCall]
Point[] path = {new Point(0, 0), new Point(1, 2), new Point(3, 3)};
DistanceSensor[] sensors = {ultrasonic, lidar};

System.out.println(last(path).getX()); // 3.0
System.out.println(last(sensors).getClass()); // class LidarSensor
// [/genericLastCall]

// [historyList]
List<Point> waypoints = new ArrayList<>();
// [/historyList]

// [historyAdd]
waypoints.add(new Point(0, 0));
waypoints.add(new Point(1, 2));
System.out.println(waypoints.size()); // 2
// [/historyAdd]

// [forEachHistory]
RobotHistoryTracker tracker = new RobotHistoryTracker(Point.ORIGIN);
tracker.move(new Point(3, 0));
tracker.move(new Point(0, 4));

for (Point visited : tracker.getHistory()) {
System.out.println(visited.getX() + ", " + visited.getY());
}
// [/forEachHistory]
}
15 changes: 15 additions & 0 deletions examples/stage0/snippets/src/interfaces-lists/LidarSensor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

// [lidarSensorClass]
class LidarSensor implements DistanceSensor {
@Override
public double getDistanceMeters() {
// In real life, this would actually interact with hardware
return 1.2;
}
}
// [/lidarSensorClass]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

import java.util.ArrayList;
import java.util.List;

// [robotHistoryTrackerClass]
class RobotHistoryTracker {
private Point position;
private final List<Point> history = new ArrayList<>();

public RobotHistoryTracker(Point startPosition) {
this.position = startPosition;
this.history.add(startPosition);
}

public void move(Point delta) {
this.position = this.position.plus(delta);
this.history.add(this.position);
}

public Point getPosition() {
return this.position;
}

public List<Point> getHistory() {
return this.history;
}
}
// [/robotHistoryTrackerClass]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

// [ultrasonicSensorClass]
class UltrasonicSensor implements DistanceSensor {
@Override
public double getDistanceMeters() {
// In real life, this would actually interact with hardware
return 1.5;
}
}
// [/ultrasonicSensorClass]
Binary file added public/learning-course/stage0/loops/ForLoop.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 12 additions & 4 deletions src/config/sidebarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ export const sidebarSections: Record<string, SidebarSection[]> = {
label: 'Conditionals',
slug: 'learning-course/stage0/conditionals',
},
// {
// label: 'Loops',
// slug: 'learning-course/stage0/loops',
// },
{
label: 'Loops',
slug: 'learning-course/stage0/loops',
},
{
label: 'Classes, Fields, and Methods',
slug: 'learning-course/stage0/classes-methods',
Expand All @@ -81,6 +81,14 @@ export const sidebarSections: Record<string, SidebarSection[]> = {
// label: 'Methods',
// slug: 'learning-course/stage0/methods',
// },
{
label: 'Arrays and For-Each Loops',
slug: 'learning-course/stage0/arrays',
},
{
label: 'Interfaces, Generics, and Lists',
slug: 'learning-course/stage0/interfaces-lists',
},
],
},
{
Expand Down
Loading
Loading