Unit 4B - Code Practice with ArrayLists

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java2 class periods10 exercises20 graded tests

Files

README.mdDownload

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-ArrayListPractice1 folder and edit Test1.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-ArrayListPractice2 folder and edit Test1.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-ArrayListPractice3 folder and edit Test1.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-ArrayListPractice4 folder and edit Test1.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-ArrayListPractice5 folder and edit Test1.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-ArrayListPractice6 folder and edit Test1.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-ArrayListPractice7 folder and edit Test1.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-ArrayListPractice8 folder and edit Test1.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-ArrayListPractice9 folder and edit Test1.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-ArrayListPractice10 folder and edit Test1.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

In class

  1. Students read README.md.
  2. Students do exercises 01–10, each in its own folder, pressing Run to try their code.
  3. 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

FolderFileGradingTests
01-ArrayListPractice1Test1.javaAutograded1
02-ArrayListPractice2Test1.javaAutograded2
03-ArrayListPractice3Test1.javaAutograded3
04-ArrayListPractice4Test1.javaAutograded2
05-ArrayListPractice5Test1.javaAutograded2
06-ArrayListPractice6Test1.javaAutograded2
07-ArrayListPractice7Test1.javaAutograded2
08-ArrayListPractice8Test1.javaAutograded2
09-ArrayListPractice9Test1.javaAutograded2
10-ArrayListPractice10Test1.javaAutograded2

Teachers can download a reference solution after signing in.