GSD Questions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package gsdsolution;
  7. /**
  8. *
  9. * @author todten
  10. */
  11. import java.math.*;
  12. import java.util.*;
  13. public class GSDSolution {
  14. /* Main Program */
  15. public static void main(String[] args) {
  16. int someCows = 10;
  17. System.out.println(someCows + " cows have " + countLegs(someCows) + " legs \n");
  18. System.out.println("Calculate Row Vector");
  19. calcAbsRowVector(inputMatrix);
  20. System.out.println("Merge and Sort Arrays");
  21. mergeAndSortArrays(input1, input2);
  22. }
  23. /* Third Exercise */
  24. public static int countLegs(int cows) {
  25. if (cows == 0) {
  26. return 0;
  27. } else {
  28. return 4 + countLegs(cows - 1);
  29. }
  30. }
  31. /* First Exercise */
  32. static int[][] inputMatrix = new int[][]{
  33. {2, 2},
  34. {4, 4},
  35. {6, 5}};
  36. public static double[] calcAbsRowVector(int[][] aMatrix) {
  37. double[] result = new double[aMatrix.length];
  38. double temp = 0.0;
  39. for (int i = 0; i < aMatrix.length; i++) {
  40. for (int j = 0; j < aMatrix[i].length; j++) {
  41. temp = temp + Math.pow(aMatrix[i][j], 2);
  42. //System.out.println(" I = " + i + " J = " + j + " Temp = " + temp);
  43. }
  44. result[i] = Math.sqrt(temp);
  45. temp = 0.0;
  46. //System.out.println(" Result at I " + i + " Result " + result[i]);
  47. }
  48. return result;
  49. }
  50. /* Second Exercise */
  51. static int[] input1 = new int[]{2, 7, 5, 34};
  52. static int[] input2 = new int[]{3, 48, 4, 72};
  53. public static int[] mergeAndSortArrays(int[] firstArray, int[] secondArray) {
  54. int[] resultArray = new int[firstArray.length * 2];
  55. /* OK, both Arrays are the same size */
  56. int offset = firstArray.length;
  57. for (int i = 0; i < firstArray.length; i++) {
  58. resultArray[i] = firstArray[i];
  59. }
  60. for (int i = 0; i < secondArray.length; i++) {
  61. resultArray[i + offset] = secondArray[i];
  62. }
  63. /* Just for debugging */
  64. System.out.println("Result Array " + Arrays.toString(resultArray));
  65. /*
  66. * Die haeufigste Antwort wird die Verwendung der build - in Funktion
  67. * von Java zum Sortieren von Arrays sein
  68. */
  69. Arrays.sort(resultArray);
  70. /*
  71. * Dann nachfragen und den Bewerber das Array manuell mit einem
  72. * Algorithmus seiner Wahl (z.B. Quicksort oder einfache Maximumssuche)
  73. * sortieren lassen
  74. */
  75. /* Just for debugging */
  76. System.out.println("Sorted Result Array " + Arrays.toString(resultArray));
  77. return resultArray;
  78. }
  79. }