//illustrates several common algorithms that use arrays.  
//EXERCISE: write a tester that uses these methods on simple arrays:
//convince yourself that you can write a tester for a class given the implementation
class Arrays1D {
    //method1: given an integer array, it returns an integer array
    //containing only the positive elements of the input.  

    public static int[] getPositiveElements(int [] input) {
	//first count positives inside input (b/c that'll be size of output array)
	int numPositives = 0;
	for(int i=0; i<input.length; ++i) {
	    if(input[i] > 0)
		numPositives++;
	}
	int [] result = new int[numPositives];
	for(int i=0, j = 0; i<input.length; ++i) {
	    if(input[i] > 0) {
		result[j] = input[i];
		j++;   	
	    }		       
	}
	return result;
    }

    //method2: given an integer array, 
    //returns: "true" if any two *adjacent* elements have the *same* value
    public static boolean hasAdjacentEqual(int [] input) {
	boolean result = false;
	for(int i=0; i<input.length-1; ++i) {
	    if(input[i] == input[i+1])
		result = true;
		
	}
	return result;
    }

    //method3: given two integer arrays
    //returns: a third array that creates point objects with coordinates
    //         (arr1[i], arr2[i])
    //PRECONDITION: arr1 and arr2 are exactly the same size (Q: what happens if this is violated?)

    //note: this shows usage of arrays of objects.  Notice that we first intantiate the array
    //itself, and then each point in the array
    public static Point[] getPoints(int [] arr1, int[] arr2) {
	Point [] parr = new Point[arr1.length]; //same as arr2.length by assumption
	for(int i=0; i<parr.length; ++i) {
	    parr[i] = new Point(arr1[i], arr2[i]);
	}
	return parr;
    }//pts 
}

