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
55 changes: 55 additions & 0 deletions examples/stage0/snippets/classes-methods/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

// [pointFull]
Comment thread
zachwaffle4 marked this conversation as resolved.
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]
}
// [/pointFull]
57 changes: 57 additions & 0 deletions examples/stage0/snippets/classes-methods/RobotTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

Comment thread
zachwaffle4 marked this conversation as resolved.
// [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]
}
// [/robotTrackerFull]
30 changes: 30 additions & 0 deletions examples/stage0/snippets/classes-methods/Usage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

void main() {
// [createAndUse]
Point a = new Point(3, 4);
Point b = new Point(1, 2);

Point sum = a.plus(b);
System.out.println(sum.getX()); // 4.0
System.out.println(sum.getY()); // 6.0
System.out.println(a.norm()); // 5.0
// [/createAndUse]

// [accessStaticConstant]
System.out.println(Point.ORIGIN.getX()); // 0.0
// [/accessStaticConstant]

// [trackerUsage]
RobotTracker tracker = new RobotTracker(Point.ORIGIN);
tracker.move(new Point(3, 0));
tracker.move(new Point(0, 4));
System.out.println(tracker.distanceTo(Point.ORIGIN)); // 5.0
tracker.reset();
System.out.println(tracker.getPosition().getX()); // 0.0
// [/trackerUsage]
}
8 changes: 4 additions & 4 deletions src/config/sidebarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ export const sidebarSections: Record<string, SidebarSection[]> = {
// label: 'Loops',
// slug: 'learning-course/stage0/loops',
// },
// {
// label: 'Objects',
// slug: 'learning-course/stage0/objects',
// },
{
label: 'Classes, Fields, and Methods',
slug: 'learning-course/stage0/classes-methods',
},
// {
// label: 'Methods',
// slug: 'learning-course/stage0/methods',
Expand Down
Loading