From 1a55157dc5381afbe3a896ff2f5aab451f98fd4c Mon Sep 17 00:00:00 2001 From: Dominik Stahl Date: Thu, 30 Jan 2025 09:07:40 +0100 Subject: [PATCH] Add Quadratics class --- .../control_structures/Quadratics.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/de/dhbwka/java/exercise/control_structures/Quadratics.java diff --git a/src/de/dhbwka/java/exercise/control_structures/Quadratics.java b/src/de/dhbwka/java/exercise/control_structures/Quadratics.java new file mode 100644 index 0000000..f4f2d24 --- /dev/null +++ b/src/de/dhbwka/java/exercise/control_structures/Quadratics.java @@ -0,0 +1,36 @@ +package de.dhbwka.java.exercise.control_structures; + +public class Quadratics { + public static void main(String[] args) { + java.util.Scanner scan = new java.util.Scanner(System.in); + + System.out.print("a: "); + double a = scan.nextDouble(); + System.out.print("b: "); + double b = scan.nextDouble(); + System.out.print("c: "); + double c = scan.nextDouble(); + + scan.close(); + + if (a == 0) { + if (b == 0) { + System.out.println("Die Gleichung ist degeneriert."); + } else { + double x = -c / b; + System.out.println("x = " + x); + } + } else { + double d = b * b - 4 * a * c; + if (d >= 0) { + double x1 = (-b + Math.sqrt(d)) / (2 * a); + double x2 = (-b - Math.sqrt(d)) / (2 * a); + System.out.println("x1 = " + x1 + ", x2 = " + x2); + } else { + double real = -b / (2 * a); + double imag = Math.sqrt(-d) / (2 * a); + System.out.println("x1 = " + real + " + " + imag + "i, x2 = " + real + " - " + imag + "i"); + } + } + } +}