From fbd72ff122b06b1575ad330859d53142adf63d39 Mon Sep 17 00:00:00 2001 From: prodriguez988 Date: Sun, 25 Feb 2024 03:07:50 -0800 Subject: [PATCH 1/2] Completed Assignment 005 --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 +++++ Java-Assignment-005.iml | 1 + src/Quadratic.java | 51 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 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..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/src/Quadratic.java b/src/Quadratic.java new file mode 100644 index 0000000..af512c5 --- /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); + } + } +} \ No newline at end of file From f667db70c71480b2c4de033f3b0fde71913aa65d Mon Sep 17 00:00:00 2001 From: prodriguez988 Date: Sun, 25 Feb 2024 03:15:05 -0800 Subject: [PATCH 2/2] Complete Assignment 005 --- README.md | 4 +-- src/Quadratic.java | 80 +++++++++++++++++++++++----------------------- 2 files changed, 42 insertions(+), 42 deletions(-) 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 index af512c5..29f6141 100644 --- a/src/Quadratic.java +++ b/src/Quadratic.java @@ -1,51 +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; + 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."); + 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("Error in computing formula as the roots of the quadratic equation DO NOT equal 0."); + System.out.println("Cannot compute quadratic formula, cannot take the square root of a negative number."); } - } 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); + 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); + } } } -} \ No newline at end of file