stage 0: add arrays + interfaces and lists lessons - #188
Conversation
Co-authored-by: 0verkil <112268976+0verkil@users.noreply.github.com>
Removed duplicate lockfile key, reverted package upgrades
|
🌐 Preview URL: https://pr-188.frcsoftware.pages.dev |
ff6f4ce to
997d1b7
Compare
997d1b7 to
f8d173f
Compare
…ata-structures # Conflicts: # examples/stage0/snippets/src/Loops.java # src/content/docs/learning-course/stage0/loops.mdx
| In this lesson, we're going to learn about **arrays**, which let us store many values of the same type together, | ||
| and the **for-each loop**, a way to loop over those values that's often cleaner than the `for` loop you already know. | ||
|
|
||
| ## Why Arrays? |
There was a problem hiding this comment.
What is an array should come before why arrays
| double motor4Speed = 0.5; | ||
| ``` | ||
|
|
||
| This works for four motors, but it doesn't scale. |
There was a problem hiding this comment.
It's hard for this point to land since swerve drive example is used and those for the most part always have exactly 4 modules. There isn't any scaling to be done.
An example that shows 4 calls reading each property individually rather than looping (e.g logging) could show the benefit better
| ``` | ||
|
|
||
| This creates an array of four `double`s, all initially set to `0.0`. | ||
| You can think of an array literal as a shorthand for creating an array this way and then filling in each value. |
There was a problem hiding this comment.
Unnecessary and possibly confusing comment
| ## Indexing and Length | ||
|
|
||
| Each value in an array is called an **element**, and you access a specific element using its **index**, the element's position in the array. | ||
| Just like `String` and `List` (which we'll see in the next lesson), array indices in Java start at `0`, not `1`. |
There was a problem hiding this comment.
| Just like `String` and `List` (which we'll see in the next lesson), array indices in Java start at `0`, not `1`. | |
| Array indices in Java start at `0`, not `1`. |
| <Aside type="caution" title="Array Bounds"> | ||
| Every index must be between `0` and `length - 1`. Accessing | ||
| `motorSpeeds[4]`, or any index that's out of that range, will throw an | ||
| exception. This stops your program from continuing. |
There was a problem hiding this comment.
| exception. This stops your program from continuing. | |
| exception. This will crash your robot code. |
| Here we use the `implements` keyword to tell Java that `UltrasonicSensor` and `LidarSensor` implement `DistanceSensor`. | ||
| If we said the `implements DistanceSensor` but didn't add a `getDistanceMeters()` method, the compiler would error. |
There was a problem hiding this comment.
| Here we use the `implements` keyword to tell Java that `UltrasonicSensor` and `LidarSensor` implement `DistanceSensor`. | |
| If we said the `implements DistanceSensor` but didn't add a `getDistanceMeters()` method, the compiler would error. | |
| By implementing `DistanceSensor`, `UltrasonicSensor` and `LidarSensor` promise to fulfill the contract specified by `DistanceSensor` and provide a `getDistanceMeters()` method. Java will not allow you to compile if these methods are missing. |
| We also use the `@Override` annotation to tell the compiler we are **overriding** that `getDistanceMeters()` method. | ||
| Overriding a method is a way to change the behavior of a method that's already defined in a parent class or interface. |
There was a problem hiding this comment.
Mention that the annotation is optional, but allows the compiler to warn you if if detects errors within the overriding method
|
|
||
| ``` | ||
|
|
||
| This imports `ArrayList<T>` and `List<T>` from the `java.util` package, which we'll use next. |
There was a problem hiding this comment.
Generics shouldn't be included here
| // [robotHistoryTrackerClass] | ||
| class RobotHistoryTracker { | ||
| private Point position; | ||
| private final List<Point> history = new ArrayList<>(); |
There was a problem hiding this comment.
Nitpick: I know that only ArrayList is mentioned in the docs, LinkedList would be better here. Could be an opportunity to mention there are multiple implementations of List to support different use cases. LinkedList works best here because we're only adding elements to the end, not inserting in the middle
Description
this is two pages so it's more digestible 👍
Meta
Merge checklist: