-
Notifications
You must be signed in to change notification settings - Fork 21
add classes and methods lesson #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zachwaffle4
merged 10 commits into
frcsoftware:main
from
zachwaffle4:stage0/classes-methods
Aug 3, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
54759f2
add classes and methods lesson
zachwaffle4 012e691
review comments
zachwaffle4 8beeeca
fix link in classes and methods lesson
zachwaffle4 775c818
add info about math class
zachwaffle4 e8aa495
address review comments part 3
zachwaffle4 b9148c2
add mutable/immutable distinction to example
zachwaffle4 f58087c
oxford comma
zachwaffle4 e365972
address review comments again
zachwaffle4 bb7962b
emphasize class vs object
zachwaffle4 0865b4d
Merge branch 'main' into stage0/classes-methods
Adrianamm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright 2026 FRCSoftware | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| // [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] | ||
| } | ||
| // [/pointFull] | ||
57 changes: 57 additions & 0 deletions
57
examples/stage0/snippets/classes-methods/RobotTracker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2026 FRCSoftware | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
|
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] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.