diff --git a/examples/stage0/solutions/Conditionals.java b/examples/stage0/solutions/Conditionals.java new file mode 100644 index 0000000..982a7bb --- /dev/null +++ b/examples/stage0/solutions/Conditionals.java @@ -0,0 +1,54 @@ +void main() { + // Using the variable `turretEnabled` below, decide whether to turn on + // (turretVelocity = 1) or turn off (turretVelocity = 0) the shooter motors. + // Then print the value of `turretVelocity`. After running, change + // `shooterEnabled` to `true`; the code should now print 1 + boolean shooterEnabled = false; + int turretVelocity; + if (shooterEnabled == true) { + turretVelocity = 1; + } else { + turretVelocity = 0; + } + System.out.println(turretVelocity); + + // Using only the NOT EQUALS TO 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/Operators.java b/examples/stage0/solutions/Operators.java new file mode 100644 index 0000000..2618a88 --- /dev/null +++ b/examples/stage0/solutions/Operators.java @@ -0,0 +1,65 @@ +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`. Multiply the sum of 1 + 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`, create a variable `rotPerHour` + // whose value represents the corresponding amount of rotations per + // hour for `rotPerSec`. 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`. + double 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); + + // Create a variable `normalizedAngle` that takes `rawEncoderDegrees` + // and calculates its "true" value (as a degree amount from 0 to 360). + // Then print the value of `normalizedAngle`. + int rawEncoderDegrees = 560; + int normalizedAngle = 560 % 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/Print.java b/examples/stage0/solutions/Print.java new file mode 100644 index 0000000..a6670c3 --- /dev/null +++ b/examples/stage0/solutions/Print.java @@ -0,0 +1,33 @@ +public class Print { + public static void main(String args[]) { + // 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