Guest User

mariablatt5

a guest
May 21st, 2019
78
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Aufgabe 4) Zweidimensionale Arrays - Berechnungen
  3. */
  4. public class Aufgabe4 {
  5.  
  6. private static int[][] genFilledArray(int centerNumber) {
  7. // TODO: Implementieren Sie hier Ihre Lösung für die Methode
  8. int[][] array = new int[centerNumber][centerNumber];
  9. int mid = centerNumber / 2;
  10. if (centerNumber % 2 != 0){
  11.  
  12. for (int i = 0; i < centerNumber; i++){
  13. for (int j = 0; j < centerNumber; j++){
  14. //1/4
  15. if (j <= mid && i <= mid) {
  16. array[i][j] = j + (i + 1);
  17. }
  18. //2/4
  19. if (j > mid && i <= mid){
  20. array[i][j] = (centerNumber - j) + i;
  21. }
  22. //3/4
  23. if (j <= mid && i > mid){
  24. array[i][j] = (centerNumber - i) + j;
  25. }
  26. //4/4
  27. if (j > mid && i > mid){
  28. array[i][j] = centerNumber * 2 - (i + j + 1);
  29. }
  30. }
  31. }
  32. }else{
  33. return null;
  34. }
  35. return array;
  36.  
  37. }
  38.  
  39. private static int[][] calcSumInArray(int[][] workArray) {
  40. // TODO: Implementieren Sie hier Ihre Lösung für die Methode
  41. int[][] array=new int[workArray.length][workArray.length];
  42.  
  43. for(int i=0;i<workArray.length;i++){
  44. for(int j=0;j<workArray[i].length;j++){
  45. if(i==0){
  46. if(j==0){
  47. array[i][j]=workArray[i][j]+workArray[i+1][j]+workArray[i][j+1]+workArray[i+1][j+1];
  48. //array[i][j]=workArray[i][j]+workArray[i+1][j]+workArray[i][j+1]+workArray[i+1][j+1];
  49. }
  50. else if(j==workArray[i].length-1){
  51. array[i][j]=workArray[i][j]+workArray[i][j-1]+workArray[i+1][j]+workArray[i+1][j-1];
  52. }
  53. else{
  54. array[i][j]=workArray[i][j]+workArray[i][j-1]+workArray[i][j+1];
  55. array[i][j]+=workArray[i+1][j-1]+workArray[i+1][j]+workArray[i+1][j+1];
  56. }
  57. }
  58. else if(i==workArray.length-1){
  59. if(j==0){
  60. array[i][j]=workArray[i][j]+workArray[i][j+1]+workArray[i-1][j]+workArray[i-1][j+1];
  61. }
  62. else if(j==workArray[i].length-1){
  63. array[i][j]=workArray[i][j]+workArray[i][j-1]+workArray[i-1][j]+workArray[i-1][j-1];
  64. }
  65. else{
  66. array[i][j]=workArray[i][j]+workArray[i][j-1]+workArray[i][j+1];
  67. array[i][j]+=workArray[i-1][j-1]+workArray[i-1][j]+workArray[i-1][j+1];
  68. }
  69.  
  70. }
  71. else{
  72. if(j==0){
  73. array[i][j]=workArray[i][j]+workArray[i+1][j]+workArray[i-1][j];
  74. array[i][j]+=workArray[i][j+1]+workArray[i+1][j+1]+workArray[i-1][j+1];
  75. }
  76. else if(j==workArray[i].length-1){
  77. array[i][j]=workArray[i][j]+workArray[i+1][j]+workArray[i-1][j];
  78. array[i][j]+=workArray[i][j-1]+workArray[i+1][j-1]+workArray[i-1][j-1];
  79. }
  80. else{
  81. array[i][j]=0;
  82. for(int k=-1;k<2;k++){
  83. for(int p=-1;p<2;p++){
  84. array[i][j]+=workArray[i+k][j+p];
  85. }
  86.  
  87. }
  88. }
  89. }
  90. }
  91. }
  92. return array;
  93. }
  94.  
  95.  
  96.  
  97.  
  98. private static void print(int[][] workArray) {
  99. if(workArray != null) {
  100. for (int y = 0; y < workArray.length; y++) {
  101. for (int x = 0; x < workArray.length; x++) {
  102. System.out.print(workArray[y][x] + "\t");
  103. }
  104. System.out.println();
  105. }
  106. System.out.println();
  107. }
  108. }
  109.  
  110. public static void main(String[] args) {
  111. int[][] myArray;
  112. int[][] myResultArray;
  113. // Test für centerNumber = 5
  114. myArray = genFilledArray(5);
  115. print(myArray);
  116. myResultArray = calcSumInArray(myArray);
  117. print(myResultArray);
  118. //Test für centerNumber = 9
  119. myArray = genFilledArray(9);
  120. print(myArray);
  121. myResultArray = calcSumInArray(myArray);
  122. print(myResultArray);
  123. //Test für centerNumber = 15
  124. myArray = genFilledArray(15);
  125. print(myArray);
  126. myResultArray = calcSumInArray(myArray);
  127. print(myResultArray);
  128. }
  129.  
  130.  
  131. }
RAW Paste Data