Unit 4A - Toggle Code Practice 4a Arrays (4.1-4.6)
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods10 exercises20 graded tests
Files
Unit 4A - Toggle Code Practice 4a Arrays (4.1-4.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 a loop that traverses the array arr from 0 to less than the length of the array. In the loop, double each element in the array arr and print out each new value on the same line separated by “, “. The finished code should print “2, 4, 6, 8, 10, “.
Open the
01-u4a-toggle1folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 2
Write a for loop that fills an array with elements that count up from 0 to 50 by 5 (0 5 10 15 20 … 50) and prints out each element on a separate line.
Open the
02-u4a-toggle2folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 3
Write an enhanced for each loop to traverse the array and print out an element if it is even.
Open the
03-u4a-toggle3folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 4
Finish the method to return the smallest integer in an array arr given as the parameter.
Open the
04-u4a-toggle4folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 5
Finish the method to return the average of the elements in an array arr given as the parameter. The method should return the average.
Open the
05-u4a-toggle5folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 6
Finish the method to return the largest integer in an array arr given as the parameter.
Open the
06-u4a-toggle6folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 7
Finish the method that given an array as a parameter, returns the elements “right shifted” by one – so {6, 2, 5, 3} returns {3, 6, 2, 5}.
Open the
07-u4a-toggle7folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 8
Finish the method to return a new array of length 2 containing the middle two elements of a given array of integers of even length (the parameter) – so {1,2,3,4} should return {2,3}.
Open the
08-u4a-toggle8folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 9
Finish the method that should return string array that is in reverse order – so {"b", "a", "z"} should return {"z", "a", "b"}.
Open the
09-u4a-toggle9folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 10
Finish the method so that it copies the first half of an array given as an argument to the method into a result array which is returned.
Open the
10-u4a-toggle10folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Unit 4A - Toggle Code Practice 4a Arrays (4.1-4.6)
Students read CSAwesome2's “Unit 4A - Toggle Code Practice 4a Arrays (4.1-4.6)” 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 4.1–4.6: 4.1 Ethical and Social Issues Around Data Collection, 4.2 Data Sets, 4.3 Array Creation and Access, 4.4 Array Traversals, 4.5 Implementing Array Algorithms, 4.6 Using Text Files.
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/unit4a-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-u4a-toggle1 | Test1.java | Autograded | 2 |
02-u4a-toggle2 | Test1.java | Autograded | 2 |
03-u4a-toggle3 | Test1.java | Autograded | 2 |
04-u4a-toggle4 | Test1.java | Autograded | 2 |
05-u4a-toggle5 | Test1.java | Autograded | 2 |
06-u4a-toggle6 | Test1.java | Autograded | 2 |
07-u4a-toggle7 | Test1.java | Autograded | 2 |
08-u4a-toggle8 | Test1.java | Autograded | 2 |
09-u4a-toggle9 | Test1.java | Autograded | 2 |
10-u4a-toggle10 | Test1.java | Autograded | 2 |
Teachers can download a reference solution after signing in.