Guest User

Untitled

a guest
Jul 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. /**  Supporting methods for an integer array
  2.   *   @author Ryon Bell
  3.   *   @version 3/28/2012
  4.   */
  5.  
  6. public class ArrayMethodsL8b
  7. {
  8.   /**
  9.    * instance variable array of ints
  10.    */
  11.   private int [] intArray;
  12.  
  13.   /**
  14.    * secondary constructor,
  15.    * the instance variable intArray will be a copy of the array created by the user
  16.    */
  17.   public ArrayMethodsL8b(int [] newArray)
  18.   {
  19.     setIntArray(newArray);
  20.   }
  21.  
  22.   /**
  23.    * accessor method,
  24.    * returns a copy of the instance variable intArray
  25.    */
  26.   public int [] getIntArray()
  27.   {
  28.     // instantiate array with the same length as the parameter
  29.     int[] copy = new int [this.intArray.length];
  30.     for (int i = 0; i < this.intArray.length; i++)
  31.     {
  32.       copy[i] = this.intArray[i];
  33.     }
  34.     return copy;
  35.   }
  36.  
  37.   /**
  38.    * returns the length of the instance variable intArray
  39.    */
  40.   public int getLength()
  41.   {
  42.     return this.intArray.length;
  43.   }
  44.  
  45.   /**
  46.    * mutator method,
  47.    * the instance variable intArray will be a copy of the array passed by the user
  48.    */
  49.   public void setIntArray(int[] newArray)
  50.   {
  51.     // instantiate array with the same length as the parameter
  52.     this.intArray = new int [newArray.length];
  53.     for (int i = 0; i < newArray.length; i++)
  54.     {
  55.       this.intArray[i] = newArray[i];
  56.     }
  57.   }
  58.  
  59.   /**
  60.    * equals method
  61.    * checks if the array in this object is the same as the array in the other object
  62.    * if the lengths are not the same returns false right away
  63.    * otherwise compairs the elements until either the first not the equal pair is found
  64.    *    or there are no more elements to compare
  65.    */
  66.   public boolean equals (ArrayMethodsL8b other)
  67.   {
  68.     boolean isEqual = true;
  69.     if ( this.intArray.length != other.intArray.length )
  70.     {
  71.       isEqual = false; // arrays are not the same size
  72.     }
  73.     else
  74.     {
  75.       for ( int i = 0; i < this.intArray.length && isEqual; i++ )
  76.       {
  77.         if ( this.intArray[i] != other.intArray[i] )
  78.         {
  79.           // found the first pair that is not the same
  80.           // no need to compare any further
  81.           isEqual = false;
  82.         }
  83.       }
  84.     }
  85.     return isEqual;
  86.   }
  87.  
  88.   /**
  89.    * toString method returns printable version
  90.    * of the conent of intArray
  91.    */
  92.   public String toString()
  93.   {
  94.     String returnValue = "";
  95.     for ( int i = 0; i < this.intArray.length; i++ )
  96.     {
  97.       returnValue += this.intArray[i] + " ";
  98.     }
  99.     return returnValue += "\n";
  100.   }
  101.  
  102.   // *** BUSINESS METHODS *** //
  103.  
  104.   /**
  105.    * "business" method that calculates product
  106.    * of all the integers in the array
  107.    * @return an integer - value of the product
  108.    */
  109.   public int arrayProduct()
  110.   {
  111.     int product = this.intArray[0];
  112.     for ( int i = 1; i < this.intArray.length; i++ )
  113.     {
  114.       product *= this.intArray[i];
  115.     }
  116.     return product;
  117.   }
  118.  
  119.   /**
  120.    * "business" method that swpas elements if necessary
  121.    * and calculates the numbers of swaps
  122.    * @return an integer - value of the productthe number of swaps performed
  123.    */
  124.   public int calculateSwaps()
  125.   {
  126.     int swaps = 0;
  127.     //STEP2
  128.     // in a for loop compare elements pairwaise: first with last; second with the second last; etc
  129.     // if the element at the lower index is greater thatn the element in the higher index swap them
  130.     // remember to count the number of swaps
  131.     for(int i = 0; i<=this.intArray.length-4;i++ ) //index 0,1,2
  132.     {
  133.       // need to get rid of loop cuz it is only updating x and comparing index 0 to indexs 3,4,5
  134.       for(int x = this.intArray.length-1; x>=this.intArray.length-3;x-- ) //index 5,4,3
  135.       {
  136.         System.out.println("comparing " +this.intArray[i] + " and " + this.intArray[x]);
  137.         if(this.intArray[i]>this.intArray[x])
  138.         {
  139.           int temp = this.intArray[i];
  140.           this.intArray[i] = this.intArray[x];
  141.           this.intArray[x] = temp;
  142.           swaps++;
  143.         }
  144.       }
  145.     }
  146.    
  147.    
  148.     return swaps;
  149.   }
  150. }
Add Comment
Please, Sign In to add comment