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

README.mdDownload

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-toggle1 folder and edit ListTest.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-toggle2 folder and edit ListTest.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-toggle3 folder and edit ListTest.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-toggle4 folder and edit ListTest.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-toggle5 folder and edit ListTest.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-toggle6 folder and edit ListTest.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-toggle7 folder and edit ListTest.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-toggle8 folder and edit ListTest.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-toggle9 folder and edit ListTest.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-toggle10 folder and edit ListTest.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

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-u4b-toggle1ListTest.javaAutograded1
02-u4b-toggle2ListTest.javaAutograded1
03-u4b-toggle3ListTest.javaAutograded1
04-u4b-toggle4ListTest.javaNot graded; students check it themselves0
05-u4b-toggle5ListTest.javaAutograded1
06-u4b-toggle6ListTest.javaAutograded1
07-u4b-toggle7ListTest.javaAutograded1
08-u4b-toggle8ListTest.javaAutograded1
09-u4b-toggle9ListTest.javaAutograded1
10-u4b-toggle10ListTest.javaAutograded1

Teachers can download a reference solution after signing in.