Free University of Bolzano/Bozen
Faculty of Computer Science - Bachelor in Applied Computer Science
Bachelor in Production Engineering
Introduction to Programming - A.A. 2004/2005

Exercises on recursion (arrays)


  1. Write a static method
    public static boolean appearsIn(int[] a, int n)
    that, given an array of integers a and an integer n, verifies whether n appears in a.
    For example, the call appearsIn({1,2,3}, 2) should return true.

  2. Write a static method
    public static int occurrences(int[] a, int n)
    that, given an array of integers a and an integer n, returns the number of occurrences of n in a.
    For example, the call occurrences({1,2,3,2,4,2}, 2) should return 3.

  3. Write a static method
    public static int[] negativesToZero(int[] a)
    that, given an array of integers a, returns a new array obtained from a by replacing each negative integer with 0.
    For example, the call negativesToZero({1,-2, 3, 4, -5}), should return the array {1, 0, 3, 4, 0}.

  4. Write a static method
    public static void invert(int[] a)
    that, given an array of integers a, inverts the positions of its elements.
    For example, the call invert(a), where a is a reference to the array {1, 2, 3, 4}, should modify the array in such a way that a refers to {4, 3, 2, 1}.

  5. Write a static method
    public static boolean palindrome(int[] a)
    that, given an array of integers a, returns the boolean value true if the sequence of elements of a coincides with the same sequence in inverse order, false otherwise.

Class Main to test the methods above.

Solution


Sunday, 9-Jan-2005 13:18:46 CET