Unit 4B - Toggle Code Practice 4b ArrayList (4.7-4.10)
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods10 exercises9 graded tests
Files
Unit 4B - Toggle Code Practice 4b ArrayList (4.7-4.10)
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
The following program should create an ArrayList called conversation, add in some elements("hello", "goodbye", "how are you", and "see you later"), and print out the elements with ", " after each. Fill in the code so that it adds the elements to conversation. The rest of the program is finished for you.
Open the
01-u4b-toggle1folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 2
Fix the two errors in the printBackwards method so that it will correctly iterate through the parameter myList backwards and print each element.
Open the
02-u4b-toggle2folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 3
Write code to define the removeZeros method. This method should take in an ArrayList of integers listOfNums and delete all of the zeros. For example, {3, 0, 5, 0} would change into {3, 5}.
Open the
03-u4b-toggle3folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 4
Write code for the findSmallest method. This code segment should take in an ArrayList nums and return the smallest element present. For example, findSmallest called on {5, 3, 1, 6} should return 1.
Open the
04-u4b-toggle4folder and editListTest.java. This one is not graded, so check it yourself.
Exercise 5
Write code to flesh out the removeOdd method. This method should take in a parameter nums and delete every odd number from it. For example, {5, 3, 2, 1, 4} should become {2, 4}.
Open the
05-u4b-toggle5folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 6
Fill out the average method. It should take in an ArrayList nums and calculate the arithmetic mean (the sum divided by the length). For example, average called on {5, 9, 6} should return 6.66666666667 as that is (5 + 9 + 6) / 3.
Open the
06-u4b-toggle6folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 7
Create the moveLargest method. This should find the largest value in an ArrayList of Integers (the parameter) and move it to the back of the list.
Open the
07-u4b-toggle7folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 8
Write code to finish the removeShort method. It should take an ArrayList words and remove all elements that are three characters long or shorter. For example, {"Dog", "Monkey", "Lion", "Cat"} would become {"Monkey", "Lion"}.
Open the
08-u4b-toggle8folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 9
Write the method doubleList. This should take in an ArrayList words and insert a copy of each element such that {"cat", "ribbon", "house"} would become {"cat", "cat", "ribbon", "ribbon", "house", "house"}.
Open the
09-u4b-toggle9folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 10
Write the method removeElement. This should take in an ArrayList nums and an integer toRemove and remove every instance of that integer from nums. E.g., if nums was {3, 6, 5, 3, 4}, it should become {6, 5, 4} after calling removeElement(nums, 3).
Open the
10-u4b-toggle10folder and editListTest.java. Press Run to try it; Run Tests checks it (1 test).
For teachers
Unit 4B - Toggle Code Practice 4b ArrayList (4.7-4.10)
Students read CSAwesome2's “Unit 4B - Toggle Code Practice 4b ArrayList (4.7-4.10)” in CS Room and work through its 10 Java coding exercises (9 graded, 1 ungraded).
- Time: 2 class periods
- Tests: 9 in 9 graded exercises, set up for you when you import the lesson
Goals
- Practice for topics 4.7–4.10: 4.7 Wrapper Classes - Integer and Double, 4.8 ArrayList and its Methods, 4.9 ArrayList Traversals, 4.10 Implementing ArrayList Algorithms.
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/unit4b-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-u4b-toggle1 | ListTest.java | Autograded | 1 |
02-u4b-toggle2 | ListTest.java | Autograded | 1 |
03-u4b-toggle3 | ListTest.java | Autograded | 1 |
04-u4b-toggle4 | ListTest.java | Not graded; students check it themselves | 0 |
05-u4b-toggle5 | ListTest.java | Autograded | 1 |
06-u4b-toggle6 | ListTest.java | Autograded | 1 |
07-u4b-toggle7 | ListTest.java | Autograded | 1 |
08-u4b-toggle8 | ListTest.java | Autograded | 1 |
09-u4b-toggle9 | ListTest.java | Autograded | 1 |
10-u4b-toggle10 | ListTest.java | Autograded | 1 |
Teachers can download a reference solution after signing in.