diff --git a/examples/stage0/snippets/operators.java b/examples/stage0/snippets/operators.java new file mode 100644 index 0000000..c29fa80 --- /dev/null +++ b/examples/stage0/snippets/operators.java @@ -0,0 +1,50 @@ +/* + * Copyright 2026 FRCSoftware + * + * SPDX-License-Identifier: BSD-3-Clause + */ +// [variables] +int answer1 = 2 + 4; +int answer2 = 6 / 3; +int answer3 = 10 - 3; +// [/variables] + +void main() { + // [multiplication] + int Number = 6; + System.out.print(Number * 2); + // [/multiplication] + + // [increments] + int x = 6; + int y = 7; + + System.out.println(++x); + System.out.println(--y); + // [/increments] + + // [arithmetic] + int A = 10; + int B = 5; + A += 1; + B -= 1; + + System.out.println(A); // prints 11 + System.out.println(B); // prints 4 + // [/arithmetic] + + // [comparison] + int C = 2; + int D = 4; + System.out.print(A > B); + // [/comparison] + + // [logical] + boolean AnswerOne = 5 > 3; // True + boolean AnswerTwo = 9 < 2; // False + + System.out.println(AnswerOne && AnswerTwo); + System.out.println(AnswerOne || AnswerTwo); + System.out.println(!AnswerOne); + // [/logical] +} diff --git a/src/config/sidebarConfig.ts b/src/config/sidebarConfig.ts index 72e16be..0a16377 100644 --- a/src/config/sidebarConfig.ts +++ b/src/config/sidebarConfig.ts @@ -70,10 +70,10 @@ export const sidebarSections: Record = { label: 'Java Fundamentals', slug: 'learning-course/intro-to-java/java-fundamentals', }, - // { - // label: 'Operators', - // slug: 'learning-course/intro-to-java/operators', - // }, + { + label: 'Operators', + slug: 'learning-course/intro-to-java/operators', + }, // { // label: 'Conditionals', // slug: 'learning-course/intro-to-java/conditionals', @@ -281,6 +281,10 @@ export const sidebarSections: Record = { label: 'Java fundamentals', slug: 'intro-to-java/java-fundamentals', }, + { + label: 'operators', + slug: 'intro-to-java/operators', + }, ], }, ], diff --git a/src/content/docs/learning-course/intro-to-java/java-fundamentals.mdx b/src/content/docs/learning-course/intro-to-java/java-fundamentals.mdx index ea4a3d0..9bc8d19 100644 --- a/src/content/docs/learning-course/intro-to-java/java-fundamentals.mdx +++ b/src/content/docs/learning-course/intro-to-java/java-fundamentals.mdx @@ -89,7 +89,7 @@ UP_POSITION is a double that holds the value -33.5 and DOWN_POSITION is a double @@ -120,7 +120,7 @@ In Java, there are two types of comments: single-line comments and multi-line co ### Single-line Comments Single line comments begin with `//` and mark the rest of the line as being a comment. -For example, the code below leave the note of "This prints out Hello World." above the print statment +For example, the code below leave the note of "This prints out Hello World." above the print statement ```java stage0/snippets/JavaFundamentals.java#singleLineComment diff --git a/src/content/docs/learning-course/intro-to-java/operators.mdx b/src/content/docs/learning-course/intro-to-java/operators.mdx new file mode 100644 index 0000000..a35a9b6 --- /dev/null +++ b/src/content/docs/learning-course/intro-to-java/operators.mdx @@ -0,0 +1,334 @@ +--- +title: Operators +description: An Intro to operators and how they are used to change variables +prev: intro-to-java/java-fundamentals +next: false +--- + +In Java, we use operators to perform to change or compare the values of variables. +There are different types of operators, which are: + +- Arithmetic Operators +- Assignment Operators +- Comparison Operators +- Logical Operators + +These types of operators are often used in programming. +They can be used to update a robot arm's setpoint, where we want the mechanism to be, or to change the value of a variable in a loop. +Operators are important to know when programming your robot + +## Arithmetic Operators + +Arithmetic operators are used to perform basic math on variables. +These include: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OperatorDescriptionCompatible Data TypesExample
+Addition: Adds two values together int, doublea + b
-Subtraction: Subtracts one value from the otherint, doublea - b
*Multiplication: Multiplies two values togetherint, doublea * b
/Division: Divides one value from the otherint, doublea / b
% + Remainder: Returns the remainder of two numbers after division + int, doublea / b
++Increment: add 1 to a variableint, doublea++
--Decrement: subtracts 1 to a variableint, doublea--
+ + + +Operators can be used when creating variables or when changing a variable later in code. +In the example below, we have three variables whose values are set using different operators. +Without running the code, what value would each variable hold? + +```java stage0/snippets/operators.java#variables + +``` + +
+ Answer +
+

+ * answer1 is 6 because 2 + 4 = 6 * answer2 is 2 because 6 / 3 = 2 * + answer3 is 7 because 10 - 3 = 7 +

