/* * Lab4Task2.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. */ import java.util.Arrays; public class Lab4Task2 { /* * square - takes an array of integers and squares each element of the array. */ public static void square(int[] values) { // First check that the array reference is not null 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) { // First check that the array reference is not null // 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 ) { // First check that the array references are not null boolean isEqual = false; // 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; 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 ) { // First check that the array reference is not null int replaced = 0; // make sure to initialze to zero for ( int i = 0; i < a.length; i++ ) { 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 ) { // First check that the array references are not null 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 ) { // First check that the array references are not null 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 ) { // First check that the array references are not null 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 referenced the same literal strings // if ( !a[i].equals( b[j] ) ) mirror = false; } // for } // if return( mirror ); } /* * main - method used to test each of the methods in this program class. */ public static void main( String[] argv ) { 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) ); } }