Exercises: Control Flow and Collections

Work on these exercises in the java-web-dev-projects repo,creating a new class for each item. You may call these classes whatever you like, but remember to use the proper Java naming conventions.

  1. Navigate inside control-flow-and-collections within java-web-dev-projects and create a new folder called exercises.
  2. Create a new IntelliJ project within your new exercises directory called collections-exercises.

Now you are ready to start creating new classes and practice using different collections!

Array Practice

  1. Create and initialize an array with the following values in a single line: 1, 1, 2, 3, 5, 8.

    int[] integerArray = {1, 1, 2, 3, 5, 8};
  2. Loop through the array and print out each value, then modify the loop to only print the odd numbers.

  3. For this exercise, use the string “I would not, could not, in a box. I would not, could not with a fox. I will not eat them in a house. I will not eat them with a mouse.” Use the split method to divide the string at each space and store the individual words in an array. If you need to review the method syntax, look back at the string methods table.

    String phrase = "I would not, could not, in a box. I would not, could not with a fox. I will not eat them in a house. I will not eat them with a mouse.";
    String[] words = phrase.split(" ");
    System.out.println(Arrays.toString(words));
  4. Print the array of words to verify that your code works. The syntax is:

       System.out.println(Arrays.toString(arrayName));
  5. Repeat steps 3 and 4, but change the delimiter to split the string into separate sentences.

    String[] sentences = phrase.split("\\.");
    System.out.println(Arrays.toString(sentences));
Note

Some characters, like a period ".", have special meanings when used with the split method. They cannot be used as-is for the delimiter.

To use these characters as the delimiter, we must escape their special meanings. Instead of .split("."), we need to use .split("\\.").

ArrayList Practice

  1. Write a static method to find the sum of all the even numbers in an ArrayList. Within main, create a list with at least 10 integers and call your method on the list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    public static int sumEven(ArrayList<Integer> arr) {
    int total = 0;
    for (int integer : arr) {
       if (integer % 2 == 0) {
          total += integer;
       }
    }
    return total;
    }
  2. Write a static method to print out each word in a list that has exactly 5 letters.

  3. Modify your code to prompt the user to enter the word length for the search.

    System.out.println("Enter a word length: ");
    int numChars = input.nextInt();
  4. BONUS: Instead of creating our own list of words, what if we want to use the string from the Array Practice section? Search “Java convert String to ArrayList” online to see how to split a string into the more flexible ArrayList collection.

HashMap Practice

Make a program similar to GradebookHashMap that does the following:

  1. It takes in student names and ID numbers (as integers) instead of names and grades.
  2. The keys should be the IDs and the values should be the names.
  3. Modify the roster printing code accordingly.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
do {
   System.out.print("Student: ");
   newStudent = input.nextLine();

   if (!newStudent.equals("")) {
      System.out.print("ID: ");
      Integer newID = input.nextInt();
      classRoster.put(newID, newStudent);

      input.nextLine();
   }
} while(!newStudent.equals(""));

input.close();

System.out.println("\nClass roster:");

for (Map.Entry<Integer, String> student : classRoster.entrySet()) {
   System.out.println(student.getValue() + "'s ID: " + student.getKey());
}

System.out.println("Number of students in roster: " + classRoster.size());