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

//import static java.lang.Math.pow;

public class Quadratic {
public static void main(String[] args) {
double root1, root2;
//Prompt the user to input integers for (a), (b), and (c).
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a:");
int inputa =ValidateInput(scanner.nextLine());
System.out.print("Enter b:");
int inputb = ValidateInput(scanner.nextLine());
System.out.print("Enter c:");
int intputc= ValidateInput(scanner.nextLine());
// calculate the determinant (b2 - 4ac)
double determinant = inputb*inputb-4.0*inputa*intputc;
// check if determinant is greater than 0
if(determinant >0.0){
// two real and distinct roots
root1=(-inputb + Math.sqrt(determinant ))/(2.0*inputa);
root2=(-inputb - Math.sqrt(determinant ))/(2.0*inputa);
System.out.printf("The root1 is %.2f and root2 is %.2f ",root1,root2 );
// check if determinant is equal to 0
}else if(determinant ==0.0){
// two real and equal roots
// determinant is equal to 0
// so -b + 0 == -b
root1 = root2 = -inputb / (2.0 * inputa);
System.out.printf("root1 = root2 = %.2f ",root1);
// if determinant is less than zero
}else{
// roots are complex number and distinct
double real = -inputb / (2.0 * inputa);
double imaginary = Math.sqrt(-determinant) / (2.0 * inputa);
System.out.format("root1 = %.2f+%.2fi", real, imaginary);
System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary);
}





}
public static int ValidateInput(String input){
try {
int value = Integer.parseInt(input);
if(value<=0){
System.out.println("a can't be 0");
System.exit(1);
}
return value;

}catch (NumberFormatException e){
System.out.println("Invalid input and please enter a valid int");
System.exit(1);
return 0;
}
}
}