Created
April 2, 2014 18:37
-
-
Save lucasdinonolte/9940312 to your computer and use it in GitHub Desktop.
Some Java Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Das Resultat passt nicht, weil long 8 byte groß ist, also maximal 1.8446744e+19 sein kann | |
// 49! ist aber 6.0828186e+62 und passt nicht rein, also schneidet der die Bytes ab und das Ergebnis passt nicht mehr | |
public class BinomService { | |
public static void main(String[] args) { | |
System.out.println(binom(49, 6)); | |
} | |
public static long fac(int n) { | |
if(n == 1) { | |
return 1; | |
} | |
return fac(n-1) * n; | |
} | |
public static long binom(int n, int k) { | |
long nFac = fac(n); | |
long kFac = fac(k); | |
int nk = n - k; | |
long nkFac = fac(nk); | |
return nFac / (kFac * nkFac); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EvenService { | |
public static void main(String[] args) { | |
// Array mit Zahlen, um Funktion zu testen | |
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; | |
// Schleife, die durchs Array geht und alle Zahlen in die isEven() Funktion schmeißt | |
for(int i = 0; i < numbers.length; i++) { | |
int number = numbers[i]; | |
if(isEven(number)) { | |
System.out.println(number + " is even"); | |
} else { | |
System.out.println(number + " is odd"); | |
} | |
} | |
} | |
// Gibt einen Boolean Wert zurück (Ja oder Nein) anstatt selber System.out.println zu machen | |
// Sollte man so machen, weil man ja nie weiß, was derjenige, der die Funktion aufruft mit der Antwort machen will ;) | |
public static boolean isEven(int n) { | |
if((n % 2) == 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HelloService { | |
public static void main(String[] args) { | |
System.out.println(hello("Frau", "Schmidt")); | |
System.out.println(hello("Herr", "Schmidt")); | |
System.out.println(hello("Ball", "Fussball")); | |
} | |
public static String hello(String anrede, String name) { | |
String output = "Hallo, "; | |
if(anrede == "Frau") { | |
output.concat("Frau "); | |
} else if(anrede == "Herr") { | |
output.concat("Herr "); | |
} else { | |
return new String("Ungueltige Anrede!"); | |
} | |
return output.concat(name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment