-
Notifications
You must be signed in to change notification settings - Fork 15
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
Open
zachwaffle4
wants to merge
1
commit into
frcsoftware:main
Choose a base branch
from
zachwaffle4:stage0/classes-methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,46 @@ | ||
| /* | ||
| * Copyright 2026 FRCSoftware | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| // [pointFull] | ||
| class Point { | ||
| // [finalFields] | ||
| private final double x; | ||
| private final double y; | ||
| // [/finalFields] | ||
| // [staticConstant] | ||
| public static final Point ORIGIN = new Point(0, 0); | ||
| // [/staticConstant] | ||
|
|
||
| // [constructor] | ||
| public Point(double x, double y) { | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
| // [/constructor] | ||
|
|
||
| // [getters] | ||
| public double getX() { return this.x; } | ||
| public double getY() { return this.y; } | ||
| // [/getters] | ||
|
|
||
| // [plus] | ||
| public Point plus(Point other) { | ||
| return new Point(this.x + other.x, this.y + other.y); | ||
| } | ||
| // [/plus] | ||
|
|
||
| public Point minus(Point other) { | ||
| return new Point(this.x - other.x, this.y - other.y); | ||
| } | ||
|
|
||
| // [norm] | ||
| public double norm() { | ||
| double sumOfSquares = this.x * this.x + this.y * this.y; | ||
| return Math.sqrt(sumOfSquares); | ||
| } | ||
| // [/norm] | ||
| } | ||
| // [/pointFull] | ||
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
| /* | ||
| * Copyright 2026 FRCSoftware | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think putting little comments that re-explain what section does can be helpful for new students |
||
| // [robotTrackerFull] | ||
| class RobotTracker { | ||
| // [mutableField] | ||
| private Point position; | ||
| // [/mutableField] | ||
|
|
||
| // [robotTrackerConstructor] | ||
| public RobotTracker(Point startPosition) { | ||
| this.position = startPosition; | ||
| } | ||
| // [/robotTrackerConstructor] | ||
|
|
||
| // [stateMutation] | ||
| public void move(Point delta) { | ||
| this.position = this.position.plus(delta); | ||
| } | ||
| // [/stateMutation] | ||
|
|
||
| // [localVariableChain] | ||
| public double distanceTo(Point target) { | ||
| Point diff = target.minus(this.position); | ||
| return diff.norm(); | ||
| } | ||
| // [/localVariableChain] | ||
|
|
||
| public Point getPosition() { | ||
| return this.position; | ||
| } | ||
|
|
||
| // [useStaticConstant] | ||
| 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,31 @@ | ||
| /* | ||
| * 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 | ||
| System.out.println(a.equals(b)); // false | ||
| // [/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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think putting little comments that re-explain what section does can be helpful for new students