First Summer 2006


Homework Seven: Methods (also known as procedures, subroutines, functions---by and large).
Due date
Tuesday June 6, 11:59pm
Late policy
Please turn everything on time, late solutions will not be accepted.

Work policy
Working in groups is encouraged but please turn in your own version of the assignment.

Also, please make sure you

with the Computer Science Department's Statement on Academic Integrity before turning in your assignment.

Task
Write programs that solve the following problems:

  1. Write a method called scalarProduct that computes the scalar product of two mathematical vectors (represented as lists). The scalar product is:
    a0b0 + a1b1 + ... + an-1bn-1

  2. Write a method that computes the alternating sum of all elements in a list of numbers. For example, if alternatingSum is called with a list containing
    1   4   9   16   9   7   4   9   11
    Then it computes
    1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11
    which is, of course, -2.

  3. Write a method reverse that reverses the sequence of elements in an array. For example, if reverse is called with an array (or list) containing
    1   4   9   16   9   7   4   9   11
    then the array is changed to
    11   9   4   7   9   16   9   4   1

  4. Write a predicate method equals that checks whether two arrays have the same elements in the same order.

  5. Write a predicate method sameSet that checks whether two arrays have the same elements in some order, ignoring multiplicities. For example, the two arrays
    1   4   9   16   9   7   4   9   11
    and
    11   11   7   9   16   4   1
    would be considered to have the same set. You may or may not need one or more helper methods.

  6. Write a predicate method sameElements that checks whether two arrays have the same elements in some order, with the same multiplicities. For example,
    1   4   9   16   9   7   4   9   11
    and
    11   1   4   9   16   9   7   4   9
    would be considered to have the same elements, but
    1   4   9   16   9   7   4   9   11
    and
    11   11   7   9   16   4   1
    would not.

    You may or may not need one (or more) helper method(s).

Grading
Feedback will be provided within a week, grades will be posted on-line.