From 4f8305bd5f9bac32752d96dad25fa8c6c18d76c8 Mon Sep 17 00:00:00 2001 From: crucial707 Date: Thu, 22 Feb 2024 11:38:55 -0800 Subject: [PATCH] Java Assignment 005 Austin Barnett --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 ++++++ Java-Assignment-005.iml | 1 + src/Quadratic.java | 48 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 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..69ace3f 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..f2e7cad --- /dev/null +++ b/src/Quadratic.java @@ -0,0 +1,48 @@ +// Written By: Austin Barnett +// Date: 2/22/2024 +// Version 1.0 + +import java.util.Scanner; + +public class Quadratic { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter three integers (a, b, c) for the quadratic equation ax^2 + bx + c = 0: "); + + if (!scanner.hasNextInt()) { + System.out.println("Error: Please enter a valid integer."); + return; + } + int a = scanner.nextInt(); + if (a == 0) { + System.out.println("Error: 'a' should not be zero."); + return; + } + + if (!scanner.hasNextInt()) { + System.out.println("Error: Please enter a valid integer."); + return; + } + int b = scanner.nextInt(); + + if (!scanner.hasNextInt()) { + System.out.println("Error: Please enter a valid integer."); + return; + } + int c = scanner.nextInt(); + + double discriminant = Math.pow(b, 2) - 4 * a * c; + + if (discriminant > 0) { + double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); + double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); + System.out.println("The roots are " + root1 + " and " + root2); + } else if (discriminant == 0) { + double root = (double) -b / (2 * a); + System.out.println("The root is " + root); + } else { + System.out.println("The equation has no real roots."); + } + } +}