diff --git a/examples/build.gradle b/examples/build.gradle index 6923a8c..01dc71e 100644 --- a/examples/build.gradle +++ b/examples/build.gradle @@ -16,7 +16,7 @@ spotless { java { target fileTree(rootDir) { include "**/*.java" - exclude "**/build/**", "**/.gradle/**", "stage0/snippets/**", "stage1/snippets/**" + exclude "**/build/**", "**/.gradle/**", "stage0/snippets/**", "stage0/solutions/**", "stage1/snippets/**" } licenseHeaderFile rootProject.file('gradle/LICENSE_HEADER') } diff --git a/examples/stage0/solutions/.gitignore b/examples/stage0/solutions/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/examples/stage0/solutions/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/examples/stage0/solutions/build.gradle b/examples/stage0/solutions/build.gradle new file mode 100644 index 0000000..1be82b4 --- /dev/null +++ b/examples/stage0/solutions/build.gradle @@ -0,0 +1,47 @@ +plugins { + id 'java' + id "com.diffplug.spotless" version "8.0.0" +} + +java { + sourceCompatibility = JavaVersion.VERSION_25 + targetCompatibility = JavaVersion.VERSION_25 +} + +sourceSets { + main { + java { + setSrcDirs(['src']) + } + } +} + +def solutionFiles = fileTree('src') { include '**/*.java' }.files +def runnableFiles = solutionFiles.findAll { it.text.contains('void main(') } +def solutionRunTasks = runnableFiles.collect { src -> + def className = src.name - '.java' + tasks.register("run${className}", JavaExec) { + dependsOn 'compileJava' + classpath = sourceSets.main.output + mainClass = className + } +} + +tasks.register('runSolutions') { + dependsOn solutionRunTasks +} + +spotless { + java { + target fileTree('.') { include '**/*.java' } + licenseHeaderFile(rootProject.file('../../gradle/LICENSE_HEADER'), "^(?!/\\*|\\s*\\*)") + } +} + +tasks.named('compileJava') { + dependsOn 'spotlessJavaCheck' +} + +tasks.named('check') { + dependsOn 'runSolutions' +} diff --git a/examples/stage0/solutions/gradle/wrapper/gradle-wrapper.jar b/examples/stage0/solutions/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..d997cfc Binary files /dev/null and b/examples/stage0/solutions/gradle/wrapper/gradle-wrapper.jar differ diff --git a/examples/stage0/solutions/gradle/wrapper/gradle-wrapper.properties b/examples/stage0/solutions/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..c61a118 --- /dev/null +++ b/examples/stage0/solutions/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/examples/stage0/solutions/gradlew b/examples/stage0/solutions/gradlew new file mode 100755 index 0000000..739907d --- /dev/null +++ b/examples/stage0/solutions/gradlew @@ -0,0 +1,248 @@ +#!/bin/sh + +# +# Copyright © 2015 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/2d6327017519d23b96af35865dc997fcb544fb40/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/examples/stage0/solutions/gradlew.bat b/examples/stage0/solutions/gradlew.bat new file mode 100644 index 0000000..c4bdd3a --- /dev/null +++ b/examples/stage0/solutions/gradlew.bat @@ -0,0 +1,93 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/examples/stage0/solutions/settings.gradle b/examples/stage0/solutions/settings.gradle new file mode 100644 index 0000000..cf23f37 --- /dev/null +++ b/examples/stage0/solutions/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'stage0-solutions' diff --git a/examples/stage0/solutions/src/Conditionals.java b/examples/stage0/solutions/src/Conditionals.java new file mode 100644 index 0000000..fc33491 --- /dev/null +++ b/examples/stage0/solutions/src/Conditionals.java @@ -0,0 +1,60 @@ +/* + * Copyright 2026 FRCSoftware + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +void main() { + // Using the variable `shooterEnabled` below, decide whether to turn on + // (shooterVelocity = 1) or turn off (shooterVelocity = 0) the shooter motors. + // Then print the value of `shooterVelocity`. After running, change + // `shooterEnabled` to `true`; the code should now print 1 + boolean shooterEnabled = false; + int shooterVelocity; + if (shooterEnabled == true) { + shooterVelocity = 1; + } else { + shooterVelocity = 0; + } + System.out.println(shooterVelocity); + + // Using only the NOT EQUALS operator, and the variable `statusCode` below, + // print "Success!" if `statusCode` is "OK", and "There was an error." + // otherwise. After running, change `statusCode` to "OK"; the code should + // now print "Success!" + String statusCode = "ERROR"; + if (statusCode != "OK") { + System.out.println("There was an error."); + } else { + System.out.println("Success!"); + } + + // If the value of `shooterAngleDeg` exceeds 75 degrees OR is less than 10 degrees, + // print "The shooter has been stopped." Otherwise, print "The shooter is moving." + // After running, change `shooterAngleDeg` to 76; the code should now print + // "The shooter has been stopped." + int shooterAngleDeg = 54; + if (shooterAngleDeg > 75 || shooterAngleDeg < 10) { + System.out.println("The shooter has been stopped."); + } else { + System.out.println("The shooter is moving."); + } + + // Sanitize and "deadband" the below input using the logical AND operator; + // if `input` is between 5 and 100 (inclusive), print "Controller input detected" + // Otherwise, print "Discarding input." + // After running, change `input` to 2; the code should now print "Discarding input." + double input = 20; + if ((input >= 5) && (input <= 100)) { + System.out.println("Controller input detected."); + } else { + System.out.println("Discarding input."); + } + + // Define a variable `intakeEnabled` whose value is the inverse of `intakeDisabled`, + // by using the logical NOT operator. Then print the value of `intakeEnabled`. + // After running, change `intakeDisabled` to `true`; the code should now print + // `false`. + boolean intakeDisabled = false; + boolean intakeEnabled = !intakeDisabled; +} \ No newline at end of file diff --git a/examples/stage0/solutions/src/Operators.java b/examples/stage0/solutions/src/Operators.java new file mode 100644 index 0000000..4009be5 --- /dev/null +++ b/examples/stage0/solutions/src/Operators.java @@ -0,0 +1,71 @@ +/* + * Copyright 2026 FRCSoftware + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +void main() { + // Create two integer variables, `first` and `second`. Assign `first` + // a value of 5, and `second` a value of 4. Then, print the sum of + // `first` and `second`. + int first = 5; + int second = 4; + System.out.println(first + second); + + // Create an integer variable `result`. Using Java math operators, multiply + // the sum of 1 and 6 by the difference of 7 and 8, and store it in this + // variable. Then, print the value of `result`. + int result = (1 + 6) * (7 - 8); + System.out.println(result); + + // Given the below variable `rotPerSec`, calculate the corresponding + // amount of rotations per hour and store it in a variable `rotPerHour`. + // Then, print the value of `rotPerHour`. + int rotPerSec = 5; + int rotPerHour = rotPerSec * 60 * 60; + System.out.println(rotPerHour); + + // Create an integer variable `quotient1`, and assign to it the value + // of 5 divided by 3. Then print the value of `quotient1`. + int quotient1 = 5 / 3; + System.out.println(quotient1); + + // Create a variable `quotient2` of type double, and assign to it the + // value of 5.0 divided by 3. Then print the value of `quotient2`. + double quotient2 = 5.0 / 3; + System.out.println(quotient2); + + // Convert `rawEncoderDegrees` to its corresponding degree amount on a scale + // from 0 to 360, and store this value in a variable `normalizedAngle`. + // Then print the value of `normalizedAngle`. + int rawEncoderDegrees = 560; + int normalizedAngle = rawEncoderDegrees % 360; + System.out.println(normalizedAngle); + + // Using only the increment operator, add 1 to the `timer` variable below. + // Then print the value of `timer`. + int timer = 1000; + timer++; + System.out.println(timer); + + // Using only the decrement operator, subtract 1 from the `secondsLeftInMatch` + // variable below. Then print the value of `secondsLeftInMatch`. + int secondsLeftInMatch = 85; + secondsLeftInMatch--; + System.out.println(secondsLeftInMatch); + + // Print the sum, difference, product, and quotient of the two variables + // below. Especially take note of how the quotient is printed. + int num1 = 15; + int num2 = 2; + System.out.println(num1 + num2); + System.out.println(num1 - num2); + System.out.println(num1 * num2); + System.out.println(num1 / num2); + + // Create a variable `avg` of type `double`, and assign to it the average + // of the values 2, 5, and 4. Then print the value of `avg`. + // HINT: How can you ensure the result is not improperly rounded? + double avg = (2 + 5 + 4) / 3.0; + System.out.println(avg); +} \ No newline at end of file diff --git a/examples/stage0/solutions/src/Print.java b/examples/stage0/solutions/src/Print.java new file mode 100644 index 0000000..1839c77 --- /dev/null +++ b/examples/stage0/solutions/src/Print.java @@ -0,0 +1,37 @@ +/* + * Copyright 2026 FRCSoftware + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +void main() { + // Print "Hello World!", with a newline at the end. + System.out.println("Hello World!"); + + // Print the following lines, each with a separate `println()` statement: + // The robot knows where it is at all times. + // It knows this because it knows where it isn't. + System.out.println("The robot knows where it is at all times."); + System.out.println("It knows this because it knows where it isn't."); + + // Define a variable `pi` that is equal to 3.14159. + double pi = 3.14159; + // Define a 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. + System.out.println(pi + " " + g + " " + mode); + + // Now, change pi to equal 3.142857. Then, print the value of `pi` again. + pi = 3.142857; + System.out.println(pi); + + // Create a variable `degrees` of type `double` and assign it a value of + // 360. Then, print the variable to observe the type narrowing behavior + // (it stores as a double, although an integer was passed to it). + double degrees = 360; + System.out.println(degrees); +} \ No newline at end of file diff --git a/src/content/docs/learning-course/stage0/conditionals.mdx b/src/content/docs/learning-course/stage0/conditionals.mdx index c785037..7247753 100644 --- a/src/content/docs/learning-course/stage0/conditionals.mdx +++ b/src/content/docs/learning-course/stage0/conditionals.mdx @@ -181,7 +181,9 @@ Since the two conditionals were false, this means the `else` runs and it prints ## Conditional Exercise diff --git a/src/content/docs/learning-course/stage0/java-fundamentals.mdx b/src/content/docs/learning-course/stage0/java-fundamentals.mdx index 5d91691..654ec3b 100644 --- a/src/content/docs/learning-course/stage0/java-fundamentals.mdx +++ b/src/content/docs/learning-course/stage0/java-fundamentals.mdx @@ -150,4 +150,21 @@ For example, this is a comment with two lines of text. ## Java Fundamentals Exercise - + diff --git a/src/content/docs/learning-course/stage0/operators.mdx b/src/content/docs/learning-course/stage0/operators.mdx index 0b27546..17a0f23 100644 --- a/src/content/docs/learning-course/stage0/operators.mdx +++ b/src/content/docs/learning-course/stage0/operators.mdx @@ -365,4 +365,10 @@ Using that information and without running the code, what should the print state ## Operators Exercise - +