![]() |
![]() Second Summer 2002 |
A201/A597 LAB ASSIGNMENT NINE
1. |
Write a method that computes the scalar product of two mathematical vectors (represented as arrays). The scalar product is:static double scalarProduct(double[] a, double[] b) 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 11Then it computes 1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11which 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 11then the array is changed to 11 9 4 7 9 16 9 4 1 |
4. |
Write a method
that appends one array after another. For example, ifpublic static int[] append(int[] a, int[] b) a is
1 4 9 16and b is
9 7 4 9 11then append returns the array
1 4 9 16 9 7 4 9 11 |
5. |
Write a predicate method
that checks whether two arrays have the same elements in the same order.public static boolean equals(int[] a, int[] b) |
6. |
Write a predicate method
that checks whether two arrays have the same elements in some order, ignoring multiplicities. For example, the two arrayspublic static boolean sameSet(int[] a, int[] b) 1 4 9 16 9 7 4 9 11and 11 11 7 9 16 4 1would be considered to have the same set. You will probably need one or more helper methods. |
7. |
Write a predicate method
that checks whether two arrays have the same elements in some order, with the same multiplicities. For example,public static boolean sameElements(int[] a, int[] b) 1 4 9 16 9 7 4 9 11and 11 1 4 9 16 9 7 4 9would be considered to have the same elements, but 1 4 9 16 9 7 4 9 11and 11 11 7 9 16 4 1would not. You will probably need one or more helper methods. |
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.