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/README.md b/README.md index 216e118..6a974d0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Quadratic Equation Solver - Java Assignment 005 +# NotIt Equation Solver - Java Assignment 005 **Instructions:** 1. Fork this repository to your GitHub account. @@ -8,7 +8,7 @@ ## Assignment **Objective:** -Create a file named `src/Quadratic.java` and write a program that calculates the roots of the quadratic equation: +Create a file named `src/NotIt.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}$$ diff --git a/src/Quadratic.java b/src/Quadratic.java new file mode 100644 index 0000000..29f6141 --- /dev/null +++ b/src/Quadratic.java @@ -0,0 +1,51 @@ + +import java.util.Scanner; + + public class Quadratic { + public static void calculateQuadratic(int a, int b, int c) { + double firstPart, squareRootofQuadratic, denominator, xpositive, xnegative, checkForRootspositive, checkForRootsNegative; + firstPart = Math.pow(b, 2) - (4 * a * c); + squareRootofQuadratic = Math.sqrt(firstPart); + denominator = 2 * a; + xpositive = (-b + squareRootofQuadratic) / denominator; + xnegative = (-b - squareRootofQuadratic) / denominator; + checkForRootsNegative = (a * Math.pow(xnegative, 2)) + (b * xnegative) + c; + checkForRootspositive = (a * Math.pow(xpositive, 2)) + (b * xpositive) + c; + + if (firstPart >= 0) { + if (checkForRootspositive == 0) { + System.out.println("a= " + a); + System.out.println("b= " + b); + System.out.println("c= " + c); + System.out.println("The quadratic formula states that x= " + xpositive+ " or " + xnegative); + System.out.print("The quadratic equation ax^2+bx+c using x= " +xpositive+" is equal to " + checkForRootspositive+". Therefore x= "+xpositive+" a solution to the quadratic equation."); + } else if (checkForRootsNegative == 0) { + System.out.println("a= " + a); + System.out.println("b= " + b); + System.out.println("c= " + c); + System.out.println("The quadratic formula states that x= " + xnegative+ " or "+xpositive); + System.out.print("The quadratic equation ax^2+bx+c using x= " +xnegative+" is equal to " + checkForRootsNegative+". Therefore x= "+xnegative+" a solution to the quadratic equation."); + + } else { + System.out.println("Error in computing formula as the roots of the quadratic equation DO NOT equal 0."); + } + } else { + System.out.println("Cannot compute quadratic formula, cannot take the square root of a negative number."); + } + + } + + public static void main(String[] args) { + int a, b, c; + Scanner in = new Scanner(System.in); + System.out.println("Enter three possible integer values for a, b, and c of the quadratic formula."); + a = in.nextInt(); + b = in.nextInt(); + c = in.nextInt(); + if (a == 0) { + System.out.println("a CANNOT equal to zero in the quadratic formula."); + } else { + calculateQuadratic(a, b, c); + } + } + }