First Summer 2004


Lab Notes Nine: Working with methods and arrays.
Here is your

A201/A597 LAB ASSIGNMENT NINE

Here are seven problems with answers. Read them and understand them. Then write a main program that illustrates how you'd be using them in a program. This lab is the opposite of lab 8: in that one you were given a main and asked to write the methods. Here we give you the methods and ask you to put them to use (and write the main).

1. Write a method
static double scalarProduct(double[] a, double[] b)
that computes the scalar product of two mathematical vectors (represented as arrays). The scalar product is:
a0b0 + a1b1 + ... + an-1bn-1


2. Write a method that computes the alternating sum of all elements in an array. For example, if alternatingSum is called with an array 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 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 method
public static int[] append(int[] a, int[] b) 
that appends one array after another. For example, if a is
1   4   9   16   
and b is
9   7   4   9   11
then append returns the array
1   4   9   16   9   7   4   9   11

5. Write a predicate method
public static boolean equals(int[] a, int[] b)
that checks whether two arrays have the same elements in the same order.

6. Write a predicate method
public static boolean sameSet(int[] a, int[] b)
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 will probably need one or more helper methods.

7. Write a predicate method
public static boolean sameElements(int[] a, int[] b)
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 will probably need one or more helper methods.


Here are possible solutions.

1. 
       public static double scalarProduct (double[] a, double[] b) {
         double result = 0;
         for (int i = 0; i < a.length; i++)
           result += a[i] * b[i];
         return result;
       }

---------------------------------------------------------------------------

2. 
       public static double alternatingSum(double[] a) {
         if (a.length == 0) return 0;
         double value = 0;
         for (int i = 0; i < a.length; i++) {
           if (i % 2 == 0) value += a[i];
           else value -= a[i];
         }
         return value;
       }

----------------------------------------------------------------------------

3. 
       public static void reverse(double[] a) {
         for (int i = 0; i < a.length / 2; i++) {
           double temp = a[i];
           a[i] = a[a.length - i - 1];
           a[a.length - i - 1] = temp;
         }
       }

---------------------------------------------------------------------------

4. 
       public static int[] append(int[] a, int[] b) {
         int[] result = new int[a.length + b.length];
         for (int i = 0; i < a.length; i++)
           result[i] = a[i];
         for (int i = 0; i < b.length; i++)
           result[i + a.length] = b[i];
         return result;
       }

--------------------------------------------------------------------------

5. 
       public static boolean equals(int[] a, int[] b) {
         if (a.length != b.length)
           return false;
         for (int i = 0; i < a.length; i++)
           if (a[i] != b[i])
             return false;
         return true;
       }

--------------------------------------------------------------------------

6. 
       public static boolean sameSet(int[] a, int[] b) {
         for (int i = 0; i < a.length; i++)
           if (! contains(b, a[i]))
             return false;
         for (int i = 0; i < b.length; i++)
           if (! contains(a, b[i]))
             return false;
         return true;
       }

------------------------------------------------------------------------

7. 
       public static boolean sameElements(int[] a, int[] b) {
         for (int i = 0; i < a.length; i++)
           if (count(a, a[i]) != count(b, a[i]))
             return false;
         for (int i = 0; i < b.length; i++)
           if (count(a, b[i]) != count(b, b[i]))
             return false;
         return true;
       }

------------------------------------------------------------------------

Understand them, then put together a main method that illustrates your understanding.


Last updated: May 16, 2004 by Adrian German for A201