Java SE 8 API documentation

Last modification: 03/10/2016

Exercises

  1. In the util package create an IntVector class, which represents a sequence of integers.

    1. Add an array of integers as private field.

    2. Write a constructor which takes an array of integers as argument. Take care to not expose the internal state.

    3. Write an add() method, which adds an integer argument to every element in the sequence.

    4. Write a toString() method as well, which returns the elements in the sequence as a string. The elements should be separated with a whitespace. For instance [1 2 3].

  2. Create a Calculator program, which takes a sequence of integers and a constant integer as arguments. The program adds the constant to every element in the sequence and prints the result. For instance:

    $ java Calculator 1,2,3 5
    [6 7 8]

    Make use of the above util.IntVector class.

  3. Modify your util.IntVector class so that it uses a java.util.LinkedList<A> instead of an array to store the integers.

    Little help on how to use a java.util.LinkedList<A>.

  4. Simplify the Calculator class by using java.util.Scanner.

    The most important methods of java.util.Scanner here are nextInt() and hasNextInt().

  5. Improve your util.IntVector.toString() method by using the StringBuilder class.