/* * Lab3Task2.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 Lab3Task2 { /* * 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. */ 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 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 ( a != null && b != null ) { // 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 ); } /* * 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 initialze 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 arrays passed 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]; c[j++] = b[i]; } return( c ); } /* * isMirror - this method determines if the arrays * are mirror images and returns true or false accordingly. */ 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 ); } /* * isMirror - 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 int[] a = {1}, b = {1}; System.out.println( "Equality: " + equals(a, b) ); } { // Test 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( isMirror(a, b) ); System.out.println( isMirror(a, c) ); } } }