Advertisement
Guest User

Blatt4_Aufgabe3_Complete

a guest
Dec 5th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. /*
  2.     Aufgabe 3) Rekursion mit eindimensionalen Arrays
  3. */
  4. import java.util.Arrays;
  5.  
  6. public class Aufgabe3 {
  7.  
  8.     private static int getMaxPairSum(int[] workArray, int start, int end) {
  9.         // TODO: Implementieren Sie hier Ihre Lösung für die Methode
  10.         if (start<end){
  11.             return Math.max((workArray[start]+workArray[start+1]),(getMaxPairSum(workArray,start+1,end)));
  12.         }
  13.         return 0; //Zeile kann geändert oder entfernt werden.
  14.     }
  15.  
  16.     private static int countValuesWithSmallerNeighbors(int[] workArray, int index) {
  17.         // TODO: Implementieren Sie hier Ihre Lösung für die Methode
  18.         if (index<workArray.length-1){
  19.          if (workArray[index]>workArray[index-1]) return 1+countValuesWithSmallerNeighbors(workArray,index+1);
  20.          else return 0+countValuesWithSmallerNeighbors(workArray,index+1);
  21.         }
  22.         return 0; //Zeile kann geändert oder entfernt werden.
  23.     }
  24.  
  25.  
  26.     private static int[] getArrayWithNegativeValues(int[] workArray, int index) {
  27.         // TODO: Implementieren Sie hier Ihre Lösung für die Methode
  28.         int[] copy=workArray.clone();
  29.         if (index<=workArray.length-1){
  30.             if (copy[index]>0) copy[index]=0;
  31.             return getArrayWithNegativeValues(copy,index+1);
  32.         }
  33.         return copy;
  34.     }
  35.  
  36.     private static boolean containsValue(int[] workArray, int value) {
  37.         // TODO: Implementieren Sie hier Ihre Lösung für die Methode
  38.         if (workArray.length>0){
  39.             if (workArray[0]==value) return true;
  40.             else if(workArray.length!=1){
  41.                 return containsValue(Arrays.copyOfRange(workArray,0,workArray.length/2),value)||containsValue(Arrays.copyOfRange(workArray,workArray.length/2,workArray.length),value);
  42.             }
  43.  
  44.         }
  45.         return false;
  46.     }
  47.    
  48.     public static void main(String[] args) {
  49.  
  50.         int[] array1 = {3, 1, 7, 9, 4, 10, 5, 3};
  51.         System.out.println(getMaxPairSum(array1, 0, array1.length - 1));
  52.         System.out.println(getMaxPairSum(array1, 4, array1.length - 1));
  53.         System.out.println(getMaxPairSum(array1, 1, 3));
  54.         System.out.println(getMaxPairSum(array1, 0, 2));
  55.         System.out.println(getMaxPairSum(array1, 0, 1));
  56.         System.out.println();
  57.  
  58.         int[] array2 = {3, 9, 7, 19, 8, 10, 6, 3, 11, 5};
  59.         System.out.println(countValuesWithSmallerNeighbors(array2, 1));
  60.         System.out.println(countValuesWithSmallerNeighbors(array2, 4));
  61.         System.out.println(countValuesWithSmallerNeighbors(array2, 6));
  62.         System.out.println(countValuesWithSmallerNeighbors(array2, 8));
  63.         System.out.println();
  64.  
  65.         int[] array3 = {5, -3, 7, -15, 20, -5, 0, 14, 3, -2, -8};
  66.         System.out.println(Arrays.toString(array3));
  67.         System.out.println(Arrays.toString(getArrayWithNegativeValues(array3, 0)));
  68.         System.out.println(Arrays.toString(getArrayWithNegativeValues(array3, 10)));
  69.         System.out.println(Arrays.toString(getArrayWithNegativeValues(array3, 8)));
  70.         System.out.println(Arrays.toString(getArrayWithNegativeValues(array3, 4)));
  71.         System.out.println();
  72.  
  73.         int[] array4 = {2, 4, 7, 10, -10, 4, 0, 0, 27, 11, 4, 6};
  74.         System.out.println(containsValue(array4, 11));
  75.         System.out.println(containsValue(array4, 2));
  76.         System.out.println(containsValue(array4, 25));
  77.         System.out.println(containsValue(array4, 0));
  78.         System.out.println(containsValue(array4, 14));
  79.         System.out.println(containsValue(array4, 6));
  80.        
  81.  
  82.         assert(getMaxPairSum(array1, 0, array1.length - 1) == 16);
  83.         assert(getMaxPairSum(array1, 4, array1.length - 1) == 15);
  84.         assert(getMaxPairSum(array1, 1, 3) == 16);
  85.         assert(getMaxPairSum(array1, 0, 2) == 8);
  86.         assert(getMaxPairSum(array1, 0, 1) == 4);
  87.  
  88.  
  89.         assert(countValuesWithSmallerNeighbors(array2, 1) == 4);
  90.         assert(countValuesWithSmallerNeighbors(array2, 4) == 2);
  91.         assert(countValuesWithSmallerNeighbors(array2, 6) == 1);
  92.         assert(countValuesWithSmallerNeighbors(array2, 8) == 1);
  93.  
  94.  
  95.         assert (Arrays.equals(getArrayWithNegativeValues(array3, 0), new int[]{0, -3, 0, -15, 0, -5, 0, 0, 0, -2, -8}) == true);
  96.         assert (Arrays.equals(getArrayWithNegativeValues(array3, 10), new int[]{5, -3, 7, -15, 20, -5, 0, 14, 3, -2, -8}) == true);
  97.         assert (Arrays.equals(getArrayWithNegativeValues(array3, 8), new int[]{5, -3, 7, -15, 20, -5, 0, 14, 0, -2, -8}) == true);
  98.         assert (Arrays.equals(getArrayWithNegativeValues(array3, 4), new int[]{5, -3, 7, -15, 0, -5, 0, 0, 0, -2, -8}) == true);
  99.  
  100.         assert (containsValue(array4, 11) == true);
  101.         assert (containsValue(array4, 2) == true);
  102.         assert (containsValue(array4, 25) == false);
  103.         assert (containsValue(array4, 0) == true);
  104.         assert (containsValue(array4, 14) == false);
  105.         assert (containsValue(array4, 6) == true);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement