forked from Christian.Pape/gsd-questions
Joerg Kreiker
7 years ago
1 changed files with 94 additions and 11 deletions
@ -1,21 +1,104 @@ |
|||
/* |
|||
* 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 { |
|||
|
|||
/** |
|||
* @param args the command line arguments |
|||
*/ |
|||
/* Main Program */ |
|||
public static void main(String[] args) { |
|||
System.out.println("Hello GSD \n"); |
|||
|
|||
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; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue