Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. public class Formulas {
  2.  
  3.     /**
  4.      * Computes the possible solutions for a polynomial equation of the second
  5.      * degree. ax^2 + bx + c = 0
  6.      *
  7.      * The method prints an error message if the input parameters a, b and c are
  8.      * not valid. Further three different types of outcome are distinguished.
  9.      * There might be 0, 1 or 2 solutions to a quadratic equation.
  10.      *
  11.      * @param a
  12.      *            coefficient for x^2
  13.      * @param b
  14.      *            coefficient for x
  15.      * @param c
  16.      *            constant
  17.      */
  18.     public static void quadraticEquation(double a, double b, double c) {
  19.         double x1;                        
  20.         double x2;
  21.         if ( (b*b - 4*a*c) < 0)
  22.         System.out.println ("Ungültige Eingabe (Determinante<0!)");
  23.         else
  24.         x1 = (-b + Math.sqrt((b*b - 4*a*c))) / 2*a;
  25.         System.out.println ("x1 =" + x1);
  26.         x2 = (-b - Math.sqrt((b*b - 4*a*c))) / 2*a;
  27.         System.out.println ("x2 =" + x2);
  28.     }
  29.  
  30.     /**
  31.      * Computes the factorial of n with a while loop.
  32.      *
  33.      * @param n
  34.      *            input value
  35.      * @return factorial of n
  36.      */
  37.     public static int factorialWhile(int n) {
  38.             int result = 1;               // Ergebnisvariable = 1 --> Startwert
  39.             int i = 1;                    // "Counter"-Variable
  40.             while (i <= n) {              // Solange i <= n...
  41.             result = result*i;            // multipliziere aktuelles Ergebnis mit i;
  42.             i++;                          // inkrementiere i, danach wird die Bedingung der Whole Schleife erneut überprüft
  43.             }
  44.         return result; // TODO
  45.     }
  46.  
  47.     /**
  48.      * Computes the factorial of n with a for loop.
  49.      *
  50.      * @param n
  51.      *            input value
  52.      * @return factorial of n
  53.      */
  54.     public static int factorialFor(int n) {
  55.             int result = 1;             // Ergebnisvariable = 1 --> Startwert
  56.             int i;                      // "Counter"-Variable
  57.             for (i = 1; i <= n; i++) {  // For-Schleife: Setze i = 1, solange wie i <= n ist, inkrementiere i
  58.             result = result*i;          // multipliziere aktuelles Ergebnis mit der inkrementierten Variable i
  59.             }
  60.         return result; // TODO
  61.     }
  62.  
  63.     /**
  64.      * Computes the binomial for the input values n and k.
  65.      *
  66.      * @param n
  67.      *            input value (n-element set)
  68.      * @param m
  69.      *            input value (k-element subsets)
  70.      * @return binomial of n and m
  71.      */
  72.     public static int binomial(int n, int k) {
  73.         int result           = 1;                     // Ergebnisvariable = 1 --> Startwer                
  74.         int resultNfaculty   = 1;                     // repräsentiert n!
  75.         int resultKfaculty   = 1;                     // repräsentiert k!
  76.         int resultNKfaculty  = 1;                     // repräsentiert (n-k)!
  77.         int i;                                        // "Counter"-Variable für n
  78.         int j;                                        // "Counter"-Variable für k
  79.         int p;                                        // "Counter"-Variable für (n-k)
  80.         // Berechnung n!:
  81.         for (i = 1; i <= n; i++) {                    // For-Schleife: Setze i = 1, solange wie i <= n ist, inkrementiere i
  82.         resultNfaculty = resultNfaculty * i;          // multipliziere aktuelles Ergebnis mit der inkrementierten Variable i
  83.             }
  84.         // Berechnung k!:  
  85.         for (j = 1; j <= k; j++) {
  86.         resultKfaculty = resultKfaculty * j;
  87.             }
  88.         // Berechnung (n-k)!:
  89.         for (p = 1; p <= (n-k); p++) {
  90.         resultNKfaculty = resultNKfaculty * p;
  91.             }
  92.         // Berechnung Binomialkoeffizient:
  93.         result = ( resultNfaculty ) / ( (resultKfaculty) * (resultNKfaculty) );
  94.         return result; // TODO
  95.     }
  96.  
  97.     /**
  98.      * Runs the program.
  99.      */
  100.     public static void main(String[] args) {
  101.         System.out.println("3! mit While Schleife = " +factorialWhile(3)); // Test der Funktion factorialWhile für "3!"
  102.         System.out.println("4! mit For Schleife = " +factorialFor(4)); // Test der Funktion factorialFor für "4!"
  103.         System.out.println("Binomialkoeffizient von n=3 und k=2 = " +binomial(3,2)); // Test der Funktion binomial für n=3 und k=2
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement