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

/**
*
* @author James Ward
*
* @since 02/25/2024
*
*/

public class Quadratic {

public static void main(String[] args) {

System.out.println("Enter positive integer values for variables a, b, and c in to calculate to roots of the quadratic equation ax^2 + bx + c = 0. Positive integers only.");

// prompt for each variable
int a = userInput("a");
int b = userInput("b");
int c = userInput("c");

// calculate roots
double[] roots = quadraticEquation(a, b, c);
System.out.printf("The roots of the quadratic equation are: %.2f and %.2f%n", roots[0], roots[1]);

}

public static int userInput(String variableString) {

Scanner in = new Scanner(System.in);

System.out.println("Enter value for " + variableString + ": ");

if (!in.hasNextInt()) {
String message = in.next();
System.err.println("Input " + message + " is not a positive integer.");
return userInput(variableString);
}

return in.nextInt();
}

public static double[] quadraticEquation(int a, int b, int c) {

// check for division by zero
if (a == 0) {
System.err.println("Invalid input: 'a' can't be zero");
}

// calculate the discriminant
double discriminant = b * b - 4 * a * c;

// check for negative discriminant
if (discriminant < 0) {
System.err.println("No real roots: Discriminant is negative");
}

// Calculate the solutions
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);

return new double[]{root1, root2};
}
}