From b994f26252bbc7d22a060bb448aadbc86e5ec863 Mon Sep 17 00:00:00 2001 From: Rigel Date: Sun, 25 Feb 2024 23:33:06 -0800 Subject: [PATCH 1/2] 2/25/2024 Ry Luciani --- .idea/misc.xml | 2 +- Java-Assignment-005.iml | 1 + src/Quadratic.java | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/Quadratic.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..bcb5da6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +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..ab2d2f4 --- /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.print("Enter coefficient a: "); + double a = scanner.nextDouble(); + + System.out.print("Enter coefficient b: "); + double b = scanner.nextDouble(); + + System.out.print("Enter coefficient c: "); + double c = scanner.nextDouble(); + + double discriminant = b * b - 4 * a * c; + if(discriminant < 0){ + System.out.printf("You are attempting to use a negative number in a radical"); + System.exit(0); + } + if(a<0||b<0||c<0){ + System.out.printf("You have entered a negative number as an input");System.exit(0);} + + if (discriminant > 0) { + double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); + double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); + System.out.printf("Roots are real and different: root1 = %.2f, root2 = %.2f%n", root1, root2); + } else if (discriminant == 0) { + double root = -b / (2 * a); + System.out.printf("Roots are real and same: root = %.2f%n", root); + } else { + double realPart = -b / (2 * a); + double imaginaryPart = Math.sqrt(-discriminant) / (2 * a); + System.out.printf("Roots are complex and different: root1 = %.2f + %.2fi, root2 = %.2f - %.2fi%n", realPart, imaginaryPart, realPart, imaginaryPart); + }}} From 7119d1e398d83473702911cd75765bb53b317caa Mon Sep 17 00:00:00 2001 From: Rigel Date: Sun, 25 Feb 2024 23:33:06 -0800 Subject: [PATCH 2/2] 2/25/2024 Ry Luciani --- .idea/vcs.xml | 6 ++++++ src/Quadratic.java | 5 +++++ 2 files changed, 11 insertions(+) create mode 100644 .idea/vcs.xml 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/src/Quadratic.java b/src/Quadratic.java index ab2d2f4..01d84c6 100644 --- a/src/Quadratic.java +++ b/src/Quadratic.java @@ -15,6 +15,10 @@ public static void main(String[] args) { double c = scanner.nextDouble(); double discriminant = b * b - 4 * a * c; + + if(a==0){System.out.printf("You are attempting to divide by zero"); + System.exit(0);} + if(discriminant < 0){ System.out.printf("You are attempting to use a negative number in a radical"); System.exit(0); @@ -26,6 +30,7 @@ public static void main(String[] args) { double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); System.out.printf("Roots are real and different: root1 = %.2f, root2 = %.2f%n", root1, root2); + } else if (discriminant == 0) { double root = -b / (2 * a); System.out.printf("Roots are real and same: root = %.2f%n", root);