From b2f2458e7fce0d3947f09aa7efd52d47d34bc86b Mon Sep 17 00:00:00 2001 From: jcheong641 Date: Sun, 25 Feb 2024 20:17:25 -0800 Subject: [PATCH] Completed Assignment 05 --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 ++++++ Java-Assignment-005.iml | 1 + src/Quadratic.java | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .idea/vcs.xml create mode 100644 src/Quadratic.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..47478b9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Java-Assignment-005.iml b/Java-Assignment-005.iml index b46c0dd..e1006ff 100644 --- a/Java-Assignment-005.iml +++ b/Java-Assignment-005.iml @@ -5,6 +5,7 @@ + \ No newline at end of file diff --git a/src/Quadratic.java b/src/Quadratic.java new file mode 100644 index 0000000..956f4a6 --- /dev/null +++ b/src/Quadratic.java @@ -0,0 +1,36 @@ +import java.util.Scanner; +public class Quadratic { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter the coefficients of the quadratic equation ax^2 + bx + c = 0:"); + System.out.print("Input an integer for a: "); + double a = getValidDoubleInput(scanner); + + System.out.print("Input an integer for b: "); + double b = getValidDoubleInput(scanner); + + System.out.print("Input an integer for c: "); + double c = getValidDoubleInput(scanner); + + double determinant = b * b - 4 * a * c; + if (determinant > 0) { + double root1 = (-b + Math.sqrt(determinant)) / (2 * a); + double root2 = (-b - Math.sqrt(determinant)) / (2 * a); + System.out.println("There are two distinct real roots: Root1 = " + root1 + ", Root2 = " + root2); + } else if (determinant == 0) { + double root = -b / (2 * a); + System.out.println("There is one identical real root: Root = " + root); + } else { + System.out.println("There are no real roots. Roots are complex numbers."); + } + } + + private static double getValidDoubleInput(Scanner scanner) { + while (!scanner.hasNextDouble()) { + System.out.println("This is an invalid input. Input a valid number."); + scanner.next(); + } + return scanner.nextDouble(); + } +}