+
+
+ +Operators can also be used to change a variable later in code. +In the example below, we have an integer variable named Number that is set to 6. Using the multiplication operator, the value is adjusted inside the print statement. +Without running the code, what is the value of Number? + +```java stage0/snippets/operators.java#multiplication + +``` + +
+ Answer +
+

+ Number's new value is 12. This is because Number holds the value 6. + When multiplied by 2, 6 becomes 12, which is the new value of + Number{' '} +

+
+
+ + + +The ++ and -- operators are often used to change the value of the code as well. +They help with incrementing and decrementing values and are used in conditionals (which are covered in a later stage). +For now, we can use the operators to change variables without using conditionals. +Like mentioned before, ++ increments the value of a variable by adding 1 and -- decrements the value of a variable by subtracting 1. + +In the example below, we have two variables. + +```java stage0/snippets/operators.java#increments + +``` + +In the first print statement, we have ++x. +This means it takes the value of x and increments it by 1 which gives us 7 because 6 + 1 is 7. +In the second print statement, we have --y. +This means that it takes the value of y, decrements it by 1 which gives us 6 since 7 - 1 is 6. + +## Assignment Operators + +Assignment operators are used when assigning or updating the values in a variable. +These include: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OperatorDescriptionExample
{'='}Assigns a value to a variablea = 2
{'+='} + Addition: Takes the current value of the variable, adds the + stated amount then assigns the result to the variable. It's the + same as x = x + y{' '} + a += 3
{'-='} + Subtraction: Takes the current value of the variable, subtracts + the stated amount then assigns the result to the variable. It's + the same as x = x - y{' '} + a {'-='} 4
{'*='} + Multiplication: Takes the current value of the variable, + multiplies the stated amount then assigns the result to the + variable. It's the same as x = x * y + a {'*='} 5
{'/='} + Division: Takes the current value of the variable, divides the + stated amount then assigns the result to the variable. It's the + same as x = x / y + a {'/='} 6
+ +Assignment Operators are similar to Arithmetic Operators, they are shorthand. +Using the shorthand version can help make code easier to read. +It prevents having to write the variable name twice which can also be helpful in preventing code issues. + +In the example below, we have variables A which is set to 10, and variable B which is set to 5. Since += adds 1, A will hold the value of 11. Similarly, B will now hold the value of 4 + +```java stage0/snippets/operators.java#arithmetic + +``` + +## Comparison Operators + +Comparison operators are symbols that tell the program how to compare values. +They can be used to help make decisions, which is an important part of programming. +Comparison operators are a main part of conditionals, which are discussed later in this stage. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OperatorDescriptionExample
{'=='} + Equals to: Checks if one value is the same values as the other + a == b
{'!='}Not Equal: Checks if one value is NOT equal to the othera != b
{'>'}Greater than: Checks if one value is greater than the othera {'>'} b
{'<'}Less than: Checks if one value is less than the othera {'<'} b
{'>='} + Greater than or equal too: Checks if one value is greater than + or equal to the other + a {'>='} b
{'<='} + Less than or equal too: Checks if one value is less than or + equal to the other + a {'<='} b
+ +Comparison Operators help programs make decisions because they can return if the value of a comparison is true or false. +If you remember from the previous section, these are booleans! + +Comparison operators are very similar to what you see in math problems. +In this example, we have two variables: A and B. +C is set to 2, and D is set to 4. The print statement compares the two variables using the greater than sign. +In the example below, it returns false because 2 is not greater than 4. + +```java stage0/snippets/operators.java#comparison + +``` + +## Logical Operators + +Logical operators are symbols that let a program make decisions by combining true or false statements. +They are also used when making decisions in a program and are also used in conditionals. + + + + + + + + + + + + + + + + + + + + + + + + + + +
OperatorDescriptionExample
{'&&'}and: true if both statements are truetrue && true // this is true
{'||'}Or: true if at least one statement is truetrue || false // this is true
{'!'}Not: reverses. If true, then false. If false then true !true // this changes to false
+ +In the example below we have 2 variables: AnswerOne and AnswerTwo. +We know AnswerOne is true and AnswerTwo is false. +Using that information and without running the code, what should the print statements print out? + +```java stage0/snippets/operators.java#logical + +``` + +
+ Answer +
+

+ * false. && returns true if both statements are true. AnswerTwo is + false, therefore the operator will return false * true. || returns + true if one of the statements are true. AnswerOne is true, therefore + the the operator will return true * false. ! reverses the outcome. + AnswerOne is true, adding ! will reverse it and return false +

+
+
diff --git a/src/content/docs/learning-course/intro-to-java/stage-overview.mdx b/src/content/docs/learning-course/intro-to-java/stage-overview.mdx index 3801dd2..c42068e 100644 --- a/src/content/docs/learning-course/intro-to-java/stage-overview.mdx +++ b/src/content/docs/learning-course/intro-to-java/stage-overview.mdx @@ -16,4 +16,8 @@ This will help ensure that you don’t miss any important information. This stage will cover the following topics -- Java Fundamentals +- + {' '} + Java Fundamentals{' '} + +- Operators