Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Java-Assignment-005.iml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}$$
Expand Down
51 changes: 51 additions & 0 deletions src/Quadratic.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}