From 3cc0ea441e9bbbf9094b46b4c0174bd20b01a7f0 Mon Sep 17 00:00:00 2001 From: Trevor Hartman <100978684+thartmanoftheredwoods@users.noreply.github.com> Date: Thu, 25 Jan 2024 17:42:37 -0800 Subject: [PATCH] John Dennis Assignment005 --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 +++++ Java-Assignment-005.iml | 1 + README.md | 14 +++++------ src/.gitkeep | 1 + src/Quadratic.java | 53 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/.gitkeep create mode 100644 src/Quadratic.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..6f29fee 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/README.md b/README.md index 216e118..1e054b8 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,12 @@ ## Assignment **Objective:** -Create a file named `src/Quadratic.java` and write a program that calculates the roots of the quadratic equation: -$$ax^2 + bx + c = 0$$ -using the quadratic formula: -$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$ +division by zero or taking the square root of a negative number. +- Validate all inputs to prevent input mismatch exceptions. +- Display specific error messages that include details about invalid input.Create a file named `src/Quadratic.java` and write a program that calculates the roots of the quadratic equation: + $$ax^2 + bx + c = 0$$ + using the quadratic formula: + $$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$ **Requirements:** - Prompt the user to input integers for \(a\), \(b\), and \(c\). @@ -19,9 +21,7 @@ $$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$ - Display the results. **Additional Instructions:** -- Your program should handle inputs where there is only one or no solution. Avoid division by zero or taking the square root of a negative number. -- Validate all inputs to prevent input mismatch exceptions. -- Display specific error messages that include details about invalid input. +- Your program should handle inputs where there is only one or no solution. Avoid ## Submission Follow these steps for submission: diff --git a/src/.gitkeep b/src/.gitkeep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/.gitkeep @@ -0,0 +1 @@ + diff --git a/src/Quadratic.java b/src/Quadratic.java new file mode 100644 index 0000000..4704136 --- /dev/null +++ b/src/Quadratic.java @@ -0,0 +1,53 @@ +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 (a, b, c):"); + + int a = 0, b = 0, c = 0; + + System.out.print("a: "); + if (scanner.hasNextInt()) { + a = scanner.nextInt(); + } else { + System.out.println("Invalid input. a must be an integer."); + return; + } + + System.out.print("b: "); + if (scanner.hasNextInt()) { + b = scanner.nextInt(); + } else { + System.out.println("Invalid input. b must be an integer."); + return; + } + + System.out.print("c: "); + if (scanner.hasNextInt()) { + c = scanner.nextInt(); + } else { + System.out.println("Invalid input. c must be an integer."); + return; + } + + if (a == 0) { + System.out.println("Invalid input. 'a' coefficient cannot be zero."); + return; + } + + double discriminant = b * b - 4 * a * c; + + if (discriminant < 0) { + System.out.println("No real roots. Discriminant is negative."); + return; + } + + double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); + double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); + + System.out.println("Root 1: " + root1); + System.out.println("Root 2: " + root2); + } +} \ No newline at end of file