/* * ArrayObjecr.java * * A collection of instance methods for working with arrays. * * * CS112 * */ import java.util.*; public class ArrayObject { private static final int DEFAULT_SIZE = 20; private int [] values; public ArrayObject() { values = new int[DEFAULT_SIZE]; } public ArrayObject( int[] arr ) { if (arr == null) throw new IllegalArgumentException(); // Note that we are explicitly assigning the instance // variable values to reference the input array values = arr; } /* * square - takes an array of integers and squares each * element of the array. */ public void square() { 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 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 - 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 boolean equals(ArrayObject 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 (this == b) isEqual = true; else if (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 (values.length == b.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] != b.values[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 int replace(int val1, int val2) { int replaced = 0; // make sure to initialize 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; } public String toString() { return Arrays.toString(values); } /* * 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, 4, 5 }; int[] b = { 5, 4, 3, 2, 1 }; int[] c = { 5, 4, 3, 2, 1 }; ArrayObject aObj_a = new ArrayObject(a); ArrayObject aObj_b = new ArrayObject(b); ArrayObject aObj_c = new ArrayObject(c); System.out.println("test 1 for toString: " + Arrays.toString(a) + " <==> " + aObj_a ); System.out.println("test 1 for toString: " + Arrays.toString(b) + " <==> " + aObj_b ); System.out.println("test 1 for toString: " + Arrays.toString(c) + " <==> " + aObj_c ); System.out.println("test 1 for equals: " + aObj_a.equals( aObj_c ) ); System.out.println("test 2 for equals: " + aObj_b.equals( aObj_c ) ); aObj_c.square(); System.out.println("test 1 for square: " + aObj_c ); } }