Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Supporting methods for an integer array
- * @author Ryon Bell
- * @version 3/28/2012
- */
- public class ArrayMethodsL8b
- {
- /**
- * instance variable array of ints
- */
- private int [] intArray;
- /**
- * secondary constructor,
- * the instance variable intArray will be a copy of the array created by the user
- */
- public ArrayMethodsL8b(int [] newArray)
- {
- setIntArray(newArray);
- }
- /**
- * accessor method,
- * returns a copy of the instance variable intArray
- */
- public int [] getIntArray()
- {
- // instantiate array with the same length as the parameter
- int[] copy = new int [this.intArray.length];
- for (int i = 0; i < this.intArray.length; i++)
- {
- copy[i] = this.intArray[i];
- }
- return copy;
- }
- /**
- * returns the length of the instance variable intArray
- */
- public int getLength()
- {
- return this.intArray.length;
- }
- /**
- * mutator method,
- * the instance variable intArray will be a copy of the array passed by the user
- */
- public void setIntArray(int[] newArray)
- {
- // instantiate array with the same length as the parameter
- this.intArray = new int [newArray.length];
- for (int i = 0; i < newArray.length; i++)
- {
- this.intArray[i] = newArray[i];
- }
- }
- /**
- * equals method
- * checks if the array in this object is the same as the array in the other object
- * if the lengths are not the same returns false right away
- * otherwise compairs the elements until either the first not the equal pair is found
- * or there are no more elements to compare
- */
- public boolean equals (ArrayMethodsL8b other)
- {
- boolean isEqual = true;
- if ( this.intArray.length != other.intArray.length )
- {
- isEqual = false; // arrays are not the same size
- }
- else
- {
- for ( int i = 0; i < this.intArray.length && isEqual; i++ )
- {
- if ( this.intArray[i] != other.intArray[i] )
- {
- // found the first pair that is not the same
- // no need to compare any further
- isEqual = false;
- }
- }
- }
- return isEqual;
- }
- /**
- * toString method returns printable version
- * of the conent of intArray
- */
- public String toString()
- {
- String returnValue = "";
- for ( int i = 0; i < this.intArray.length; i++ )
- {
- returnValue += this.intArray[i] + " ";
- }
- return returnValue += "\n";
- }
- // *** BUSINESS METHODS *** //
- /**
- * "business" method that calculates product
- * of all the integers in the array
- * @return an integer - value of the product
- */
- public int arrayProduct()
- {
- int product = this.intArray[0];
- for ( int i = 1; i < this.intArray.length; i++ )
- {
- product *= this.intArray[i];
- }
- return product;
- }
- /**
- * "business" method that swpas elements if necessary
- * and calculates the numbers of swaps
- * @return an integer - value of the productthe number of swaps performed
- */
- public int calculateSwaps()
- {
- int swaps = 0;
- //STEP2
- // in a for loop compare elements pairwaise: first with last; second with the second last; etc
- // if the element at the lower index is greater thatn the element in the higher index swap them
- // remember to count the number of swaps
- for(int i = 0; i<=this.intArray.length-4;i++ ) //index 0,1,2
- {
- // need to get rid of loop cuz it is only updating x and comparing index 0 to indexs 3,4,5
- for(int x = this.intArray.length-1; x>=this.intArray.length-3;x-- ) //index 5,4,3
- {
- System.out.println("comparing " +this.intArray[i] + " and " + this.intArray[x]);
- if(this.intArray[i]>this.intArray[x])
- {
- int temp = this.intArray[i];
- this.intArray[i] = this.intArray[x];
- this.intArray[x] = temp;
- swaps++;
- }
- }
- }
- return swaps;
- }
- }
Add Comment
Please, Sign In to add comment