micher43

E7.2 parts A-J

Nov 14th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. public class ArrayMethods {
  2.    
  3.     private int[] values;
  4.    
  5.     public ArrayMethods(int[] initialValues) {
  6.         values = initialValues;
  7.         }
  8.    
  9.     //part a
  10.     public void swapFirstAndLast() {
  11.         int a = values[0];
  12.         int n = values.length;
  13.         values[0] = values[n-1];
  14.         values[n-1] = a;
  15.     }
  16.    
  17.     //part b
  18.     public void shiftRight() {
  19.         int temp  = values[values.length -1];
  20.            if(values.length >= 2)
  21.            {
  22.                System.arraycopy(values,0,values,1, values.length -1);
  23.            }
  24.            values[0] = temp;
  25.     }
  26.    
  27.     //part c
  28.     public void evensWithZero(){
  29.         for(int i = 0; i < values.length ; i++){
  30.             if(values[i] % 2 == 0 ){
  31.                 values[i] = 0;
  32.             }
  33.         }
  34.     }
  35.    
  36.     //part d
  37.     public void replaceWithLargestNeighbor(){
  38.         for (int i = 1; i < values.length - 1; i++) {
  39.            
  40.             if (values[i + 1] > values[i - 1]) {
  41.                
  42.                 values[i] = values[i + 1];
  43.             }
  44.            
  45.             else if (values[i - 1] > values[i + 1]) {
  46.                 values[i] = values[i - 1];
  47.             }
  48.         }
  49.  
  50.     }
  51.    
  52.     //part e
  53.     public void removeMiddleElement(){
  54.         int count = values.length ;
  55.         int[] copy;
  56.             if (count % 2 == 0)
  57.             {
  58.                 int middle1 = count / 2;
  59.                 int middle2 = (count / 2);
  60.                 copy = new int[values.length - 2];
  61.                 System.arraycopy(values, 0, copy, 0, middle1 - 1);
  62.                 System.arraycopy(values, middle2 + 1 , copy, middle2 - 1, middle2 - 1);
  63.             }
  64.             else
  65.             {
  66.                 copy = new int[values.length - 1];
  67.                 int middle1 = count / 2;
  68.                 int middle2 = (count / 2);
  69.                 System.arraycopy(values, 0, copy, 0, middle1);
  70.                 System.arraycopy(values, middle2 +1, copy, middle2, (count / 2 ));
  71.             }
  72.        
  73.     }
  74.    
  75.     //part f
  76.     public void evensToFront(){
  77.         int swapPos = 0;
  78.         for (int i = 0; i < values.length; i++) {
  79.            
  80.             if (values[i] % 2 == 0) {
  81.                
  82.                 int temp = values[swapPos];
  83.                 values[swapPos] = values[i];
  84.                 values[i] = temp;
  85.                 swapPos += 1;
  86.             }
  87.         }
  88.        
  89.     }
  90.    
  91.     //part g
  92.     public static int secondLargestElement(int values[]) {
  93.         int largest = values[0];
  94.         int secondLargest = largest;
  95.         for (int i = 0; i < values.length; i++) {
  96.             if (values[i] > largest) {
  97.                 int temp = largest;
  98.                 largest = values[i];
  99.                 secondLargest = temp;
  100.             }
  101.         }
  102.         return secondLargest;
  103.     }
  104.    
  105.     //part h
  106.     public static boolean isSorted(int[] values) {
  107.         for (int i = 0; i < values.length - 1; i++) {
  108.             if (values[i] > values[i + 1]) {
  109.                 return false;
  110.             }
  111.         }
  112.         return true;
  113.     }
  114.  
  115.     //part i
  116.     public static boolean hasAdjacentDuplicates(int[] values) {
  117.         for (int i = 0; i < values.length - 1; i++) {
  118.             if (values[i] == values[i + 1]) {
  119.                 return true;
  120.             }
  121.         }
  122.         return false;
  123.     }
  124.  
  125.     // part j
  126.     public static boolean hasDuplicateElements(int[] values) {
  127.         for (int i = 0; i < values.length; i++) {
  128.             for (int j = 0; j < values.length; j++) {
  129.                 if (i != j && values[i] == values[j]) {
  130.                     return true;
  131.                 }
  132.             }
  133.         }
  134.         return false;
  135.     }
  136.    
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment