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>
58 changes: 58 additions & 0 deletions src/Quadratic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.Scanner;

public class Quadratic {

public static void main(String[] args) {

int a;
int b;
int c;
Scanner scanner = new Scanner(System.in);

System.out.print("Enter an amount for a: ");
if (!scanner.hasNextDouble()) {
String word = scanner.next();
System.err.println(word + " is not a number");
return;
}
a = scanner.nextInt();

System.out.print("Enter an amount for b: ");
if (!scanner.hasNextDouble()) {
String word = scanner.next();
System.err.println(word + " is not a number");
return;
}
b = scanner.nextInt();

System.out.print("Enter an amount for c: ");
if (!scanner.hasNextDouble()) {
String word = scanner.next();
System.err.println(word + " is not a number");
return;
}
c = scanner.nextInt();

quadriticEquation(a, b, c);


}

public static void quadriticEquation(int a, int b, int c) {

double discriminant = (b * b) - (4 * a * c);
double divide2A = (2 * a);

if (discriminant < 0 || divide2A == 0) {
// Discriminant is negative & Division by zero, no real roots
System.out.println("No real roots exist for the given quadratic equation.");
} else {
double plusX = ((-b) + Math.sqrt(discriminant)) / divide2A;
double minusX = ((-b) - Math.sqrt(discriminant)) / divide2A;

System.out.printf("The solutions are: x1 = %.02f, x2 = %.02f", plusX, minusX);
}

}

}