Mixed Up Code PracticeΒΆ

Try to solve each of the following. Click the Check Me button to check each solution. You will be told if your solution is too short, has a block in the wrong order, or you are using the wrong block. Some of the problems have an extra block or two that aren’t needed in the correct solution. Try to solve these on your phone or other mobile device!

        8-22-1: The following program segment should be a class that adds some Strings of conversational phrases to List and then prints them out.  But, the blocks have been mixed up and include one extra block that is not needed in a correct solution.  Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly.  Click the Check Me button to check your solution.

import java.util.List; import java.util.ArrayList; --- import java.util.List; import java.util.Arraylist; #distractor --- public class ListTest { --- public static void main(String[] args) { --- List conversation; conversation = new ArrayList(); --- conversation.add("hello"); conversation.add("goodbye"); conversation.add("how are you"); conversation.add("see you later"); --- for (String element: conversation) { --- System.out.print(element + ", "); --- } //end for loop } //end main method } //end class
        8-22-2: The following program segment should be a method that traverses through an ArrayList of Strings (the parameter) and print out the elements in reverse order -- so  {"cat", "dog", "mouse"} should print "mouse, dog, cat, " as output.  Assume the ArrayList "myList" has been instantiated and filled with Strings.  But, the blocks have been mixed up and include two extra blocks that are not needed in a correct solution.  Drag the needed blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution. 

public static void printBackwards(ArrayList myList) { --- for (int i = myList.size() - 1; i >= 0; i--) { --- for (int i = myList.size() - 1; i > 0; i--) { #distractor --- System.out.print(myList.get(i) + ", "); --- System.out.print(myList[i] + ", "); #distractor --- } //end for loop } //end printBackwards method
        8-22-3: The following program segment should remove all the zeros from an ArrayList of Integers.  Assume the ArrayList "listOfNums" has been instantiated and filled with Integers.  But, the blocks have been mixed up and include two extra blocks that are not needed in a correct solution.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution. 

Integer zero = new Integer(0); int i = 0; --- while (i < listOfNums.size()) { --- if (listOfNums.get(i).equals(zero)) { --- if (listOfNums.get(i) == 0) { #distractor --- listOfNums.remove(i); --- } //end if --- else { i++; } --- i++; #distractor --- } //end while loop
        8-22-4: The following program segment is a method that should return the smallest int given an ArrayList of Integers (the parameter).  But, the blocks have been mixed up and include one extra block that is not needed in a correct solution.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution. 

public static int findSmallest(ArrayList nums) { --- int smallest = nums.get(0); --- int smallest = nums[0]; #distractor --- for (int i = 0; i < nums.size(); i++) { --- if (nums.get(i) < min) { --- smallest = nums.get(i); --- } --- } //end for loop --- return smallest; --- } //end findSmallest method
        8-22-5: The following program segment is a method that should remove all the positive and negative odd values in an ArrayList of Integers (the parameter).  But, the blocks have been mixed up and include one extra block that is not needed in a correct solution.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution. 

public static void removeOdd(ArrayList nums) { --- int i = 0; --- while (i < nums.size()) { --- if (Math.abs(nums.get(i)) % 2 == 1) { --- if (nums.get(i) % 2 == 1) { #distractor --- nums.remove(i); --- } else { i++; } --- } //end while loop --- } //end removeOdd method
        8-22-6: The following program segment should be method that calculates the average from an ArrayList of Integers (the parameter).  But, the blocks have been mixed up and include one extra block that is not needed in a correct solution.  Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly.  Click the Check Me button to check your solution.

public static double average(ArrayList nums) { --- double sum = 0; --- for (int i = 0; i < nums.size(); i++) { --- for (int i = 0; i < nums.length; i++) { #distractor --- sum += nums.get(i); --- } //end for loop --- return (sum / nums.size()); --- } //end average method
        8-22-7: The following program segment is a method that should find the largest value given an ArrayList of Integers (the parameter) and move it to the back of the list.  But, the blocks have been mixed up and include two extra blocks that are not needed in a correct solution.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution. 

public static void moveLargest(ArrayList nums) { --- int largest = 0; --- for (int i = 0; i < nums.size(); i++) { --- if (nums.get(i) > nums.get(largest)) { --- if (nums[i] > nums[largest]) { #distractor --- largest = i; --- } --- } //end for loop --- Integer largestVal = nums.remove(largest); nums.add(largestVal); --- nums.add(largest); #distractor --- } //end moveLargest method
        8-22-8: The following program segment should be method that removes all the Strings that have length 3 or shorter from an ArrayList of Strings (the parameter) -- so {"catch", "dog", "tree", "me"} should return {"catch", "tree"}.  But, the blocks have been mixed up and include one extra block that is not needed in a correct solution.  Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly.  Click the Check Me button to check your solution.

public static void removeShort(ArrayList words) { --- int i = 0; --- while (i < words.size()) { --- if (words.get(i).length() <= 3) { --- if (words.get(i).length <= 3) { #distractor --- words.remove(i); --- } else { i++; } --- } //end while loop --- } //end removeShort method
        8-22-9: The following program segment is a method that should take each String from an ArrayList of Strings (the parameter) and add it again to the list -- so {"cat", "ribbon", "house"} should become {"cat", "cat", "ribbon", "ribbon", "house", "house"}.  But, the blocks have been mixed up and include two extra blocks that are not needed in a correct solution.  Drag the blocks from the left and put them in the correct order on the right.  Click the Check Me button to check your solution. 

public static void doubleList(ArrayList words) { --- int count = 0; --- while (count < words.size()) { --- while (count <= words.size()) { #distractor --- String toAdd = words.get(count); words.add(count, toAdd); --- count += 2; --- count++; #distractor --- } //end while loop --- } //end doubleList method
        8-22-10: The following program segment should be method that removes a specific Integer (specified in parameter) whenever it occurs in a given ArrayList of Integers (the parameter).  But, the blocks have been mixed up and include three extra blocks that are not needed in a correct solution.  Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly.  Click the Check Me button to check your solution.

public static void removeElement(ArrayList nums, int toRemove) { --- int i = 0; --- while (i < nums.size()) { --- while (i < nums.length) { #distractor --- if (nums.get(i) == toRemove) { --- if (nums.get(i) == nums(toRemove)) { #distractor --- nums.remove(i); --- } //end if --- else { i++; } --- i++; #distractor --- } //end while loop } //end average method
Next Section - Introduction to 2D Arrays