This commit is contained in:
Luca Conte 2022-11-17 16:42:13 +01:00
parent 971a07a3af
commit 7bfb8cbc0c
2 changed files with 58 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,58 @@
/**
* Klasse zum berechnen Quadratischer gleichungen
*/
public class Quadratic {
public static void main(String[] args) {
quadratic(1, -12, 35);
quadratic(0.2, -0.8, 0.8);
quadratic(1, 12, 37);
quadratic(0.1, 0.6, 0.9);
quadratic(1.8, 1.2, 0.2);
quadratic(0, 4, 7);
quadratic(1, -4.2, 4.21);
}
/**
* berechnet alle reellen Lösungen einer Quadratischen Gleichung des Formats a*x*x + b*x + c
* a != 0
* Lösungen werden mit System.out.println ausgegeben, nicht zurueckgegeben
*/
public static void quadratic(double a, double b, double c) {
double epsilon = 0.0000000001;
if (Math.abs(a) < epsilon) {
System.out.println("a muss ungleich 0 sein");
return;
}
double wurzelterm = (b * b) - (4.0 * a * c);
if (Math.abs(wurzelterm) < epsilon) {
System.out.println("Lösung: " + simplifiedQuadraticFormula(a, b, 0));
} else if (wurzelterm < 0) {
System.out.println("Keine reelle Lösung");
} else {
double wurzel = Math.sqrt(wurzelterm);
System.out.println("Erste Lösung: " + simplifiedQuadraticFormula(a, b, wurzel));
System.out.println("Zweite Lösung: " + simplifiedQuadraticFormula(a, b, -1 * wurzel));
}
}
/**
* Berechnet eine Loesung einer Quadratischen gleichung
* Plus-Minus wird als Plus interpretiert
* root muss bereits berechnet uebergeben werden
* Um Plus-Minus als Minus zu interpretieren, root zuvor mit -1 multiplizieren
* Ergebnis mittels round1 auf eine Nachkommastelle gerundet
*/
public static double simplifiedQuadraticFormula(double a, double b, double root) {
return round1((-1 * b + root) / (2 * a));
}
/** Rundet eine gegebene Zahl auf eine Nachkommastelle. */
public static double round1(double value) {
if (Double.isNaN(value)) return value;
if (Double.isInfinite(value)) return value;
return Math.round(value * 10) / 10.0;
}
}