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

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gsdsolution;
/**
*
* @author todten
*/
import java.math.*;
import java.util.*;
public class GSDSolution {
/* Main Program */
public static void main(String[] args) {
int someCows = 10;
System.out.println(someCows + " cows have " + countLegs(someCows) + " legs \n");
System.out.println("Calculate Row Vector");
calcAbsRowVector(inputMatrix);
System.out.println("Merge and Sort Arrays");
mergeAndSortArrays(input1, input2);
}
/* Third Exercise */
public static int countLegs(int cows) {
if (cows == 0) {
return 0;
} else {
return 4 + countLegs(cows - 1);
}
}
/* First Exercise */
static int[][] inputMatrix = new int[][]{
{2, 2},
{4, 4},
{6, 5}};
public static double[] calcAbsRowVector(int[][] aMatrix) {
double[] result = new double[aMatrix.length];
double temp = 0.0;
for (int i = 0; i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) {
temp = temp + Math.pow(aMatrix[i][j], 2);
//System.out.println(" I = " + i + " J = " + j + " Temp = " + temp);
}
result[i] = Math.sqrt(temp);
temp = 0.0;
//System.out.println(" Result at I " + i + " Result " + result[i]);
}
return result;
}
/* Second Exercise */
static int[] input1 = new int[]{2, 7, 5, 34};
static int[] input2 = new int[]{3, 48, 4, 72};
public static int[] mergeAndSortArrays(int[] firstArray, int[] secondArray) {
int[] resultArray = new int[firstArray.length * 2];
/* OK, both Arrays are the same size */
int offset = firstArray.length;
for (int i = 0; i < firstArray.length; i++) {
resultArray[i] = firstArray[i];
}
for (int i = 0; i < secondArray.length; i++) {
resultArray[i + offset] = secondArray[i];
}
/* Just for debugging */
System.out.println("Result Array " + Arrays.toString(resultArray));
/*
* Die haeufigste Antwort wird die Verwendung der build - in Funktion
* von Java zum Sortieren von Arrays sein
*/
Arrays.sort(resultArray);
/*
* Dann nachfragen und den Bewerber das Array manuell mit einem
* Algorithmus seiner Wahl (z.B. Quicksort oder einfache Maximumssuche)
* sortieren lassen
*/
/* Just for debugging */
System.out.println("Sorted Result Array " + Arrays.toString(resultArray));
return resultArray;
}
}