Unit 4B - Code Practice with ArrayLists
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods10 exercises20 graded tests
Files
Unit 4B - Code Practice with ArrayLists
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.
Exercise 1
Fix the code so that it compiles. The code should instantiate an ArrayList of Strings names and fill it with the Strings from the array friends. It should then print out names.
Open the
01-ArrayListPractice1folder and editTest1.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 2
Fix the following class so that it will compile and the method reverse will return an ArrayList containing Integers in the reversed order of the ArrayList parameter list. Hint: for this solution, only one line needs to be added to the for-loop inside of the reverse method.
Open the
02-ArrayListPractice2folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 3
Fix the following method printEvenIndex so that it will print out the Integers at even indices of the passed-in ArrayList list.
Open the
03-ArrayListPractice3folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 4
Fix the following method printEvenElements so that it will print out all of the even Integers that are in the passed-in ArrayList list.
Open the
04-ArrayListPractice4folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 5
Rewrite the code so that it fills the ArrayList values with the elements of the array nums using a for-each loop instead of a for loop.
Open the
05-ArrayListPractice5folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 6
Finish the following method sumNegValues to return the sum of all of the negative numbers in the ArrayList list, the parameter.
Open the
06-ArrayListPractice6folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 7
Finish the following method removeLongStrings that checks each element of the passed in ArrayList list and removes any that are strictly longer than 4 characters.
Open the
07-ArrayListPractice7folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 8
Fill in the method shiftLeftOne to shift all of the elements of the passed-in ArrayList list left by one. The original first element should be wrapped to the back of the list after the shift. Ex: {1, 2, 3, 4} should turn into {2, 3, 4, 1}
Open the
08-ArrayListPractice8folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 9
Finish the method moveSmallest so that it finds the smallest value in the passed-in ArrayList list and moves it to the front of the list.
Open the
09-ArrayListPractice9folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 10
Finish the method findLongest to find and return the longest String in the ArrayList of Strings list.
Open the
10-ArrayListPractice10folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Unit 4B - Code Practice with ArrayLists
Students read CSAwesome2's “Unit 4B - Code Practice with ArrayLists” 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 Unit 4: Data Collections.
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/ArrayListPractice.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-ArrayListPractice1 | Test1.java | Autograded | 1 |
02-ArrayListPractice2 | Test1.java | Autograded | 2 |
03-ArrayListPractice3 | Test1.java | Autograded | 3 |
04-ArrayListPractice4 | Test1.java | Autograded | 2 |
05-ArrayListPractice5 | Test1.java | Autograded | 2 |
06-ArrayListPractice6 | Test1.java | Autograded | 2 |
07-ArrayListPractice7 | Test1.java | Autograded | 2 |
08-ArrayListPractice8 | Test1.java | Autograded | 2 |
09-ArrayListPractice9 | Test1.java | Autograded | 2 |
10-ArrayListPractice10 | Test1.java | Autograded | 2 |
Teachers can download a reference solution after signing in.