/* * ArrayMethods.java * * A collection of static methods for working with arrays. * * To use one of these methods from outside the class, you need to * prepend the class name. * * CS112 * */ import java.util.*; public class ArrayMethods { /* * square - takes an array of integers and squares each * element of the array. */ public static void square(int[] values) { if (values == null) throw new IllegalArgumentException(); for (int i = 0; i < values.length; i++) { values[i] = values[i] * values[i]; } } /* * shiftLeft - takes an array of integers and shifts each element * one position to the left. The last element of the array becomes 0, * and the original first element is returned. */ public static int shiftLeft(int[] values) { if (values == null) throw new IllegalArgumentException(); // save the first value so we don't lose it int first = values[0]; // do the shifting for (int i = 0; i < values.length - 1; i++) { values[i] = values[i + 1]; } values[values.length - 1] = 0; return first; } /* * equals - takes two arrays and determines if the arrays * are equivalent. * * version 1: use a boolean variable for the return value, * and adjust it as needed based on the two arrays. */ public static boolean equals(int[] a, int[] b) { boolean isEqual = false; // Are a and b referencing the same physical array? // Note will also return true if a and b are null. if (a == b) isEqual = true; else if (a != null && b != null) { // If either array is null then they are not equal // so no need to check further, only continue // checking if both a and b are referencing a valid array // if the arrays are not of the same size // there is no need to check the contents, // as they cannot be equivalent. // if (a.length == b.length) { isEqual = true; // assume they are equivalent // check each element in each array, and stop // the loop at the first element that does not match for (int i = 0; (i < a.length) && isEqual; i++) { if (a[i] != b[i]) isEqual = false; } } } return isEqual; } /* * equals2 - takes two arrays and determines if the arrays * are equivalent. * * version 2: use multiple return statements, and return * as soon as the return value can be determined */ public static boolean equals2(int[] a, int[] b) { if (a == b) { return true; } else if (a == null || b == null) { return false; } else if (a.length != b.length) { return false; } else { // Check each element in each array, and return false // at the first element that does not match. for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) { return false; } } // If we get here, all of the elements matched! return true; } } /* * replace - this method replaces all occurences of * val2 in the array with val1. The method returns the * number of occurences. */ public static int replace(int[] a, int val1, int val2) { if (a == null) throw new IllegalArgumentException(); int replaced = 0; // make sure to initialize to zero for (int i = 0; i < a.length; i++) { // Note that we are comparing elements of the array // and because the elements are of type int can use == if (a[i] == val1) { a[i] = val2; replaced++; } } return replaced; } /* * interChange - this method interchanges the elements * from the two input arrays into a third array * which the method returns. * * Note the use of int[] as the return type indicating * that a reference to an array of integers is being * returned. */ public static int[] interChange(int[] a, int[] b) { if (a == null || b == null) throw new IllegalArgumentException(); int smallest = a.length; // Only interchange as many elements as contained // int he smallest of the two arrays. if (b.length < smallest) smallest = b.length; // the array containing the interchanged elements // must be double the size of the smallest array. int[] c = new int[smallest * 2]; // the loop uses two control variables, // one to index into the two arrays and one // to index into the array that will contain // the interchanged elements. for (int i = 0, j = 0; i < smallest; i++) { c[j] = a[i]; j++; c[j] = b[i]; j++; } return c; } /* * isMirror - this method determines if the arrays * are mirror images and returns true or false accordingly. * * version 1: use a boolean variable for the return value, * and adjust it as needed based on the two arrays. */ public static boolean isMirror(int[] a, int[] b) { if (a == null || b == null) throw new IllegalArgumentException(); boolean mirror = false; // As with equality, if the arrays are of different // sizes they cannot be mirror images. // if (a.length == b.length) { mirror = true; for (int i = 0, j = b.length - 1; i < b.length && mirror; i++, j--) { if (a[i] != b[j]) mirror = false; } // for } // if return mirror; } /* * isMirror2 - this method determines if the arrays * are mirror images and returns true or false accordingly. * * version 2: use multiple return statements, and return * as soon as the return value can be determined */ public static boolean isMirror2(int[] a, int[] b) { if (a == null || b == null) { throw new IllegalArgumentException(); } else if (a.length != b.length) { return false; } for (int i = 0; i < a.length; i++) { if (a[i] != b[b.length - 1 - i]) { return false; } } return true; } /* * isMirror (string version) - we can have a second method named * isMirror because the parameters to the method are different * (method overloading). * * Note however, that for this method to work properly the test * for equality musy be done differently. */ public static boolean isMirror(String[] a, String[] b) { if ( a == null || b == null ) throw new IllegalArgumentException(); boolean mirror = false; if (a.length == b.length) { mirror = true; for (int i = 0, j = b.length-1; i < b.length && mirror; i++, j--) { // Note the use of the equals method! // Using if ( a[i] != b[j] ) would only work // if the arrays contained literal string values. // if ( !a[i].equals(b[j]) ) mirror = false; } // for } // if return mirror; } /* * main - * * sample unit test method used to test each of the methods in this program * class. */ public static void main(String[] argv) { // Note the use of separate scopes to reuse variable names. { // Test equals and interChange int[] a = { 1, 2, 3 }; int[] b = { 1, 2, 3 }; int[] c = { 1, 4, 3 }; System.out.println("test 1 for equals: " + equals(a, b)); System.out.println("test 2 for equals: " + equals(a, c)); int[] result = interChange(a, c); System.out.println("test for interChange: " + Arrays.toString(result)); } { // Test integer version of isMirror int[] a = { 1, 2, 3, 4, 5 }; int[] b = { 5, 4, 3, 2, 1 }; int[] c = { 5, 4, 7, 2, 1 }; System.out.println("test 1 for integer version of isMirror: " + isMirror(a, b)); System.out.println("test 2 for integer version of isMirror: " + isMirror(a, c)); } { // Test string version of isMirror String[] a = { "abc", "def", "ghi" }; String[] b = { "ghi", "def", "abc" }; String[] c = { new String("ghi"), new String("def"), new String("abc") }; System.out.println("test 1 for string version of isMirror: " + isMirror(a, b)); System.out.println("test 2 for string version of isMirror: " + isMirror(a, c)); } } }