Selection Toggle Code Practice (2.1-2.6)
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods11 exercises37 graded tests
Files
Selection Toggle Code Practice (2.1-2.6)
Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.
For each of the coding problems below, write your own solution.
Exercise 1
Write code that tests guess to see if it is equal to answer or too high or too low. If it is too high as in the example below, it should print out Your guess is too high.
Open the
01-u2a-toggle1folder and editTest1.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 2
Write code that prints You can go out if you don’t have any homework and have cleaned and otherwise prints You can not go out.
Open the
02-u2a-toggle2folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 3
Write code that tests if x is between 1 and 10, and prints 1 <= x <= 10 or x is not in range.
Open the
03-u2a-toggle3folder and editTest1.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 4
Write code that prints out if the string message has the word ringing in it or not. It should print out Answer the phone! if ringing is in message, and I don't hear anything. if not.
Open the
04-u2a-toggle4folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 5
Write code that prints if your favorite food is junk food (pizza or wings) or not. Your code should check to see if the variable favFood value is pizza or wings. If it is, it should print out Your fav is junk food. If not, it should print Your fav is not junk.
Open the
05-u2a-toggle5folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 6
Write code that prints your fine if you are speeding. If you are going over 65 but less than 75, the fine is 50. If you are going at least 75 and less than 85, the fine is 100. Over that the fine is 200. It should not print anything if you are not speeding.
Open the
06-u2a-toggle6folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 7
Write code that prints the alarm time. If it is a weekday you should get up at 7:00am and if not get up at 10:00am.
Open the
07-u2a-toggle7folder and editTest1.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 8
Write code that prints if you Can text now or Can't text now. You can text if you are not driving and not eating.
Open the
08-u2a-toggle8folder and editTest1.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 9
Write code that prints whether your name Starts with a vowel (a, e, i, o, u) or Starts with a consonant.
Open the
09-u2a-toggle9folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 10
Write code that prints the letter grade with your score. For a score greater than 90, it should print A. For a score between 80 and 90, it should print B. For a score between 70 and 80, it should print C. For a score between 60 and 70, it should print D. For all other scores, it should print E.
Open the
10-u2a-toggle10folder and editTest1.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 11
Complete the get_mid(str) method in the StringWorker class to return the middle character if the string has an odd number of characters and the middle two characters if the string has an even number of characters. For example, get_mid("way") should return "a" and get_mid("away") should return "wa".
Open the
11-u2a-toggle11folder and editStringWorker.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Selection Toggle Code Practice (2.1-2.6)
Students read CSAwesome2's “Selection Toggle Code Practice (2.1-2.6)” in CS Room and work through its 11 Java coding exercises.
- Time: 2 class periods
- Tests: 37 in 11 graded exercises, set up for you when you import the lesson
Goals
- Practice for topics 2.1–2.6: 2.1 Algorithms with Selection and Repetition, 2.2 Boolean Expressions, 2.3 if Statements, 2.4 Nested if Statements, 2.5 Compound Boolean Expressions, 2.6 Comparing Boolean Expressions (De Morgan’s Laws).
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/unit2a-practice-toggle-code.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–11, each in its own folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-u2a-toggle1 | Test1.java | Autograded | 4 |
02-u2a-toggle2 | Test1.java | Autograded | 3 |
03-u2a-toggle3 | Test1.java | Autograded | 4 |
04-u2a-toggle4 | Test1.java | Autograded | 3 |
05-u2a-toggle5 | Test1.java | Autograded | 3 |
06-u2a-toggle6 | Test1.java | Autograded | 3 |
07-u2a-toggle7 | Test1.java | Autograded | 4 |
08-u2a-toggle8 | Test1.java | Autograded | 4 |
09-u2a-toggle9 | Test1.java | Autograded | 3 |
10-u2a-toggle10 | Test1.java | Autograded | 4 |
11-u2a-toggle11 | StringWorker.java | Autograded | 2 |
Teachers can download a reference solution after signing in.