Skip to main content
Logo image

Section 12.14 Unit 4b ArrayList Write Code for Toggle Code

90 minutes
This is the ArrayList write code problems associated with the mixed up code problems.

Activity 12.14.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.

Activity 12.14.2.

Fix the two errors in the printBackwards method so that it will correctly iterate through the parameter myList backwards and print each element.

Activity 12.14.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}.

Activity 12.14.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.

Activity 12.14.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}.

Activity 12.14.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.

Activity 12.14.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.

Activity 12.14.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"}.

Activity 12.14.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"}.

Activity 12.14.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).
You have attempted of activities on this page.