Loops Toggle Code Practice (2.7-2.12)
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods10 exercises20 graded tests
Files
Loops Toggle Code Practice (2.7-2.12)
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
Finish the code to print out all the values from 20 to 30 (20, 21, 22, … 30).
Open the
01-u2b-toggle1folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests). The tests expect each value on its own line.
Exercise 2
Finish the code to print a countdown from 15 to 0 (15, 14, 13, … 0).
Open the
02-u2b-toggle2folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests). The tests expect each value on its own line.
Exercise 3
Finish the code to print up from 0 to 50 by 5 (0, 5, 10, 15 … 50).
Open the
03-u2b-toggle3folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests). The tests expect each value on its own line.
Exercise 4
Finish the code to print out the values from 0 to 100 by 20’s (0, 20, 40, … 100).
Open the
04-u2b-toggle4folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests). The tests expect each value on its own line.
Exercise 5
Finish the code to print out the values from 100 to 0 by 10’s (100, 90, 80, … 0).
Open the
05-u2b-toggle5folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests). The tests expect each value on its own line.
Exercise 6
Finish the code to print 3 rows with 6 * in each row. Be sure to use two loops.
Open the
06-u2b-toggle6folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 7
Finish the code to print 1 (followed by a newline), then 22 (followed by a newline), and then 333 (followed by a newline). Be sure to use two loops.
Open the
07-u2b-toggle7folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 8
Finish the code to print 11111, 22222, 33333, 44444, and 55555. Be sure to use two loops.
Open the
08-u2b-toggle8folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests). The tests expect each value on its own line.
Exercise 9
Finish the code to print 11111, 2222, 333, 44, 5. Be sure to use two loops.
Open the
09-u2b-toggle9folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests). The tests expect each value on its own line.
Exercise 10
The even_div(s, n) method in the NumWorker class should return the number of digits in s (a string representation of a number) that are evenly divisible by n. For example, even_div("6892",3) should return 2 since both 6 and 9 are evenly divisible by 3.
Open the
10-u2b-toggle10folder and editNumWorker.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Loops Toggle Code Practice (2.7-2.12)
Students read CSAwesome2's “Loops Toggle Code Practice (2.7-2.12)” in CS Room and work through its 10 Java coding exercises.
- Time: 2 class periods
- Tests: 20 in 10 graded exercises, set up for you when you import the lesson
Goals
- Practice for topics 2.7–2.12: 2.7 While Loops, 2.8 For Loops, 2.9 Implementing Selection and Iteration Algorithms, 2.10 Implementing String Algorithms, 2.11 Nested Iteration, 2.12 Informal Runtime Analysis of Loops.
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/unit2b-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–10, 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-u2b-toggle1 | Test1.java | Autograded | 2 |
02-u2b-toggle2 | Test1.java | Autograded | 2 |
03-u2b-toggle3 | Test1.java | Autograded | 2 |
04-u2b-toggle4 | Test1.java | Autograded | 2 |
05-u2b-toggle5 | Test1.java | Autograded | 2 |
06-u2b-toggle6 | Test1.java | Autograded | 2 |
07-u2b-toggle7 | Test1.java | Autograded | 2 |
08-u2b-toggle8 | Test1.java | Autograded | 2 |
09-u2b-toggle9 | Test1.java | Autograded | 2 |
10-u2b-toggle10 | NumWorker.java | Autograded | 2 |
Teachers can download a reference solution after signing in.