From e26a701b602df5657b3be91f77de31ab786473a5 Mon Sep 17 00:00:00 2001 From: Tyler-FS Date: Sun, 25 Feb 2024 19:12:26 -0800 Subject: [PATCH] Completed Java Assignment 005 --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 +++++ Java-Assignment-005.iml | 1 + src/Quadratic.java | 56 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 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..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/src/Quadratic.java b/src/Quadratic.java new file mode 100644 index 0000000..4e01e63 --- /dev/null +++ b/src/Quadratic.java @@ -0,0 +1,56 @@ +import java.util.Scanner; + +public class Quadratic { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + System.out.println("Lets solve for 'x' in the quadratic equation: ax^2 + bx + c = 0"); + + //Prompt user to provide a value for a, validate value is integer and not 0 + System.out.println("Provide a value for a: "); + if (!in.hasNextInt()) { + String word = in.next(); + System.err.println(word + " is not a valid integer."); + return; + } + int a = in.nextInt(); + if (a == 0) { + System.err.println("a cannot be 0."); + return; + } + + //Prompt user to provide a value for b, validate value is integer + System.out.println("Provide a value for b: "); + if (!in.hasNextInt()) { + String word = in.next(); + System.err.println(word + " is not a valid integer."); + return; + } + int b = in.nextInt(); + + //Prompt user to provide a value for c, validate value is integer + System.out.println("Provide a value for c: "); + if (!in.hasNextInt()) { + String word = in.next(); + System.err.println(word + " is not a valid integer."); + return; + } + int c = in.nextInt(); + + //Calculate discriminate and solve + double discriminate = (Math.pow(b,2) - (4 * a * c)); + double plus = (-b + Math.sqrt(discriminate)) / (2 * a); + double minus = (-b - Math.sqrt(discriminate)) / (2 * a); + + //Validate not a negative square root and whether there is only one solution + if(discriminate >= 0){ + if(discriminate == 0){ + System.out.printf("x = %.1f", plus); + } else { + System.out.printf("x = %.1f, %.1f", plus, minus); + } + }else { + System.err.println("Error: Inputs would result in a negative square root."); + } + + } +}