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>
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
## Assignment

**Objective:**
Create a file named `src/Quadratic.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}$$
division by zero or taking the square root of a negative number.
- Validate all inputs to prevent input mismatch exceptions.
- Display specific error messages that include details about invalid input.Create a file named `src/Quadratic.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}$$

**Requirements:**
- Prompt the user to input integers for \(a\), \(b\), and \(c\).
- Compute the two solutions for \(x\) using the quadratic formula.
- Display the results.

**Additional Instructions:**
- Your program should handle inputs where there is only one or no solution. Avoid division by zero or taking the square root of a negative number.
- Validate all inputs to prevent input mismatch exceptions.
- Display specific error messages that include details about invalid input.
- Your program should handle inputs where there is only one or no solution. Avoid

## Submission
Follow these steps for submission:
Expand Down
1 change: 1 addition & 0 deletions src/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

53 changes: 53 additions & 0 deletions src/Quadratic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.Scanner;

public class Quadratic {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the coefficients of the quadratic equation (a, b, c):");

int a = 0, b = 0, c = 0;

System.out.print("a: ");
if (scanner.hasNextInt()) {
a = scanner.nextInt();
} else {
System.out.println("Invalid input. a must be an integer.");
return;
}

System.out.print("b: ");
if (scanner.hasNextInt()) {
b = scanner.nextInt();
} else {
System.out.println("Invalid input. b must be an integer.");
return;
}

System.out.print("c: ");
if (scanner.hasNextInt()) {
c = scanner.nextInt();
} else {
System.out.println("Invalid input. c must be an integer.");
return;
}

if (a == 0) {
System.out.println("Invalid input. 'a' coefficient cannot be zero.");
return;
}

double discriminant = b * b - 4 * a * c;

if (discriminant < 0) {
System.out.println("No real roots. Discriminant is negative.");
return;
}

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

System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
}
}