1. Using generics

1.1. Cons Lists

We developed mutliple versions of our List implementations using Generics. The code from our lectures is available in the lecture notes repo.

Using the code in the lecture notes repo under folder 07 attempt the following exercises.

Practise Exercises
  • 1) Write black box tests for List found inside the package lists.finalgenericlist. Make sure you write tests for multiple different kinds of lists including lists of lists of list …​ etc.

1.2. Lists with mutation

In lecture 6 we developed another implemenation for lists of integers that uses mutation. The code for lecture 6 is available in the lecture notes repo.

Using the code in the lecture notes repo under folder 06 attempt the following exercises.

Practise Exercises
  • 2) Alter List and its implementation so that user can use List with any Java reference type.

  • 3) Provide black box tests for your new generic implementation of List.

  • 4) Add a new operation to your generic List called removeAll(). The new operation should consume one argument which is another list with elements of the same type as our list. The operation should update our current list and remove all elements that are also found in the argument list, e.g., (1 1 2 3 4 5).removeAll( (1, 4) ) should return (2 3 5).

2. Java Util

2.1. Lists

Create a class called ListUtils to place all methods that you need to design for the following exercises.

Practise Exercises
  • 5) Using List from java.util write a method that removes all duplicates element from the list.

  • 6) Using List from java.util write a method that given a List<List<X>> flattens the list, e.g., given ( (1 2 3) (3 4 5)) will return (1 2 3 4 5).

2.2. Maps

There are mutliple ways to iterate over the key-value pairs inside Java’s Map, a very concise way to iterate over the elements of a Map is to use Map.Entry and the method entrySet() defined on Map.

Here is small example that

  1. Creates a Map<String, Integer>.

  2. Adds 3 key-value pairs.

  3. Iterates over its pairs and prints them out to the Console.

Using Map<String,Integer>.
public class Test {

  public static void main(String[] args) {
    // Create a map. We assign to a variable
    // with type `Map`
    Map<String, Integer> index = new HashMap<>();

    index.put("a", 2);  // add "a" mapped to the integer 2
    index.put("b", 3);  // add "b" mapped to the integer 3
    index.put("c", 4);  // add "c" mapped to the integer 4

    for (Map.Entry<String, Integer> entry: index.entrySet())        // iterate over
      System.out.println(entry.getKey() + "," + entry.getValue());  // print key and value
  }
}

The above code outputs

a,2
b,3
c,4
Practise Exercises
  • 7) You are asked to implement a method that checks if two strings are anagrams (that means that they contain the same letters up to permutations). You must implement count and compare algorithms that rely on the fact that any two anagrams will have the same number of a’s, the same number of b’s, the same number of c’s, and so on. You may not assume any specific alphabet and should be able to deal with any kind of characters.

    For example: your method should return true for "cine-ma!" and "ice-man!". However, you may assume that "Cinema" and "Iceman" are NOT anagrams (your method returns false).

    As such, your method should use java.util.Map.