-
Notifications
You must be signed in to change notification settings - Fork 23
Stage 0: Loops Exercises #295
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
base: main
Are you sure you want to change the base?
Changes from all commits
6c2bf72
ba0c185
9285a03
2689abc
4412623
df3f459
507e100
038c2e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,55 @@ | ||||||
| /* | ||||||
| * Copyright 2026 FRCSoftware | ||||||
| * | ||||||
| * SPDX-License-Identifier: BSD-3-Clause | ||||||
| */ | ||||||
|
|
||||||
| class Drivetrain { | ||||||
| /** | ||||||
| * Dummy function that spins drivetrain motors at the specified speed. | ||||||
| * @param speed | ||||||
| */ | ||||||
| public void setThrottle(double speed) { | ||||||
| System.out.println("Spinning drivetrain motors at speed: " + speed); | ||||||
| } | ||||||
| } | ||||||
| Drivetrain drivetrain = new Drivetrain(); | ||||||
|
|
||||||
| void main() { | ||||||
|
|
||||||
| // First, create a new integer variable named `error` | ||||||
| // Below, create a while loop that compares if error is less than 5 | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // if that is true, print "Robots should not quit", then increase `error` by one | ||||||
| // if error is no longer less than 5, print "but yours did" | ||||||
|
Contributor
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.
Suggested change
Comment on lines
+22
to
+23
Contributor
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. This example is funny to us, but might be confusing and not very intuitive what the students should be trying to accomplish with this loop. What if we made a more realistic scenario like: A robot gyro must be calibrated before using it. Calibrating is done by printing "Calibrating..." to the console 5 times. Using a |
||||||
| int error = 0; | ||||||
| while (error < 5){ | ||||||
| System.out.println("Robots should not quit"); | ||||||
| error++; | ||||||
| } | ||||||
| System.out.println("but yours did"); | ||||||
|
|
||||||
| // Create a for loop that has a new integer variable named `timer` | ||||||
| // that is set to 15. Check for when timer is greater than or equal to 0, | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // then decrease timer by one. | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // Inside the for loop, include a print statement that prints "time left: " | ||||||
| // and the variable `timer` | ||||||
| for (int timer = 15; timer >= 0; timer--){ | ||||||
| System.out.println("time left: " + timer); | ||||||
| } | ||||||
|
|
||||||
| // First, create a for loop that has a new integer variable named | ||||||
| // `timer` that is set to 15. The for loop checks if `timer` is | ||||||
| // greater than or equal to 0 then it decreases timer by one | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // Inside the for loop, have an if statement that checks if timer | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // is less than or equal to 0. If so, set the drivetrain speed to 0 | ||||||
| // If the timer is less than or equal to 15, set the speed to one | ||||||
|
Contributor
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.
Suggested change
|
||||||
| for (int timer = 15; timer >= 0; timer--){ | ||||||
| if (timer <= 0 ){ | ||||||
| drivetrain.setThrottle(0); | ||||||
|
|
||||||
|
Contributor
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. is this extra line the result of spotless/prettify or is it accidental? |
||||||
| } else if (timer <= 15 ){ | ||||||
| drivetrain.setThrottle(1); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||
| /* | ||||||
| * Copyright 2026 FRCSoftware | ||||||
| * | ||||||
| * SPDX-License-Identifier: BSD-3-Clause | ||||||
| */ | ||||||
|
|
||||||
| class Drivetrain { | ||||||
| /** | ||||||
| * Dummy function that spins drivetrain motors at the specified speed. | ||||||
| * @param speed | ||||||
| */ | ||||||
| public void setThrottle(double speed) { | ||||||
| System.out.println("Spinning drivetrain motors at speed: " + speed); | ||||||
| } | ||||||
| } | ||||||
| Drivetrain drivetrain = new Drivetrain(); | ||||||
|
|
||||||
| void main() { | ||||||
|
|
||||||
| // First, create a new integer variable named `error` | ||||||
| // Below, create a while loop that compares if error is less than 5 | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // if that is true, print "Robots should not quit", then increase `error` by one | ||||||
| // if error is no longer less than 5, print "but yours did" | ||||||
|
Contributor
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.
Suggested change
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
| // Create a for loop that has a new integer variable named `timer` | ||||||
| // that is set to 15. Check for when timer is greater than or equal to 0, | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // then decrease timer by one. | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // Inside the for loop, include a print statement that prints "time left " | ||||||
| // and the variable `timer` | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
| // First, create a for loop that has a new integer variable named | ||||||
| // `timer` that is set to 15. The for loop checks if `timer` is | ||||||
| // greater than or equal to 0 then it decreases timer by one | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // Inside the for loop, have an if statement that checks if timer | ||||||
|
Contributor
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.
Suggested change
|
||||||
| // is less than or equal to 0. If so, set the drivetrain speed to 0 | ||||||
| // If the timer is less than or equal to 15, set the speed to one | ||||||
|
Contributor
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.
Suggested change
|
||||||
|
|
||||||
|
|
||||||
| } | ||||||
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.
It's probably best to use throttle as the variable name