Skip to content
Merged
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
37 changes: 32 additions & 5 deletions examples/stage0/snippets/src/JavaFundamentals.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
*
* SPDX-License-Identifier: BSD-3-Clause
*/
int matchTime = 0;

// [variables]
int CLIMBER_ID = 51;
double UP_POSITION = -33.5;
double DOWN_POSITION = 0;
int CLIMBER_ID = 51; // an integer named CLIMBER_ID that holds the value 51
double UP_POSITION = 33.5; // a double named UP_POSITION that holds the value 33.5
boolean isFinished = false; // a boolean named isFinished that holds the value false
String autoName = "Side Auto"; // a String named autoName that holds the value "Side Auto"
// [/variables]

void main() {
Expand All @@ -15,10 +18,24 @@ void main() {
// [/printLiteral]

// [printVariable]
int number = 4;
System.out.println(number); // prints out the value 4
int num = 4;
System.out.println(num); // prints out the value 4
// [/printVariable]

// [stringConcatenation1]
double speed = 1;
System.out.println("Left Motor Speed " + speed);
//[/stringConcatenation1]

// [stringConcatenation2]
int first = 6;
double second = 2.0;

System.out.println(first + " " + second);

//[/stringConcatenation2]


// [singleLineComment]
// This prints Hello World
System.out.println("Hello World");
Expand All @@ -33,4 +50,14 @@ void main() {
This is another line */
System.out.println("Hello World");
// [/multiLineComment]

// [goodComment]
// Calculate how long is left in the match and show it to the drivers
System.out.println(150 - matchTime);
// [/goodComment]

// [badComment]
// Print 150 minus matchTime
System.out.println(150 - matchTime);
// [/badComment]
}
18 changes: 13 additions & 5 deletions examples/stage0/snippets/src/Operators.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
// [/variables]

void main() {
// [multiplication]
int magicNumber = 6;
System.out.println(magicNumber * 2);
// [/multiplication]

// [increments]
int x = 6;
int y = 7;
Expand Down Expand Up @@ -50,4 +45,17 @@ void main() {
System.out.println(fiveIsGreaterThanThree || nineIsLessThanTwo);
System.out.println(!fiveIsGreaterThanThree);
// [/logical]


// [math1]
int e = 0;
int f = 2;
System.out.println(e = f + 10); // prints 12
// [/math1]

// [math2]
int magicNumber = 6;
System.out.println(magicNumber * 2); // prints 12
// [/math2]

}
12 changes: 3 additions & 9 deletions examples/stage0/solutions/src/Print.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@ void main() {
System.out.println("It knows this because it knows where it isn't.");

// Define a variable `pi` that is equal to 3.14159.
// HINT: Make sure to pick the correct datatypes.
// HINT: Make sure to pick the correct data types.
double pi = 3.14159;
// Define an variable `g` that is equal to 10.
int g = 10;
// Define a variable `mode` that is equal to "autonomous".
String mode = "autonomous";

// Now, print all three variables in the **same** print statement,
// separated by spaces. You can do this by passing in the three variables, and
// "adding" an empty string (" ") in between each pair, as if you were adding
// two numbers together, using the `+` operator. Ex. [pi + " " + g].
// Java recognizes that one of the items being added is a string, and
// converts the items on either side to a string before combining everything.
// This method of combining strings via the `+` operator is known as string
// concatenation.
// separated by spaces.
System.out.println(pi + " " + g + " " + mode);

// Now, change pi to equal 3.142857 (a slightly incorrect approximation of pi
Expand All @@ -42,7 +36,7 @@ void main() {
// Create a variable `degrees` of type `double` and assign it a value of
// 360. Then, print the variable to observe type narrowing behavior
// (it prints 360.0 with a decimal part, instead of just 360, since the
// variable uses a dataype with decimal parts).
// variable uses a data type with decimal parts).
double degrees = 360;
System.out.println(degrees);
}
12 changes: 3 additions & 9 deletions examples/stage0/templates/src/Print.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {


// Define a variable `pi` that is equal to 3.14159.
// HINT: Pick the correct datatypes.
// HINT: Pick the correct data types.


// Define a variable `g` that is equal to 10.
Expand All @@ -25,13 +25,7 @@ void main() {


// Now, print all three variables in the **same** print statement,
// separated by spaces. You can do this by passing in the three variables, and
// "adding" an empty string (" ") in between both pairs, as if you were adding
// two numbers together, using the `+` operator. Ex. [pi + " " + g].
// Java recognizes that one of the items being added is a string, and
// converts the items on either side to a string before combining everything.
// This method of combining strings via the `+` operator is known as string
// concatenation.
// separated by spaces.


// Now, change pi to equal 3.142857 (a slightly incorrect approximation of pi
Expand All @@ -41,6 +35,6 @@ void main() {
// Create a variable `degrees` of type `double` and assign it a value of
// 360. Then, print the variable to observe type narrowing behavior
// (it prints 360.0 with a decimal part, instead of just 360, since the
// variable uses a dataype with decimal parts).
// variable uses a data type with decimal parts).

}
Binary file removed public/learning-course/stage0/conditionals/else.webp
Binary file not shown.
Binary file not shown.
Binary file not shown.
53 changes: 10 additions & 43 deletions src/content/docs/learning-course/stage0/conditionals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ codeRegionSources:
---

In everyday life, we make decisions based on different situations.
Such as "If we are hungry, then we get a snack" or "If we are tired, we go to bed".
Often in code, we want our program to do something similar and make different decisions based on different situations.
When programming a robot, one example could be "If the timer starts, drive the robot for 5 seconds then stop when the timer reached 15 seconds.
For example, "If we are hungry, then we get a snack" or "If we are tired, then we go to bed."
Robots often need to make different decisions depending on the situation.
For example, "If the timer starts, drive the robot for 5 seconds" or "If the timer is 0, stop driving."
To add decisions to our code, we use conditionals.

This section will cover three types of conditionals which are:
Expand All @@ -26,14 +26,9 @@ This section will cover three types of conditionals which are:
conditional.](https://www.w3schools.com/java/java_switch.asp)
</Aside>

Like we mentioned earlier in [Java Fundamentals](/learning-course/stage0/java-fundamentals/), all programming languages have syntax that needs to be followed.
One part of syntax is keywords.
As a reminder, a keyword is a reserved word that the compiler uses to run the code.
In Java, we use the following keywords for conditionals: `if`, `else if`, `else`

## if

An `if` statement is a basic conditional.
An `if` statement runs code only if a specified condition is true.
The syntax for an `if` statement is as follows:

```java #ifSyntax
Expand All @@ -48,8 +43,7 @@ Let's break down what this means:
- `{` an open curly braces is used to denote the opening of the conditional
- `}` a closed curly braces is used to denote the end of the conditional

The code in between the opening and closing curly braces are called a "code block".
A code block a sequence of code that is enclosed in curly braces.
The code in between the opening and closing curly braces is called a "code block".

<Aside type="tip">
All conditionals follow a very similar syntax. They all start with a
Expand All @@ -60,20 +54,9 @@ A code block a sequence of code that is enclosed in curly braces.
`if` statements can make decisions based on the `condition` inside the parentheses.
If `condition` is true, the code inside the `if` block will run.
If `condition` is false, the code inside the `if` block will be skipped.
This is also described in the flowchart below

<ContentFigure
src="/learning-course/stage0/conditionals/ifstatement.webp"
width="400px"
height="800px"
/>

Now let's look at an example.
In this example, pretend that we have a robot with a distance sensor on it.
A distance sensor detects and reports how far or how close the robot is to an object.
We want our robot's drivetrain motors to keep spinning until the robot is 10cm away from an object.
Our robot is currently 6cm away.
Looking at the example below, what should happen?
In this example, this code will only tell the drivetrain to move forward when distance is smaller then 10.

```java #ifExample

Expand All @@ -100,15 +83,8 @@ The syntax for an else statement is as follows:
Here's how this works: if the`if` statement's condition is `true`, then the code inside will run.
If the `if` statement's condition is `false`, the `if` block will be skipped, and the code inside the `else` block will run.

This is also described in the flowchart below

<ContentFigure
src="/learning-course/stage0/conditionals/else.webp"
width="600px"
height="800px"
/>

In this example, we now have an `else` statement that sets the motor speed to 0 if `distance` is greater than 20. Since our `distance` is set to 21, what would the code print out?
In this example, we now have an `else` statement that sets the motor speed to 0 if `distance` is not less than 10.
Since our `distance` is set to 21, what would the code print out?

```java #elseExample

Expand All @@ -120,10 +96,8 @@ The `else` runs and it prints out "Motors are not spinning", and the motors are

## else if

Sometimes we want our code to have specific logic for multiple cases.
Often we want the robot to slow down as it approaches its target.
Let's say we want the robot to drive at half speed when it's 5cm away from its target.
This can be done by adding an `else if` statement.
`if` and `else` lets you choose between two options, but what if you wanted to choose between even more options?
`else if` statements let you choose between as many conditions as you like.

The syntax for an `else if` statement goes as follows:

Expand All @@ -139,13 +113,6 @@ If `conditionA` is true then the code inside the `if` block will run.
If `conditionA` if false, then it will go to the `else if` statement.
If `conditionB` is true, the code inside the `else if` block will run.
If `conditionB` if false, then it will exit the statement and none of the code will run.
This is also described in the flowchart below

<ContentFigure
src="/learning-course/stage0/conditionals/elseif.webp"
width="700px"
height="800px"
/>

In the example, we now have an `else if` statement that sets the motor to half speed if the distance from an object is less than 10cm.
Our `distance` is now 15cm.
Expand Down
Loading
Loading