Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Formulas {
- /**
- * Computes the possible solutions for a polynomial equation of the second
- * degree. ax^2 + bx + c = 0
- *
- * The method prints an error message if the input parameters a, b and c are
- * not valid. Further three different types of outcome are distinguished.
- * There might be 0, 1 or 2 solutions to a quadratic equation.
- *
- * @param a
- * coefficient for x^2
- * @param b
- * coefficient for x
- * @param c
- * constant
- */
- public static void quadraticEquation(double a, double b, double c) {
- double x1;
- double x2;
- if ( (b*b - 4*a*c) < 0)
- System.out.println ("Ungültige Eingabe (Determinante<0!)");
- else
- x1 = (-b + Math.sqrt((b*b - 4*a*c))) / 2*a;
- System.out.println ("x1 =" + x1);
- x2 = (-b - Math.sqrt((b*b - 4*a*c))) / 2*a;
- System.out.println ("x2 =" + x2);
- }
- /**
- * Computes the factorial of n with a while loop.
- *
- * @param n
- * input value
- * @return factorial of n
- */
- public static int factorialWhile(int n) {
- int result = 1; // Ergebnisvariable = 1 --> Startwert
- int i = 1; // "Counter"-Variable
- while (i <= n) { // Solange i <= n...
- result = result*i; // multipliziere aktuelles Ergebnis mit i;
- i++; // inkrementiere i, danach wird die Bedingung der Whole Schleife erneut überprüft
- }
- return result; // TODO
- }
- /**
- * Computes the factorial of n with a for loop.
- *
- * @param n
- * input value
- * @return factorial of n
- */
- public static int factorialFor(int n) {
- int result = 1; // Ergebnisvariable = 1 --> Startwert
- int i; // "Counter"-Variable
- for (i = 1; i <= n; i++) { // For-Schleife: Setze i = 1, solange wie i <= n ist, inkrementiere i
- result = result*i; // multipliziere aktuelles Ergebnis mit der inkrementierten Variable i
- }
- return result; // TODO
- }
- /**
- * Computes the binomial for the input values n and k.
- *
- * @param n
- * input value (n-element set)
- * @param m
- * input value (k-element subsets)
- * @return binomial of n and m
- */
- public static int binomial(int n, int k) {
- int result = 1; // Ergebnisvariable = 1 --> Startwer
- int resultNfaculty = 1; // repräsentiert n!
- int resultKfaculty = 1; // repräsentiert k!
- int resultNKfaculty = 1; // repräsentiert (n-k)!
- int i; // "Counter"-Variable für n
- int j; // "Counter"-Variable für k
- int p; // "Counter"-Variable für (n-k)
- // Berechnung n!:
- for (i = 1; i <= n; i++) { // For-Schleife: Setze i = 1, solange wie i <= n ist, inkrementiere i
- resultNfaculty = resultNfaculty * i; // multipliziere aktuelles Ergebnis mit der inkrementierten Variable i
- }
- // Berechnung k!:
- for (j = 1; j <= k; j++) {
- resultKfaculty = resultKfaculty * j;
- }
- // Berechnung (n-k)!:
- for (p = 1; p <= (n-k); p++) {
- resultNKfaculty = resultNKfaculty * p;
- }
- // Berechnung Binomialkoeffizient:
- result = ( resultNfaculty ) / ( (resultKfaculty) * (resultNKfaculty) );
- return result; // TODO
- }
- /**
- * Runs the program.
- */
- public static void main(String[] args) {
- System.out.println("3! mit While Schleife = " +factorialWhile(3)); // Test der Funktion factorialWhile für "3!"
- System.out.println("4! mit For Schleife = " +factorialFor(4)); // Test der Funktion factorialFor für "4!"
- System.out.println("Binomialkoeffizient von n=3 und k=2 = " +binomial(3,2)); // Test der Funktion binomial für n=3 und k=2
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement