/* * MyArray * * An instance class to process * an array of integers. * * CS112 * */ import java.util.*; public class MyArray { private static int DEFAULT_SIZE = 10; int[] values; /* * Constructors */ MyArray() { this(DEFAULT_SIZE); } MyArray( int n ) { if ( n <=0 ) throw new IllegalArgumentException( "argument passed must be grearer than 0" ); values = new int[n]; } MyArray( int[] arr ) { if ( arr == null ) throw new IllegalArgumentException( "argument passed must not be null." ); values = arr; } /* * square - squares each element of the member array. */ public void square() { for (int i = 0; i < values.length; i++) { values[i] *= values[i]; } } /* * shiftLeft - shifts each element of the member array * one position to the left. The last element of the array becomes 0, * and the original first element is returned. */ public int shiftLeft() { // 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 - determines if the member arrays are equivalent. */ public boolean equals( MyArray other ) { boolean isEqual = false; // 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 ( other != null ) { // if the arrays are not of the same size // there is no need to check the contents, // as they cannot be equivalent. // if ( values.length == other.values.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 < values.length) && isEqual; i++ ) if ( values[i] != other.values[i] ) isEqual = false; } } return( isEqual ); } /* * replace - replaces all occurences of * val2 in the member array with val1. The method returns the * number of occurences. */ public int replace( int val1, int val2 ) { int replaced = 0; // make sure to initialze to zero for ( int i = 0; i < values.length; i++ ) { // Note that we are comparing elements of the array // and because the elements are of type int can use == if ( values[i] == val1 ) { values[i] = val2; replaced++; } } return( replaced ); } /* * interleave - this method interchanges the elements * from the two member arrays into the member array * of a new object. * */ public MyArray interleave( MyArray other ) { if (other == null) throw new IllegalArgumentException(); int smallest = values.length; // Only interchange as many elements as contained // int he smallest of the two arrays. if ( other.values.length < smallest ) smallest = other.values.length; // the array containing the interchanged elements // must be double the size of the smallest array. MyArray newArr = new MyArray( 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++ ) { newArr.values[j++] = values[i]; newArr.values[j++] = other.values[i]; } return( newArr ); } /* * isMirror - this method determines if the member arrays * are mirror images and returns true or false accordingly. */ public boolean isMirror( MyArray other ) { if ( other == null ) throw new IllegalArgumentException(); boolean mirror = false; // As with equality, if the arrays are of different // sizes they cannot be mirror images. // if ( values.length == other.values.length ) { // Assume they are mirrors and prove otherwise mirror = true; for ( int i = 0, j = other.values.length-1; i < other.values.length && mirror; i++, j-- ) { if ( values[i] != other.values[j] ) // change the state to stop the loop mirror = false; } // for } // if return( mirror ); } /* * simple toString method */ public String toString() { return Arrays.toString(values); } /* * main - * * sample unit test method used to test each of the methods of this class. */ public static void main( String[] argv ) { int[] a = {1, 2, 3, 4, 5}; int[] b = {10, 9, 8, 7, 6}; int[] c = {5, 4, 3, 2, 1}; MyArray ma = new MyArray(a); MyArray mb = new MyArray(b); MyArray mc = new MyArray(c); System.out.println( "ma == " + ma ); System.out.println( "mb == " + mb ); System.out.println( "mc == " + mc ); System.out.println( "ma.IsMirror(mb) == " + ma.isMirror(mb) ); System.out.println( "ma.IsMirror(mc) == " + ma.isMirror(mc) ); System.out.println( "ma.interleave(mb) == " + ma.interleave(mb) ); System.out.println( "ma.isEqual(ma) == " + ma.equals(ma) ); System.out.println( "ma.isEqual(mb) == " + ma.equals(mb) ); ma.replace(4,12); System.out.println( "ma.replace(4,12) == " + ma ); ma.square(); System.out.println( "ma.square() == " + ma ); } }